-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathquery_spec.moon
94 lines (73 loc) · 2.45 KB
/
query_spec.moon
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
db = require "lapis.db.mysql"
schema = require "lapis.db.mysql.schema"
import configure_mysql, bind_query_log from require "spec_mysql.helpers"
import drop_tables from require "lapis.spec.db"
import create_table, drop_table, types from schema
describe "model", ->
configure_mysql!
local query_log
bind_query_log -> query_log
before_each ->
query_log = {}
it "runs query", ->
assert.truthy db.query [[
select * from information_schema.tables
where table_schema = "lapis_test"
]]
it "runs query with interpolation", ->
assert.truthy db.query [[
select * from information_schema.tables
where table_schema = ?
]], "lapis_test"
it "creates a table", ->
drop_table "hello_worlds"
create_table "hello_worlds", {
{"id", types.id}
{"name", types.varchar}
}
assert.same 1, #db.query [[
select * from information_schema.tables
where table_schema = "lapis_test" and table_name = "hello_worlds"
]]
db.insert "hello_worlds", {
name: "well well well"
}
res = db.insert "hello_worlds", {
name: "another one"
}
assert.same {
affected_rows: 1
last_auto_id: 2
}, res
assert.same {
[[DROP TABLE IF EXISTS `hello_worlds`;]]
[[CREATE TABLE `hello_worlds` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255) NOT NULL
) CHARSET=UTF8;]]
[[ select * from information_schema.tables
where table_schema = "lapis_test" and table_name = "hello_worlds"
]]
[[INSERT INTO `hello_worlds` (`name`) VALUES ('well well well')]]
[[INSERT INTO `hello_worlds` (`name`) VALUES ('another one')]]
}, query_log
describe "with table", ->
before_each ->
drop_table "hello_worlds"
create_table "hello_worlds", {
{"id", types.id}
{"name", types.varchar}
}
query_log = {}
it "creates index and removes index", ->
schema.create_index "hello_worlds", "id", "name", unique: true
schema.drop_index "hello_worlds", "id", "name", unique: true
assert.same {
[[CREATE UNIQUE INDEX `hello_worlds_id_name_idx` ON `hello_worlds` (`id`, `name`);]]
[[DROP INDEX `hello_worlds_id_name_idx` on `hello_worlds`;]]
}, query_log
it "adds column", ->
schema.add_column "hello_worlds", "counter", schema.types.integer 123
assert.same {
[[ALTER TABLE `hello_worlds` ADD COLUMN `counter` INT(123) NOT NULL]]
}, query_log