github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/internal/sql/adapter/sqlite/dialect_test.go (about)

     1  package sqlite
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/octohelm/storage/internal/testutil"
     8  	"github.com/octohelm/storage/pkg/sqlbuilder"
     9  )
    10  
    11  func TestSqliteDialect(t *testing.T) {
    12  	c := &dialect{}
    13  
    14  	table := sqlbuilder.T("t",
    15  		sqlbuilder.Col("f_id", sqlbuilder.ColTypeOf(uint64(0), ",autoincrement")),
    16  		sqlbuilder.Col("f_old_name", sqlbuilder.ColTypeOf("", ",deprecated=f_name")),
    17  		sqlbuilder.Col("f_name", sqlbuilder.ColTypeOf("", ",size=128,default=''")),
    18  		sqlbuilder.Col("F_created_at", sqlbuilder.ColTypeOf(int64(0), ",default='0'")),
    19  		sqlbuilder.Col("F_updated_at", sqlbuilder.ColTypeOf(int64(0), ",default='0'")),
    20  		sqlbuilder.PrimaryKey(sqlbuilder.Cols("F_id")),
    21  		sqlbuilder.UniqueIndex("I_name", sqlbuilder.Cols("F_id", "F_name"), sqlbuilder.IndexUsing("BTREE")),
    22  		sqlbuilder.Index("I_created_at", sqlbuilder.Cols("F_created_at"), sqlbuilder.IndexUsing("BTREE")),
    23  	)
    24  
    25  	cases := map[string]struct {
    26  		expr   sqlbuilder.SqlExpr
    27  		expect sqlbuilder.SqlExpr
    28  	}{
    29  		"CreateTableIsNotExists": {
    30  			c.CreateTableIsNotExists(table)[0],
    31  			sqlbuilder.Expr( /* language=sqlite */ `CREATE TABLE IF NOT EXISTS t (
    32  	f_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    33  	f_name TEXT NOT NULL DEFAULT '',
    34  	f_created_at BIGINT NOT NULL DEFAULT '0',
    35  	f_updated_at BIGINT NOT NULL DEFAULT '0'
    36  );`),
    37  		},
    38  		"DropTable": {
    39  			c.DropTable(table),
    40  			sqlbuilder.Expr( /* language=sqlite */ "DROP TABLE IF EXISTS t;"),
    41  		},
    42  		"TruncateTable": {
    43  			c.TruncateTable(table),
    44  			sqlbuilder.Expr( /* language=sqlite */ "TRUNCATE TABLE t;"),
    45  		},
    46  		"AddColumn": {
    47  			c.AddColumn(table.F("f_name")),
    48  			sqlbuilder.Expr( /* language=sqlite */ "ALTER TABLE t ADD COLUMN f_name TEXT NOT NULL DEFAULT '';"),
    49  		},
    50  		"DropColumn": {
    51  			c.DropColumn(table.F("f_name")),
    52  			sqlbuilder.Expr( /* language=sqlite */ "ALTER TABLE t DROP COLUMN f_name;"),
    53  		},
    54  		"AddIndex": {
    55  			c.AddIndex(table.K("I_name")),
    56  			sqlbuilder.Expr( /* language=sqlite */ "CREATE UNIQUE INDEX t_i_name ON t (f_id,f_name);"),
    57  		},
    58  		"AddPrimaryKey": {
    59  			c.AddIndex(table.K("PRIMARY")),
    60  			sqlbuilder.Expr( /* language=sqlite */ "ALTER TABLE t ADD PRIMARY KEY (f_id);"),
    61  		},
    62  		"DropIndex": {
    63  			c.DropIndex(table.K("I_name")),
    64  			sqlbuilder.Expr( /* language=sqlite */ "DROP INDEX IF EXISTS t_i_name;"),
    65  		},
    66  	}
    67  
    68  	for name, c := range cases {
    69  		t.Run(name, func(t *testing.T) {
    70  			testutil.ShouldBeExpr(t, c.expr, c.expect.Ex(context.Background()).Query())
    71  		})
    72  	}
    73  }