forked from brianc/node-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert-tests.js
More file actions
136 lines (120 loc) · 4.5 KB
/
insert-tests.js
File metadata and controls
136 lines (120 loc) · 4.5 KB
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
'use strict';
var Harness = require('./support');
var post = Harness.definePostTable();
Harness.test({
query : post.insert(post.content.value('test'), post.userId.value(1)),
pg : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
sqlite: 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
mysql : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?)',
params: ['test', 1]
});
Harness.test({
query : post.insert(post.content.value('whoah')),
pg : 'INSERT INTO "post" ("content") VALUES ($1)',
sqlite: 'INSERT INTO "post" ("content") VALUES ($1)',
mysql : 'INSERT INTO `post` (`content`) VALUES (?)',
params: ['whoah']
});
Harness.test({
query : post.insert({content: 'test', userId: 2}),
pg : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
sqlite: 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2)',
mysql : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?)',
params: ['test', 2]
});
// allow bulk insert
Harness.test({
query : post.insert([{content: 'whoah'}, {content: 'hey'}]),
pg : 'INSERT INTO "post" ("content") VALUES ($1), ($2)',
sqlite: 'INSERT INTO "post" ("content") VALUES ($1), ($2)',
mysql : 'INSERT INTO `post` (`content`) VALUES (?), (?)',
params: ['whoah', 'hey']
});
Harness.test({
query : post.insert([{content: 'whoah', userId: 1}, {content: 'hey', userId: 2}]),
pg : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
sqlite: 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
mysql : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?), (?, ?)',
params: ['whoah', 1, 'hey', 2]
});
// consistent order
Harness.test({
query : post.insert([{content: 'whoah', userId: 1}, {userId: 2, content: 'hey' }]),
pg : 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
sqlite: 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
mysql : 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?), (?, ?)',
params: ['whoah', 1, 'hey', 2]
});
// handle missing columns
Harness.test({
query : post.insert([{content: 'whoah', userId: 1}, {content: 'hey'}]),
pg : {
text: 'INSERT INTO "post" ("content", "userId") VALUES ($1, $2), ($3, $4)',
params: ['whoah', 1, 'hey', 'DEFAULT']
},
sqlite: {
text: 'Sqlite requires the same number of columns in each insert row',
throws: true
},
mysql : {
text: 'INSERT INTO `post` (`content`, `userId`) VALUES (?, ?), (?, DEFAULT)',
params: ['whoah', 1, 'hey']
}
});
Harness.test({
query : post.insert([{userId: 1}, {content: 'hey', userId: 2}]),
pg : {
text: 'INSERT INTO "post" ("userId", "content") VALUES ($1, $2), ($3, $4)',
params: [1, 'DEFAULT', 2, 'hey']
},
sqlite: {
text: 'Sqlite requires the same number of columns in each insert row',
throws: true
},
mysql : {
text: 'INSERT INTO `post` (`userId`, `content`) VALUES (?, DEFAULT), (?, ?)',
params: [1, 2, 'hey']
}
});
Harness.test({
query : post.insert({}),
pg : 'INSERT INTO "post" DEFAULT VALUES',
sqlite: 'INSERT INTO "post" DEFAULT VALUES',
mysql : 'INSERT INTO `post` () VALUES ()',
params: []
});
Harness.test({
query : post.insert({}).returning('*'),
pg : 'INSERT INTO "post" DEFAULT VALUES RETURNING *',
sqlite: 'INSERT INTO "post" DEFAULT VALUES RETURNING *',
mysql : 'INSERT INTO `post` () VALUES () RETURNING *',
params: []
});
Harness.test({
query : post.insert({}).returning(post.star()),
pg : 'INSERT INTO "post" DEFAULT VALUES RETURNING *',
sqlite: 'INSERT INTO "post" DEFAULT VALUES RETURNING *',
mysql : 'INSERT INTO `post` () VALUES () RETURNING *',
params: []
});
Harness.test({
query : post.insert({}).returning(post.id),
pg : 'INSERT INTO "post" DEFAULT VALUES RETURNING "id"',
sqlite: 'INSERT INTO "post" DEFAULT VALUES RETURNING "id"',
mysql : 'INSERT INTO `post` () VALUES () RETURNING `id`',
params: []
});
Harness.test({
query : post.insert({}).returning(post.id, post.content),
pg : 'INSERT INTO "post" DEFAULT VALUES RETURNING "id", "content"',
sqlite: 'INSERT INTO "post" DEFAULT VALUES RETURNING "id", "content"',
mysql : 'INSERT INTO `post` () VALUES () RETURNING `id`, `content`',
params: []
});
Harness.test({
query : post.insert({}).returning([post.id, post.content]),
pg : 'INSERT INTO "post" DEFAULT VALUES RETURNING "id", "content"',
sqlite: 'INSERT INTO "post" DEFAULT VALUES RETURNING "id", "content"',
mysql : 'INSERT INTO `post` () VALUES () RETURNING `id`, `content`',
params: []
});