193 lines
5.1 KiB
Go
193 lines
5.1 KiB
Go
package sqlite3_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.fsdpf.net/go/db/v2"
|
|
"git.fsdpf.net/go/db/v2/engine"
|
|
"git.fsdpf.net/go/db/v2/schema"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
var (
|
|
tableName = "entry"
|
|
|
|
dropTable = "DROP TABLE IF EXISTS `entry`;"
|
|
|
|
createTable = "CREATE TABLE IF NOT EXISTS `entry` (" +
|
|
"`id` INTEGER PRIMARY KEY AUTOINCREMENT," +
|
|
"`int` INT NOT NULL ," +
|
|
"`float` FLOAT NOT NULL ," +
|
|
"`string` VARCHAR(255) NOT NULL ," +
|
|
"`time` DATETIME NOT NULL ," +
|
|
"`bool` TINYINT NOT NULL ," +
|
|
"`bytes` BLOB NOT NULL" +
|
|
");"
|
|
)
|
|
|
|
type sqlite3Test struct {
|
|
suite.Suite
|
|
schema schema.Schema
|
|
}
|
|
|
|
func TestSqlite3Suite(t *testing.T) {
|
|
suite.Run(t, new(sqlite3Test))
|
|
}
|
|
|
|
func (t *sqlite3Test) SetupSuite() {
|
|
db := engine.Open(map[string]engine.DBConfig{
|
|
"test-sqlite3": engine.NewDBConfig("sqlite3",
|
|
engine.WithSQLiteFile(os.Getenv("DB_FILE")),
|
|
),
|
|
}).Connection("test-sqlite3")
|
|
|
|
t.schema = schema.GetSchemaDialect(db)
|
|
|
|
// db.Exec(dropTable)
|
|
if _, err := db.Exec(createTable); err != nil {
|
|
t.Require().NoError(err)
|
|
}
|
|
}
|
|
|
|
func (t *sqlite3Test) TestTableExists() {
|
|
result, err := t.schema.TableExists(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().Equal(result, true)
|
|
}
|
|
|
|
func (t *sqlite3Test) TestGetColumnListing() {
|
|
result, err := t.schema.GetColumnListing(tableName)
|
|
t.Require().NoError(err)
|
|
t.Require().ElementsMatch([]string{"bool", "bytes", "float", "id", "int", "string", "time"}, result)
|
|
}
|
|
|
|
// 创建列表
|
|
func (t *sqlite3Test) TestCompileCreate() {
|
|
bp := schema.NewBlueprint("users")
|
|
bp.Create()
|
|
bp.Charset("utf8mb4")
|
|
|
|
bp.BigIncrements("id").AutoIncrement().Comment("ID")
|
|
bp.Boolean("enabled").Default("1").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().Default(db.L("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
|
bp.DateTime("deleted_at").Nullable().Comment("删除时间")
|
|
|
|
sql := t.schema.CompileCreate(bp)
|
|
|
|
t.Equal([]string{
|
|
"CREATE TABLE `users` (\n" +
|
|
"`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, -- ID\n" +
|
|
"`enabled` tinyint(1) NOT NULL DEFAULT '1', -- 是否有效\n" +
|
|
"`created_user` char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', -- 创建者\n" +
|
|
"`owned_user` char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', -- 拥有者\n" +
|
|
"`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间\n" +
|
|
"`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间\n" +
|
|
"`deleted_at` datetime NULL -- 删除时间\n" +
|
|
")",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 添加字段
|
|
func (t *sqlite3Test) TestCompileAdd() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.String("name", 50).Comment("用户名")
|
|
bp.SmallInteger("age").Default("18").Comment("年龄")
|
|
bp.Enum("sex", []string{"0", "1"}).Default("0").Comment("性别")
|
|
|
|
sql := t.schema.CompileAdd(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE `users` ADD COLUMN `name` varchar(50) NOT NULL",
|
|
"ALTER TABLE `users` ADD COLUMN `age` smallint(4) NOT NULL DEFAULT '18'",
|
|
"ALTER TABLE `users` ADD COLUMN `sex` varchar check('sex' in('0', '1')) NOT NULL DEFAULT '0'",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 修改字段
|
|
func (t *sqlite3Test) 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(len([]string{
|
|
"ALTER TABLE `users` ADD COLUMN `name_1742738970482105700` varchar(50) NOT NULL",
|
|
"UPDATE `users` SET `name_1742738970482105700`=`name`",
|
|
"ALTER TABLE `users` DROP COLUMN `name`",
|
|
"ALTER TABLE `users` RENAME COLUMN `name_1742738970482105700` TO `username`",
|
|
"ALTER TABLE `users` ADD COLUMN `age_1742738970482151800` smallint(4) NOT NULL DEFAULT '19'",
|
|
"UPDATE `users` SET `age_1742738970482151800`=`age`",
|
|
"ALTER TABLE `users` DROP COLUMN `age`",
|
|
"ALTER TABLE `users` RENAME COLUMN `age_1742738970482151800` TO `age`",
|
|
}), len(sql))
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 删除字段
|
|
func (t *sqlite3Test) TestCompileDropColumn() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.DropColumn("age", "sex")
|
|
|
|
sql := t.schema.CompileDropColumn(bp)
|
|
|
|
t.Equal([]string{
|
|
"ALTER TABLE `users` DROP COLUMN `age`",
|
|
"ALTER TABLE `users` DROP COLUMN `sex`",
|
|
}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|
|
|
|
// 删除表
|
|
func (t *sqlite3Test) 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 *sqlite3Test) 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 *sqlite3Test) TestCompileRename() {
|
|
bp := schema.NewBlueprint("users")
|
|
|
|
bp.Rename("user")
|
|
|
|
sql := t.schema.CompileRename(bp)
|
|
|
|
t.Equal([]string{"RENAME TABLE `users` TO `user`"}, sql)
|
|
|
|
t.T().Log(sql)
|
|
}
|