A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

147 lines
7.9 KiB

  1. package db
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestDialect_Column(t *testing.T) {
  7. c1 := DialectSQLite.Column("foo", ColumnTypeBool, UnsetSize)
  8. assert.Equal(t, DialectSQLite, c1.Dialect)
  9. c2 := DialectMySQL.Column("foo", ColumnTypeBool, UnsetSize)
  10. assert.Equal(t, DialectMySQL, c2.Dialect)
  11. }
  12. func TestColumnType_Format(t *testing.T) {
  13. type args struct {
  14. dialect DialectType
  15. size OptionalInt
  16. }
  17. tests := []struct {
  18. name string
  19. d ColumnType
  20. args args
  21. want string
  22. wantErr bool
  23. }{
  24. {"Sqlite bool", ColumnTypeBool, args{dialect: DialectSQLite}, "INTEGER", false},
  25. {"Sqlite small int", ColumnTypeSmallInt, args{dialect: DialectSQLite}, "INTEGER", false},
  26. {"Sqlite int", ColumnTypeInteger, args{dialect: DialectSQLite}, "INTEGER", false},
  27. {"Sqlite char", ColumnTypeChar, args{dialect: DialectSQLite}, "TEXT", false},
  28. {"Sqlite varchar", ColumnTypeVarChar, args{dialect: DialectSQLite}, "TEXT", false},
  29. {"Sqlite text", ColumnTypeText, args{dialect: DialectSQLite}, "TEXT", false},
  30. {"Sqlite datetime", ColumnTypeDateTime, args{dialect: DialectSQLite}, "DATETIME", false},
  31. {"MySQL bool", ColumnTypeBool, args{dialect: DialectMySQL}, "TINYINT(1)", false},
  32. {"MySQL small int", ColumnTypeSmallInt, args{dialect: DialectMySQL}, "SMALLINT", false},
  33. {"MySQL small int with param", ColumnTypeSmallInt, args{dialect: DialectMySQL, size: OptionalInt{true, 3}}, "SMALLINT(3)", false},
  34. {"MySQL int", ColumnTypeInteger, args{dialect: DialectMySQL}, "INT", false},
  35. {"MySQL int with param", ColumnTypeInteger, args{dialect: DialectMySQL, size: OptionalInt{true, 11}}, "INT(11)", false},
  36. {"MySQL char", ColumnTypeChar, args{dialect: DialectMySQL}, "CHAR", false},
  37. {"MySQL char with param", ColumnTypeChar, args{dialect: DialectMySQL, size: OptionalInt{true, 4}}, "CHAR(4)", false},
  38. {"MySQL varchar", ColumnTypeVarChar, args{dialect: DialectMySQL}, "VARCHAR", false},
  39. {"MySQL varchar with param", ColumnTypeVarChar, args{dialect: DialectMySQL, size: OptionalInt{true, 25}}, "VARCHAR(25)", false},
  40. {"MySQL text", ColumnTypeText, args{dialect: DialectMySQL}, "TEXT", false},
  41. {"MySQL datetime", ColumnTypeDateTime, args{dialect: DialectMySQL}, "DATETIME", false},
  42. {"invalid column type", 10000, args{dialect: DialectMySQL}, "", true},
  43. {"invalid dialect", ColumnTypeBool, args{dialect: 10000}, "", true},
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. got, err := tt.d.Format(tt.args.dialect, tt.args.size)
  48. if (err != nil) != tt.wantErr {
  49. t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr)
  50. return
  51. }
  52. if got != tt.want {
  53. t.Errorf("Format() got = %v, want %v", got, tt.want)
  54. }
  55. })
  56. }
  57. }
  58. func TestColumn_Build(t *testing.T) {
  59. type fields struct {
  60. Dialect DialectType
  61. Name string
  62. Nullable bool
  63. Default OptionalString
  64. Type ColumnType
  65. Size OptionalInt
  66. PrimaryKey bool
  67. }
  68. tests := []struct {
  69. name string
  70. fields fields
  71. want string
  72. wantErr bool
  73. }{
  74. {"Sqlite bool", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo INTEGER NOT NULL", false},
  75. {"Sqlite bool nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo INTEGER", false},
  76. {"Sqlite small int", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeSmallInt, UnsetSize, true}, "foo INTEGER NOT NULL PRIMARY KEY", false},
  77. {"Sqlite small int nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeSmallInt, UnsetSize, false}, "foo INTEGER", false},
  78. {"Sqlite int", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INTEGER NOT NULL", false},
  79. {"Sqlite int nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INTEGER", false},
  80. {"Sqlite char", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo TEXT NOT NULL", false},
  81. {"Sqlite char nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo TEXT", false},
  82. {"Sqlite varchar", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo TEXT NOT NULL", false},
  83. {"Sqlite varchar nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo TEXT", false},
  84. {"Sqlite text", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT NOT NULL", false},
  85. {"Sqlite text nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT", false},
  86. {"Sqlite datetime", fields{DialectSQLite, "foo", false, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME NOT NULL", false},
  87. {"Sqlite datetime nullable", fields{DialectSQLite, "foo", true, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME", false},
  88. {"MySQL bool", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo TINYINT(1) NOT NULL", false},
  89. {"MySQL bool nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeBool, UnsetSize, false}, "foo TINYINT(1)", false},
  90. {"MySQL small int", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeSmallInt, UnsetSize, true}, "foo SMALLINT NOT NULL PRIMARY KEY", false},
  91. {"MySQL small int nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeSmallInt, UnsetSize, false}, "foo SMALLINT", false},
  92. {"MySQL int", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INT NOT NULL", false},
  93. {"MySQL int nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeInteger, UnsetSize, false}, "foo INT", false},
  94. {"MySQL char", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo CHAR NOT NULL", false},
  95. {"MySQL char nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeChar, UnsetSize, false}, "foo CHAR", false},
  96. {"MySQL varchar", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo VARCHAR NOT NULL", false},
  97. {"MySQL varchar nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeVarChar, UnsetSize, false}, "foo VARCHAR", false},
  98. {"MySQL text", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT NOT NULL", false},
  99. {"MySQL text nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeText, UnsetSize, false}, "foo TEXT", false},
  100. {"MySQL datetime", fields{DialectMySQL, "foo", false, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME NOT NULL", false},
  101. {"MySQL datetime nullable", fields{DialectMySQL, "foo", true, UnsetDefault, ColumnTypeDateTime, UnsetSize, false}, "foo DATETIME", false},
  102. }
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. c := &Column{
  106. Dialect: tt.fields.Dialect,
  107. Name: tt.fields.Name,
  108. Nullable: tt.fields.Nullable,
  109. Default: tt.fields.Default,
  110. Type: tt.fields.Type,
  111. Size: tt.fields.Size,
  112. PrimaryKey: tt.fields.PrimaryKey,
  113. }
  114. if got, err := c.String(); got != tt.want {
  115. if (err != nil) != tt.wantErr {
  116. t.Errorf("String() error = %v, wantErr %v", err, tt.wantErr)
  117. return
  118. }
  119. if got != tt.want {
  120. t.Errorf("String() got = %v, want %v", got, tt.want)
  121. }
  122. }
  123. })
  124. }
  125. }
  126. func TestCreateTableSqlBuilder_ToSQL(t *testing.T) {
  127. sql, err := DialectMySQL.
  128. Table("foo").
  129. SetIfNotExists(true).
  130. Column(DialectMySQL.Column("bar", ColumnTypeInteger, UnsetSize).SetPrimaryKey(true)).
  131. Column(DialectMySQL.Column("baz", ColumnTypeText, UnsetSize)).
  132. Column(DialectMySQL.Column("qux", ColumnTypeDateTime, UnsetSize).SetDefault("NOW()")).
  133. UniqueConstraint("bar").
  134. UniqueConstraint("bar", "baz").
  135. ToSQL()
  136. assert.NoError(t, err)
  137. assert.Equal(t, "CREATE TABLE IF NOT EXISTS foo ( bar INT NOT NULL PRIMARY KEY, baz TEXT NOT NULL, qux DATETIME NOT NULL DEFAULT NOW(), UNIQUE(bar), UNIQUE(bar,baz) )", sql)
  138. }