-
Notifications
You must be signed in to change notification settings - Fork 26
/
column_test.go
137 lines (109 loc) · 4.14 KB
/
column_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package qb
import (
"testing"
"github.com/stretchr/testify/suite"
"github.com/stretchr/testify/assert"
)
type ColumnTestSuite struct {
suite.Suite
dialect Dialect
ctx *CompilerContext
}
func (suite *ColumnTestSuite) SetupTest() {
suite.dialect = NewDefaultDialect()
suite.ctx = NewCompilerContext(suite.dialect)
}
func (suite *ColumnTestSuite) TestColumnVarcharSpecificSize() {
col := Column("id", Varchar().Size(40))
assert.Equal(suite.T(), "id", col.Name)
assert.Equal(suite.T(), Varchar().Size(40), col.Type)
assert.Equal(suite.T(), "id VARCHAR(40)", col.String(suite.dialect))
}
func (suite *ColumnTestSuite) TestColumnVarcharUniqueNotNullDefault() {
col := Column("s", Varchar().Size(255)).Unique().NotNull().Default("hello")
assert.Equal(suite.T(), "s VARCHAR(255) UNIQUE NOT NULL DEFAULT 'hello'", col.String(suite.dialect))
}
func (suite *ColumnTestSuite) TestColumnFloatPrecision() {
col := Column("f", Type("FLOAT").Precision(2, 5)).Null()
assert.Equal(suite.T(), "f FLOAT(2, 5) NULL", col.String(suite.dialect))
}
func (suite *ColumnTestSuite) TestColumnIntInlinePrimaryKeyAutoIncrement() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
assert.Equal(suite.T(), "id INT PRIMARY KEY AUTO INCREMENT", col.String(suite.dialect))
assert.Equal(suite.T(), "c INT TEST", Column("c", Int()).Constraint("TEST").String(suite.dialect))
}
func (suite *ColumnTestSuite) TestColumnLike() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
like := col.Like("s%")
sql := like.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id LIKE ?", sql)
assert.Equal(suite.T(), []interface{}{"s%"}, binds)
}
func (suite *ColumnTestSuite) TestColumnNotIn() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
notIn := col.NotIn("id1", "id2")
sql := notIn.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id NOT IN (?, ?)", sql)
assert.Equal(suite.T(), []interface{}{"id1", "id2"}, binds)
}
func (suite *ColumnTestSuite) TestColumnIn() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
in := col.In("id1", "id2")
sql := in.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id IN (?, ?)", sql)
assert.Equal(suite.T(), []interface{}{"id1", "id2"}, binds)
}
func (suite *ColumnTestSuite) TestColumnNotEq() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
notEq := col.NotEq("id1")
sql := notEq.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id != ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func (suite *ColumnTestSuite) TestColumnEq() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
eq := col.Eq("id1")
sql := eq.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id = ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func (suite *ColumnTestSuite) TestColumnGt() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
gt := col.Gt("id1")
sql := gt.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id > ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func (suite *ColumnTestSuite) TestColumnLt() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
lt := col.Lt("id1")
sql := lt.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id < ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func (suite *ColumnTestSuite) TestcolumnGte() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
gte := col.Gte("id1")
sql := gte.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id >= ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func (suite *ColumnTestSuite) TestColumnLte() {
col := Column("id", Int()).PrimaryKey().AutoIncrement().inlinePrimaryKey()
lte := col.Lte("id1")
sql := lte.Accept(suite.ctx)
binds := suite.ctx.Binds
assert.Equal(suite.T(), "id <= ?", sql)
assert.Equal(suite.T(), []interface{}{"id1"}, binds)
}
func TestColumnTestSuite(t *testing.T) {
suite.Run(t, new(ColumnTestSuite))
}