-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
237 lines (202 loc) Β· 4.16 KB
/
test.js
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
const test = require('tape')
const { 'api-firestore': firestore } = require('.')
//
// Create a mock request and response method
//
function status (code) {
this.statusCode = code
return this
}
function send (obj) {
const body = { ...this, ...obj }
return body
}
function set (value) {
this[value] = value
return this
}
const res = {
status,
send,
set
}
const collection = 'mock1'
const document = 'mock-doc-1'
const value = {
name: 'joe',
email: '[email protected]',
connections: ['a', 'b', 'c']
}
test('sanity', t => {
t.ok(true)
t.end()
})
test('pass - POST new mock document', async t => {
const req = {
method: 'POST',
body: {
collection,
document,
value
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
t.equals(statusCode, 200)
t.equals(data, 'OK')
t.end()
})
test('fail - POST new document, missing collection name', async t => {
const req = {
method: 'POST',
body: {
document
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(err)
t.ok(!data)
t.equals(statusCode, 404)
t.equals(err, `Collection name required.`)
t.end()
})
test('fail - POST new document, missing document', async t => {
const req = {
method: 'POST',
body: {
collection
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(err)
t.ok(!data)
t.equals(statusCode, 404)
t.equals(err, `Document name required.`)
t.end()
})
test('pass - GET document by document name', async t => {
const req = {
method: 'GET',
query: {
collection,
document
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
delete data.ctime
t.equals(statusCode, 200)
t.deepEqual(data, value)
t.end()
})
test('fail - GET document by document name', async t => {
const req = {
method: 'GET',
query: {
collection,
document: 'xxx'
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(err)
t.ok(!data)
t.equals(statusCode, 404)
t.equals(err, 'Unable to find the document for xxx.')
t.end()
})
test('pass - GET all documents from a collection', async t => {
const req = {
method: 'GET',
query: {
allDocs: true,
collection
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
t.ok(data.length, 1)
t.equals(statusCode, 200)
t.end()
})
test('pass - POST update document', async t => {
const req = {
method: 'POST',
body: {
collection,
document,
value: {
phone: '+1-525-555-1111'
},
update: true
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
t.equals(statusCode, 200)
t.equals(data, 'OK')
t.end()
})
test('pass - GET document by document, verify update', async t => {
const req = {
method: 'GET',
query: {
collection,
document
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
const valid = { ...value, phone: '+1-525-555-1111' }
delete data.ctime
t.equals(statusCode, 200)
t.deepEqual(data, valid)
t.end()
})
test('fail - POST update document, document already exists', async t => {
const req = {
method: 'POST',
body: {
collection,
document,
update: false
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(err)
t.ok(!data)
t.equals(statusCode, 404)
t.equals(err, `Document, ${document}, already exists.`)
t.end()
})
test('pass - DELETE document by document', async t => {
const req = {
method: 'DELETE',
query: {
collection,
document
}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(!err)
t.ok(data)
t.equals(statusCode, 200)
t.deepEqual(data, 'OK')
t.end()
})
test('fail - DELETE document by document, no document or collection', async t => {
const req = {
method: 'DELETE',
query: {}
}
const { err, data, statusCode } = await firestore(req, res)
t.ok(err)
t.ok(!data)
t.equals(statusCode, 404)
t.equals(err, 'Collection name is empty.')
t.end()
})