-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_orm_test.v
227 lines (188 loc) · 4.92 KB
/
sqlite_orm_test.v
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
import orm
import sqlite
import time
struct TestCustomSqlType {
id int @[primary; sql: serial]
custom string @[sql_type: 'INTEGER']
custom1 string @[sql_type: 'TEXT']
custom2 string @[sql_type: 'REAL']
custom3 string @[sql_type: 'NUMERIC']
custom4 string
custom5 int
custom6 time.Time
}
struct TestDefaultAttribute {
id string @[primary; sql: serial]
name string
created_at ?string @[default: 'CURRENT_TIME']
created_at1 ?string @[default: 'CURRENT_DATE']
created_at2 ?string @[default: 'CURRENT_TIMESTAMP']
}
struct EntityToTest {
id int @[notnull; sql_type: 'INTEGER']
smth string @[notnull; sql_type: 'TEXT']
}
fn test_sqlite_orm() {
mut db := sqlite.connect(':memory:') or { panic(err) }
defer {
db.close() or { panic(err) }
}
db.create('Test', [
orm.TableField{
name: 'id'
typ: typeof[int]().idx
attrs: [
VAttribute{
name: 'primary'
},
VAttribute{
name: 'sql'
has_arg: true
kind: .plain
arg: 'serial'
},
]
},
orm.TableField{
name: 'name'
typ: typeof[string]().idx
attrs: []
},
orm.TableField{
name: 'age'
typ: typeof[i64]().idx
},
]) or { panic(err) }
db.insert('Test', orm.QueryData{
fields: ['name', 'age']
data: [orm.string_to_primitive('Louis'), orm.i64_to_primitive(100)]
}) or { panic(err) }
res := db.@select(orm.SelectConfig{
table: 'Test'
has_where: true
fields: ['id', 'name', 'age']
types: [typeof[int]().idx, typeof[string]().idx, typeof[i64]().idx]
}, orm.QueryData{}, orm.QueryData{
fields: ['name', 'age']
data: [orm.Primitive('Louis'), i64(100)]
types: [typeof[string]().idx, typeof[i64]().idx]
is_and: [true, true]
kinds: [.eq, .eq]
}) or { panic(err) }
id := res[0][0]
name := res[0][1]
age := res[0][2]
assert id is int
if id is int {
assert id == 1
}
assert name is string
if name is string {
assert name == 'Louis'
}
assert age is i64
if age is i64 {
assert age == 100
}
/** test orm sql type
* - verify if all type create by attribute sql_type has created
*/
sql db {
create table TestCustomSqlType
}!
mut result_custom_sql := db.exec('
pragma table_info(TestCustomSqlType);
')!
mut table_info_types_results := []string{}
information_schema_custom_sql := ['INTEGER', 'INTEGER', 'TEXT', 'REAL', 'NUMERIC', 'TEXT',
'INTEGER', 'INTEGER']
for data_type in result_custom_sql {
table_info_types_results << data_type.vals[2]
}
assert table_info_types_results == information_schema_custom_sql
sql db {
drop table TestCustomSqlType
}!
/** test default attribute
*/
sql db {
create table TestDefaultAttribute
}!
mut result_default_sql := db.exec('
pragma table_info(TestDefaultAttribute);
')!
mut information_schema_data_types_results := []string{}
information_schema_default_sql := ['', '', 'CURRENT_TIME', 'CURRENT_DATE', 'CURRENT_TIMESTAMP']
for data_type in result_default_sql {
information_schema_data_types_results << data_type.vals[4]
}
assert information_schema_data_types_results == information_schema_default_sql
test_default_attribute := TestDefaultAttribute{
name: 'Hitalo'
}
sql db {
insert test_default_attribute into TestDefaultAttribute
}!
test_default_attributes := sql db {
select from TestDefaultAttribute limit 1
}!
result_test_default_attribute := test_default_attributes.first()
assert result_test_default_attribute.name == 'Hitalo'
assert test_default_attribute.created_at or { '' } == ''
assert test_default_attribute.created_at1 or { '' } == ''
assert test_default_attribute.created_at2 or { '' } == ''
assert result_test_default_attribute.created_at or { '' }.len == 8 // HH:MM:SS
assert result_test_default_attribute.created_at1 or { '' }.len == 10 // YYYY-MM-DD
assert result_test_default_attribute.created_at2 or { '' }.len == 19 // YYYY-MM-DD HH:MM:SS
sql db {
drop table TestDefaultAttribute
}!
}
fn test_get_affected_rows_count() {
mut db := sqlite.connect(':memory:') or { panic(err) }
defer {
db.close() or { panic(err) }
}
db.exec('create table EntityToTest(
id integer not null constraint tbl_pk primary key,
smth integer
);')!
fst := EntityToTest{
id: 1
smth: '1'
}
sql db {
insert fst into EntityToTest
} or { panic('first insert failed') }
assert db.get_affected_rows_count() == 1
snd := EntityToTest{
id: 1
smth: '2'
}
mut sndfailed := false
sql db {
insert snd into EntityToTest
} or { sndfailed = true }
assert db.get_affected_rows_count() == 0
assert sndfailed
all := sql db {
select from EntityToTest
}!
assert 1 == all.len
sql db {
update EntityToTest set smth = '2' where id == 1
}!
assert db.get_affected_rows_count() == 1
sql db {
update EntityToTest set smth = '2' where id == 2
}!
assert db.get_affected_rows_count() == 0
sql db {
delete from EntityToTest where id == 2
}!
assert db.get_affected_rows_count() == 0
sql db {
delete from EntityToTest where id == 1
}!
assert db.get_affected_rows_count() == 1
}