forked from slicebit/qb
/
mapper_test.go
249 lines (200 loc) · 8.52 KB
/
mapper_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package qb
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestMapper(t *testing.T) {
type UnknownType struct{}
type User struct {
ID string `qb:"constraints:primary_key"`
SecondaryID string `qb:"constraints:primary_key"`
FacebookID int64 `qb:"constraints:ref(facebook.id)"`
FacebookProfile string `qb:"constraints:ref(facebook.profile_id)"`
ProfileID int64 `qb:"constraints:ref(profile.id)"`
ProfileName string `qb:"constraints:ref(profile.name)"`
Email string `qb:"type:varchar(255); constraints:unique,notnull"`
FullName string `qb:"constraints:notnull,default"`
Password string `qb:"type:text"`
UserType string `qb:"constraints:default(guest)"`
Ignored bool `qb:"-"`
Premium bool
CreatedAt time.Time `qb:"constraints:notnull"`
DeletedAt *time.Time `qb:"constraints:null"`
Level int
Money float32
Score float64
Unknown UnknownType `qb:"index"`
CompositeIndex `qb:"index:full_name, password"`
}
dialect := NewDialect("mysql")
mapper := Mapper(dialect)
userTable, err := mapper.ToTable(User{})
assert.Nil(t, err)
ddl := userTable.Create(dialect)
fmt.Println(ddl, "\n")
assert.Contains(t, ddl, "CREATE TABLE user (")
assert.Contains(t, ddl, "id VARCHAR(255)")
assert.Contains(t, ddl, "secondary_id VARCHAR(255)")
assert.Contains(t, ddl, "facebook_id BIGINT")
assert.Contains(t, ddl, "facebook_profile VARCHAR(255)")
assert.Contains(t, ddl, "profile_id BIGINT")
assert.Contains(t, ddl, "profile_name VARCHAR(255)")
assert.Contains(t, ddl, "email VARCHAR(255) UNIQUE NOT NULL")
assert.Contains(t, ddl, "full_name VARCHAR(255) NOT NULL DEFAULT ''")
assert.Contains(t, ddl, "password TEXT")
assert.Contains(t, ddl, "user_type VARCHAR(255) DEFAULT 'guest'")
assert.Contains(t, ddl, "premium BOOLEAN")
assert.Contains(t, ddl, "created_at TIMESTAMP NOT NULL")
assert.Contains(t, ddl, "deleted_at TIMESTAMP NULL")
assert.Contains(t, ddl, "level INT")
assert.Contains(t, ddl, "money FLOAT")
assert.Contains(t, ddl, "score FLOAT")
assert.Contains(t, ddl, "unknown VARCHAR(255)")
assert.Contains(t, ddl, "PRIMARY KEY(id, secondary_id)")
assert.Contains(t, ddl, "FOREIGN KEY(facebook_id, facebook_profile) REFERENCES facebook(id, profile_id)")
assert.Contains(t, ddl, "FOREIGN KEY(profile_id, profile_name) REFERENCES profile(id, name)")
assert.Contains(t, ddl, ")")
assert.Contains(t, ddl, "CREATE INDEX i_unknown ON user(unknown)")
assert.Contains(t, ddl, "CREATE INDEX i_full_name_password ON user(full_name, password);")
}
func TestMapperSqliteAutoIncrement(t *testing.T) {
type User struct {
ID int64 `qb:"constraints:auto_increment"`
}
dialect := NewDialect("sqlite3")
mapper := Mapper(dialect)
table, err := mapper.ToTable(User{})
assert.Nil(t, err)
ddl := table.Create(dialect)
assert.Contains(t, ddl, "CREATE TABLE user (")
assert.Contains(t, ddl, "id BIGINT")
assert.Contains(t, ddl, ")")
fmt.Println(ddl, "\n")
}
func TestMapperWithDBTag(t *testing.T) {
type User struct {
ID string `db:"_id" qb:"type:varchar(36); constraints:primary_key"`
Email string `qb:"constraints:unique, notnull"`
}
dialect := NewDialect("mysql")
mapper := Mapper(dialect)
table, err := mapper.ToTable(User{})
assert.Nil(t, err)
ddl := table.Create(dialect)
assert.Contains(t, ddl, "CREATE TABLE user (")
assert.Contains(t, ddl, "_id VARCHAR(36)")
assert.Contains(t, ddl, "email VARCHAR(255) UNIQUE NOT NULL")
assert.Contains(t, ddl, "PRIMARY KEY(_id)")
m := mapper.ToMap(User{ID: "cba0667d-8c76-4999-9a55-84ffe572fb23", Email: "aras@slicebit.com"}, false)
assert.Equal(t, m, map[string]interface{}{
"_id": "cba0667d-8c76-4999-9a55-84ffe572fb23",
"email": "aras@slicebit.com",
})
fmt.Println(ddl, "\n")
}
func TestMapperPostgresAutoIncrement(t *testing.T) {
type User struct {
ID int64 `qb:"constraints:auto_increment"`
}
dialect := NewDialect("postgres")
mapper := Mapper(dialect)
table, err := mapper.ToTable(User{})
assert.Nil(t, err)
ddl := table.Create(dialect)
assert.NotContains(t, ddl, "AUTOINCREMENT")
assert.NotContains(t, ddl, "AUTO INCREMENT")
}
func TestMapperError(t *testing.T) {
type UserErr struct {
ID string `qb:"type:varchar(255);tag_should_raise_err:val;"`
Email string `qb:"wrongtag:"`
}
dialect := NewDialect("postgres")
mapper := Mapper(dialect)
userErrTable, err := mapper.ToTable(UserErr{})
assert.NotNil(t, err)
assert.Zero(t, userErrTable)
}
func TestMapperInvalidConstraint(t *testing.T) {
type InvalidConstraint struct {
ID string `qb:"constraints:invalid_constraint"`
}
dialect := NewDialect("mysql")
mapper := Mapper(dialect)
invalidConstraintTable, err := mapper.ToTable(InvalidConstraint{})
assert.Zero(t, invalidConstraintTable)
assert.NotNil(t, err)
}
func TestNonZeroStruct(t *testing.T) {
type User struct {
ID int
}
mapper := Mapper(NewDialect("mysql"))
m := mapper.ToMap(User{5}, false)
assert.Equal(t, m, map[string]interface{}{"id": 5})
}
func TestMapperUtilFuncs(t *testing.T) {
type UserErr struct {
ID string `qb:"type:varchar(255);tag_should_raise_err:val;"`
Email string `qb:"wrongtag:"`
}
mapper := Mapper(NewDialect("mysql"))
kv := mapper.ToMap(UserErr{}, false)
assert.Equal(t, kv, map[string]interface{}{})
}
func TestMapperTypes(t *testing.T) {
sqliteMapper := Mapper(NewDialect("sqlite3"))
postgresMapper := Mapper(NewDialect("postgres"))
mysqlMapper := Mapper(NewDialect("mysql"))
assert.Equal(t, sqliteMapper.ToType("string", ""), Varchar().Size(255))
assert.Equal(t, postgresMapper.ToType("string", ""), Varchar().Size(255))
assert.Equal(t, mysqlMapper.ToType("string", ""), Varchar().Size(255))
assert.Equal(t, sqliteMapper.ToType("int", ""), Int())
assert.Equal(t, postgresMapper.ToType("int", ""), Int())
assert.Equal(t, mysqlMapper.ToType("int", ""), Int())
assert.Equal(t, sqliteMapper.ToType("int8", ""), SmallInt())
assert.Equal(t, postgresMapper.ToType("int8", ""), SmallInt())
assert.Equal(t, mysqlMapper.ToType("int8", ""), SmallInt())
assert.Equal(t, sqliteMapper.ToType("int16", ""), SmallInt())
assert.Equal(t, postgresMapper.ToType("int16", ""), SmallInt())
assert.Equal(t, mysqlMapper.ToType("int16", ""), SmallInt())
assert.Equal(t, sqliteMapper.ToType("int32", ""), Int())
assert.Equal(t, postgresMapper.ToType("int32", ""), Int())
assert.Equal(t, mysqlMapper.ToType("int32", ""), Int())
assert.Equal(t, sqliteMapper.ToType("int64", ""), BigInt())
assert.Equal(t, postgresMapper.ToType("int64", ""), BigInt())
assert.Equal(t, mysqlMapper.ToType("int64", ""), BigInt())
assert.Equal(t, sqliteMapper.ToType("uint", ""), BigInt())
assert.Equal(t, postgresMapper.ToType("uint", ""), BigInt())
assert.Equal(t, mysqlMapper.ToType("uint", ""), Type("INT UNSIGNED"))
assert.Equal(t, sqliteMapper.ToType("uint8", ""), SmallInt())
assert.Equal(t, postgresMapper.ToType("uint8", ""), SmallInt())
assert.Equal(t, mysqlMapper.ToType("uint8", ""), Type("TINYINT UNSIGNED"))
assert.Equal(t, sqliteMapper.ToType("uint16", ""), Int())
assert.Equal(t, postgresMapper.ToType("uint16", ""), Int())
assert.Equal(t, mysqlMapper.ToType("uint16", ""), Type("SMALLINT UNSIGNED"))
assert.Equal(t, sqliteMapper.ToType("uint32", ""), BigInt())
assert.Equal(t, postgresMapper.ToType("uint32", ""), BigInt())
assert.Equal(t, mysqlMapper.ToType("uint32", ""), Type("INT UNSIGNED"))
assert.Equal(t, sqliteMapper.ToType("uint64", ""), BigInt())
assert.Equal(t, postgresMapper.ToType("uint64", ""), BigInt())
assert.Equal(t, mysqlMapper.ToType("uint64", ""), Type("BIGINT UNSIGNED"))
assert.Equal(t, sqliteMapper.ToType("float32", ""), Float())
assert.Equal(t, postgresMapper.ToType("float32", ""), Float())
assert.Equal(t, mysqlMapper.ToType("float32", ""), Float())
assert.Equal(t, sqliteMapper.ToType("float64", ""), Float())
assert.Equal(t, postgresMapper.ToType("float64", ""), Float())
assert.Equal(t, mysqlMapper.ToType("float64", ""), Float())
assert.Equal(t, sqliteMapper.ToType("bool", ""), Boolean())
assert.Equal(t, postgresMapper.ToType("bool", ""), Boolean())
assert.Equal(t, mysqlMapper.ToType("bool", ""), Boolean())
assert.Equal(t, sqliteMapper.ToType("time.Time", ""), Timestamp())
assert.Equal(t, postgresMapper.ToType("time.Time", ""), Timestamp())
assert.Equal(t, mysqlMapper.ToType("time.Time", ""), Timestamp())
assert.Equal(t, sqliteMapper.ToType("*time.Time", ""), Timestamp())
assert.Equal(t, postgresMapper.ToType("*time.Time", ""), Timestamp())
assert.Equal(t, mysqlMapper.ToType("*time.Time", ""), Timestamp())
}