-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
165 lines (115 loc) · 6.71 KB
/
Copy pathtest.js
File metadata and controls
165 lines (115 loc) · 6.71 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
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
const tape = require('tape')
const SQL = require('./')
tape('createDatabase()', async function (t) {
const builder = new SQL()
const output = builder.createDatabase('myapp')
t.deepEqual(output, ['CREATE DATABASE IF NOT EXISTS `myapp`'])
const output2 = builder.createDatabase('myapp', { charset: 'utf8' })
t.deepEqual(output2, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8'])
const output3 = builder.createDatabase('myapp', { collate: 'utf8mb4_general_ci' })
t.deepEqual(output3, ['CREATE DATABASE IF NOT EXISTS `myapp` COLLATE utf8mb4_general_ci'])
const output4 = builder.createDatabase('myapp', { charset: 'utf8', collate: 'utf8mb4_general_ci' })
t.deepEqual(output4, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8 COLLATE utf8mb4_general_ci'])
})
tape('createDatabase() with options', async function (t) {
const builder = new SQL({ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci' })
const output = builder.createDatabase('myapp')
t.deepEqual(output, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'])
const output2 = builder.createDatabase('myapp', { charset: 'utf8' })
t.deepEqual(output2, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8 COLLATE utf8mb4_unicode_ci'])
const output3 = builder.createDatabase('myapp', { collate: 'utf8mb4_general_ci' })
t.deepEqual(output3, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci'])
const output4 = builder.createDatabase('myapp', { charset: 'utf8', collate: 'utf8mb4_general_ci' })
t.deepEqual(output4, ['CREATE DATABASE IF NOT EXISTS `myapp` DEFAULT CHARACTER SET utf8 COLLATE utf8mb4_general_ci'])
})
tape('dropDatabase()', async function (t) {
const builder = new SQL()
const [sql] = builder.dropDatabase('myapp')
t.deepEqual(sql, 'DROP DATABASE IF EXISTS `myapp`')
})
/*
tape('createTable()', async function (t) {
const builder = new SQL()
})
tape('dropTable()', async function (t) {
const builder = new SQL()
const [sql] = builder.dropTable('users')
t.deepEqual(sql, 'DROP TABLE IF EXISTS `users`')
})
*/
tape('insert()', async function (t) {
const builder = new SQL()
const output = builder.insert('users', { username: 'joe', password: '123' })
t.deepEqual(output, ['INSERT INTO `users` (`username`, `password`) VALUES (?, ?)', ['joe', '123']])
const output2 = builder.insert('users', { username: 'joe', password: '123' }, { ignore: true })
t.deepEqual(output2, ['INSERT OR IGNORE INTO `users` (`username`, `password`) VALUES (?, ?)', ['joe', '123']])
const output3 = builder.insert('files', { content: Buffer.from('Hello World!') })
t.deepEqual(output3, ['INSERT INTO `files` (`content`) VALUES (?)', [Buffer.from('Hello World!')]])
})
tape('select()', async function (t) {
const builder = new SQL()
const output = builder.select('users')
t.deepEqual(output, ['SELECT * FROM `users`', []])
const output2 = builder.select('users', ['username'])
t.deepEqual(output2, ['SELECT `username` FROM `users`', []])
const output3 = builder.select('users', ['username'], 'LIMIT 1')
t.deepEqual(output3, ['SELECT `username` FROM `users` LIMIT 1', []])
const output4 = builder.select('users', ['password'], 'username = ?', 'joe')
t.deepEqual(output4, ['SELECT `password` FROM `users` WHERE username = ?', ['joe']])
const output5 = builder.select('users', ['*'], 'ORDER BY username ASC')
t.deepEqual(output5, ['SELECT * FROM `users` ORDER BY username ASC', []])
const output6 = builder.select('users', ['*'], 'ORDER BY username ASC LIMIT 1')
t.deepEqual(output6, ['SELECT * FROM `users` ORDER BY username ASC LIMIT 1', []])
const output7 = builder.select('users', ['*'], 'username = ? ORDER BY username ASC LIMIT 2', 'joe')
t.deepEqual(output7, ['SELECT * FROM `users` WHERE username = ? ORDER BY username ASC LIMIT 2', ['joe']])
const output8 = builder.select('users', ['*'], 'username = ?', 'random-username')
t.deepEqual(output8, ['SELECT * FROM `users` WHERE username = ?', ['random-username']])
const output9 = builder.select('users', ['*'], 'username LIKE ?', 'b%')
t.deepEqual(output9, ['SELECT * FROM `users` WHERE username LIKE ?', ['b%']])
})
tape('selectOne()', async function (t) {
const builder = new SQL()
const output = builder.selectOne('users', ['*'], 'username = ?', 'joe')
t.deepEqual(output, ['SELECT * FROM `users` WHERE username = ? LIMIT 1', ['joe']])
const output2 = builder.selectOne('users', ['*'], 'ORDER BY username ASC')
t.deepEqual(output2, ['SELECT * FROM `users` ORDER BY username ASC LIMIT 1', []])
const output3 = builder.selectOne('users', ['*'], 'username = ? ORDER BY username ASC', 'joe')
t.deepEqual(output3, ['SELECT * FROM `users` WHERE username = ? ORDER BY username ASC LIMIT 1', ['joe']])
const output4 = builder.selectOne('users', ['*'], 'username = ?', 'random-username')
t.deepEqual(output4, ['SELECT * FROM `users` WHERE username = ? LIMIT 1', ['random-username']])
})
tape('exists()', async function (t) {
const builder = new SQL()
const output = builder.exists('users', 'username = ?', 'joe')
t.deepEqual(output, ['SELECT EXISTS(SELECT 1 FROM `users` WHERE username = ? LIMIT 1)', ['joe']])
})
tape('count()', async function (t) {
const builder = new SQL()
const output = builder.count('users')
t.deepEqual(output, ['SELECT COUNT(1) FROM `users`', []])
const output2 = builder.count('users', 'username = ?', 'joe')
t.deepEqual(output2, ['SELECT COUNT(1) FROM `users` WHERE username = ?', ['joe']])
})
tape('update()', async function (t) {
const builder = new SQL()
const output = builder.update('users', { username: 'alice' })
t.deepEqual(output, ['UPDATE `users` SET `username` = ?', ['alice']])
const output2 = builder.update('users', { username: 'alice' }, 'LIMIT 1')
t.deepEqual(output2, ['UPDATE `users` SET `username` = ? LIMIT 1', ['alice']])
const output3 = builder.update('users', { username: 'alice' }, 'username = ?', 'bob')
t.deepEqual(output3, ['UPDATE `users` SET `username` = ? WHERE username = ?', ['alice', 'bob']])
})
tape('update() with arithmetic', async function (t) {
const builder = new SQL()
const output = builder.update('users', [{ count: 'count + ?' }, 1], 'username = ?', 'bob')
t.deepEqual(output, ['UPDATE `users` SET `count` = count + ? WHERE username = ?', [1, 'bob']])
})
tape('delete()', async function (t) {
const builder = new SQL()
const output = builder.delete('users')
t.deepEqual(output, ['DELETE FROM `users`', []])
const output2 = builder.delete('users', 'LIMIT 1')
t.deepEqual(output2, ['DELETE FROM `users` LIMIT 1', []])
const output3 = builder.delete('users', 'username = ?', 'bob')
t.deepEqual(output3, ['DELETE FROM `users` WHERE username = ?', ['bob']])
})