github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/driver/postgres/connector_test.go (about) 1 package postgres_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/onsi/gomega" 8 9 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 10 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/driver/postgres" 11 "github.com/machinefi/w3bstream/pkg/depends/testutil/buildertestutil" 12 ) 13 14 func TestConnector(t *testing.T) { 15 c := &postgres.Connector{} 16 17 table := builder.T("t", 18 builder.Col("F_id").Type(uint64(0), ",autoincrement"), 19 builder.Col("f_old_name").Type("", ",deprecated=f_name"), 20 builder.Col("f_name").Type("", ",size=128,default=''"), 21 builder.Col("F_geo").Type(&buildertestutil.Point{}, ""), 22 builder.Col("F_created_at").Type(int64(0), ",default='0'"), 23 builder.Col("F_updated_at").Type(int64(0), ",default='0'"), 24 builder.PrimaryKey(builder.Cols("F_id")), 25 builder.UniqueIndex("I_name", builder.Cols("F_name")).Using("BTREE"), 26 builder.Index("I_created_at", builder.Cols("F_created_at")).Using("BTREE"), 27 builder.Index("I_geo", builder.Cols("F_geo")).Using("SPATIAL"), 28 ) 29 30 cases := map[string]struct { 31 expr builder.SqlExpr 32 expect builder.SqlExpr 33 }{ 34 "CreateDatabase": { 35 c.CreateDatabase("db"), 36 builder.Expr( /* language=PostgreSQL */ `CREATE DATABASE db;`), 37 }, 38 "DropDatabase": { 39 c.DropDatabase("db"), 40 builder.Expr( /* language=PostgreSQL */ `DROP DATABASE IF EXISTS db;`), 41 }, 42 "AddIndex": { 43 c.AddIndex(table.Key("I_name")), 44 builder.Expr( /* language=PostgreSQL */ "CREATE UNIQUE INDEX t_i_name ON t USING BTREE (f_name);"), 45 }, 46 "AddPrimaryKey": { 47 c.AddIndex(table.Key("PRIMARY")), 48 builder.Expr( /* language=PostgreSQL */ "ALTER TABLE t ADD PRIMARY KEY (f_id);"), 49 }, 50 "AddSpatialIndex": { 51 c.AddIndex(table.Key("I_geo")), 52 builder.Expr( /* language=PostgreSQL */ "CREATE INDEX t_i_geo ON t USING GIST (f_geo);"), 53 }, 54 "DropIndex": { 55 c.DropIndex(table.Key("I_name")), 56 builder.Expr( /* language=PostgreSQL */ "DROP INDEX IF EXISTS t_i_name"), 57 }, 58 "DropPrimaryKey": { 59 c.DropIndex(table.Key("PRIMARY")), 60 builder.Expr( /* language=PostgreSQL */ "ALTER TABLE t DROP CONSTRAINT t_pkey;"), 61 }, 62 "CreateTableIsNotExists": { 63 c.CreateTableIsNotExists(table)[0], 64 builder.Expr( /* language=PostgreSQL */ `CREATE TABLE IF NOT EXISTS t ( 65 f_id bigserial NOT NULL, 66 f_name varchar(128) NOT NULL DEFAULT '', 67 f_geo POINT NOT NULL, 68 f_created_at bigint NOT NULL DEFAULT '0', 69 f_updated_at bigint NOT NULL DEFAULT '0', 70 PRIMARY KEY (f_id) 71 );`), 72 }, 73 "DropTable": { 74 c.DropTable(table), 75 builder.Expr( /* language=PostgreSQL */ "DROP TABLE IF EXISTS t;"), 76 }, 77 "TruncateTable": { 78 c.TruncateTable(table), 79 builder.Expr( /* language=PostgreSQL */ "TRUNCATE TABLE t;"), 80 }, 81 "AddColumn": { 82 c.AddColumn(table.Col("F_name")), 83 builder.Expr( /* language=PostgreSQL */ "ALTER TABLE t ADD COLUMN f_name varchar(128) NOT NULL DEFAULT '';"), 84 }, 85 "DropColumn": { 86 c.DropColumn(table.Col("F_name")), 87 builder.Expr( /* language=PostgreSQL */ "ALTER TABLE t DROP COLUMN f_name;"), 88 }, 89 } 90 91 for name, c := range cases { 92 t.Run(name, func(t *testing.T) { 93 gomega.NewWithT(t).Expect(c.expr). 94 To(buildertestutil.BeExpr(c.expr.Ex(context.Background()).Query())) 95 }) 96 } 97 }