183 lines
4.8 KiB
Go
183 lines
4.8 KiB
Go
package postgres_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.fsdpf.net/go/db/v2/engine"
|
|
"git.fsdpf.net/go/db/v2/schema"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type postgresTest struct {
|
|
suite.Suite
|
|
schema schema.Schema
|
|
}
|
|
|
|
var (
|
|
tableName = "entry"
|
|
|
|
dropTable = "DROP TABLE IF EXISTS entry;"
|
|
|
|
createTable = "CREATE TABLE IF NOT EXISTS entry (" +
|
|
"id SERIAL PRIMARY KEY," +
|
|
"int INTEGER NOT NULL UNIQUE," +
|
|
"float REAL NOT NULL," +
|
|
"string VARCHAR(255) NOT NULL," +
|
|
"time TIMESTAMP NOT NULL," +
|
|
"bool BOOLEAN NOT NULL," +
|
|
"bytes BYTEA NOT NULL" +
|
|
");"
|
|
)
|
|
|
|
func TestPostgresSuite(t *testing.T) {
|
|
suite.Run(t, new(postgresTest))
|
|
}
|
|
|
|
func (t *postgresTest) SetupSuite() {
|
|
db := engine.Open(map[string]engine.DBConfig{
|
|
"test-postgres": engine.NewDBConfig("postgres",
|
|
engine.WithHost(os.Getenv("POSTGRES_HOST")),
|
|
engine.WithPort(os.Getenv("POSTGRES_PORT")),
|
|
engine.WithDatabase(os.Getenv("POSTGRES_DB")),
|
|
engine.WithUsername(os.Getenv("POSTGRES_USER")),
|
|
engine.WithPassword(os.Getenv("POSTGRES_PASSWD")),
|
|
),
|
|
}).Connection("test-postgres")
|
|
|
|
t.schema = schema.GetSchemaDialect(db)
|
|
|
|
db.Exec(dropTable)
|
|
db.Exec(createTable)
|
|
}
|
|
|
|
func (t *postgresTest) TestGetColumnListing() {
|
|
result, err := t.schema.GetColumnListing(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().ElementsMatch([]string{"id", "int", "float", "string", "time", "bool", "bytes"}, result)
|
|
}
|
|
|
|
func (t *postgresTest) TestTableExists() {
|
|
result, err := t.schema.TableExists(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().Equal(result, true)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileCreate() {
|
|
bp := schema.NewBlueprint("users")
|
|
bp.Create()
|
|
|
|
bp.BigIncrements("id").AutoIncrement().Comment("ID")
|
|
bp.Boolean("enabled").Default("true").Comment("是否有效")
|
|
bp.Char("created_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("创建者")
|
|
bp.Char("owned_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("拥有者")
|
|
bp.Timestamp("created_at").UseCurrent().Comment("创建时间")
|
|
bp.Timestamp("updated_at").UseCurrent().Comment("更新时间")
|
|
bp.DateTime("deleted_at").Nullable().Comment("删除时间")
|
|
|
|
sql := t.schema.CompileCreate(bp)
|
|
|
|
t.Equal([]string{
|
|
"CREATE TABLE \"users\" (\n" +
|
|
"\"id\" bigint NOT NULL BIGSERIAL PRIMARY KEY COMMENT 'ID',\n" +
|
|
"\"enabled\" boolean NOT NULL DEFAULT true COMMENT '是否有效',\n" +
|
|
"\"created_user\" char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000' COMMENT '创建者',\n" +
|
|
"\"owned_user\" char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000' COMMENT '拥有者',\n" +
|
|
"\"created_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
|
|
"\"updated_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
|
|
"\"deleted_at\" timestamp without time zone NULL COMMENT '删除时间'\n" +
|
|
")",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileAdd() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.String("name", 50).Comment("用户名")
|
|
bp.SmallInteger("age").Default("18").Comment("年龄")
|
|
bp.String("sex", 1).Default("0").Comment("性别") // Using string instead of enum
|
|
|
|
sql := t.schema.CompileAdd(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE \"users\"\n" +
|
|
"ADD COLUMN \"name\" varchar(50) NOT NULL COMMENT '用户名',\n" +
|
|
"ADD COLUMN \"age\" smallint NOT NULL DEFAULT 18 COMMENT '年龄',\n" +
|
|
"ADD COLUMN \"sex\" varchar(1) NOT NULL DEFAULT '0' COMMENT '性别'",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileChange() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.String("name", 50).Change("username")
|
|
bp.SmallInteger("age").Default("19").Comment("年龄").Change()
|
|
|
|
sql := t.schema.CompileChange(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE \"users\"\n" +
|
|
"ALTER COLUMN \"name\" TYPE varchar(50) NOT NULL,\n" +
|
|
"ALTER COLUMN \"age\" TYPE smallint NOT NULL DEFAULT 19 COMMENT '年龄'",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileDropColumn() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.DropColumn("age", "sex")
|
|
|
|
sql := t.schema.CompileDropColumn(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE \"users\"\n" +
|
|
"DROP COLUMN \"age\",\n" +
|
|
"DROP COLUMN \"sex\"",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileDrop() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Drop()
|
|
|
|
sql := t.schema.CompileDrop(bp)
|
|
|
|
t.Equal([]string{"DROP TABLE \"users\""}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileDropIfExists() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Drop()
|
|
|
|
sql := t.schema.CompileDropIfExists(bp)
|
|
t.Equal([]string{"DROP TABLE IF EXISTS \"users\""}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
func (t *postgresTest) TestCompileRename() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Rename("user")
|
|
|
|
sql := t.schema.CompileRename(bp)
|
|
|
|
t.Equal([]string{"ALTER TABLE \"users\" RENAME TO \"user\""}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|