-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.test.js
215 lines (188 loc) · 5.8 KB
/
main.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
const assert = require('chai').assert;
const { integrify } = require('../lib');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
describe('Error conditions', () => {
it('should error on bad rule', () => {
assert.throws(
() => integrify({ rule: 'BAD_RULE_ea8e3a2a2d3e' }),
/Unknown rule/i
);
assert.throws(() => require('./functions-bad-rules-file'), /Unknown rule/i);
});
it('should error on no rule or config', () => {
assert.throws(() => integrify(42), /Input must be rule or config/i);
});
it('should error on absent config file', () => {
assert.throws(
() => require('./functions-absent-rules-file'),
/Rules file not found/i
);
});
});
describe('REPLICATE_ATTRIBUTES', () => {
it('should replicate attributes', async () => {
// Create master document to replicate from
const masterRef = await db
.collection('master')
.add({ random: Math.random() });
const masterId = masterRef.id;
// Create couple of detail docs to replicate to
await db.collection('detail1').add({ masterId });
await db.collection('detail2').add({ masterId });
// Update master doc
const masterField1 = randstr();
const masterField2 = randstr();
await masterRef.update({ masterField1, masterField2 });
// Ensure update is reflected in detail docs
await assertQuerySizeEventually(
db
.collection('detail1')
.where('masterId', '==', masterId)
.where('detail1Field1', '==', masterField1),
1
);
await assertQuerySizeEventually(
db
.collection('detail2')
.where('masterId', '==', masterId)
.where('detail2Field1', '==', masterField1),
1
);
// Make an irrelevant update
await masterRef.set({ someOtherField: randstr() });
// Ensure prehook is called twice (once for each update)
await assertQuerySizeEventually(
db
.collection('prehooks')
.where(
'message',
'==',
'[788a32e05504] REPLICATE_ATTRIBUTES prehook was called!'
),
2
);
});
});
describe('DELETE_REFERENCES', () => {
it('should delete references', async () => {
// Create master document to replicate from
const masterRef = await db
.collection('master')
.add({ random: Math.random() });
const masterId = masterRef.id;
// Create couple of detail docs to replicate to
await db.collection('detail1').add({ masterId });
await db.collection('detail2').add({ masterId });
// Delete master doc
await masterRef.delete();
// Ensure detail docs are deleted
await assertQuerySizeEventually(
db.collection('detail1').where('masterId', '==', masterId),
0
);
await assertQuerySizeEventually(
db.collection('detail2').where('masterId', '==', masterId),
0
);
// Ensure prehook is called
await assertQuerySizeEventually(
db
.collection('prehooks')
.where(
'message',
'==',
'[6a8f4f8f090c] DELETE_REFERENCES prehook was called!'
),
1
);
});
});
describe('MAINTAIN_COUNT', () => {
it('should maintain count', async () => {
// Create an article to be favorited
const articleId = randstr();
await db.collection('articles').doc(articleId).set({ favoritesCount: 0 });
// Add a few "favorites" for the article
const NUM_TIMES_TO_FAVORITE = 5;
const favorites = [];
for (let i = 0; i < NUM_TIMES_TO_FAVORITE; ++i) {
favorites.push(await db.collection('favorites').add({ articleId }));
}
// Ensure an update on a "favorite" is ignored
await favorites[0].update({ random: Math.random() });
// Ensure "favoritesCount" is correct
await assertQuerySizeEventually(
db
.collection('articles')
.where(admin.firestore.FieldPath.documentId(), '==', articleId)
.where('favoritesCount', '==', NUM_TIMES_TO_FAVORITE),
1
);
// Remove a few "favorites" for the article
const NUM_TIMES_TO_UNFAVORITE = 3;
for (let i = 0; i < NUM_TIMES_TO_UNFAVORITE; ++i) {
await favorites[i].delete();
}
// Ensure "favoritesCount" is correct
await assertQuerySizeEventually(
db
.collection('articles')
.where(admin.firestore.FieldPath.documentId(), '==', articleId)
.where(
'favoritesCount',
'==',
NUM_TIMES_TO_FAVORITE - NUM_TIMES_TO_UNFAVORITE
),
1
);
// Delete article and ensure favoritesCount is not updated on decrement or
// increment (See issue #3)
await db.collection('articles').doc(articleId).delete();
const newFavorite = await db.collection('favorites').add({ articleId });
await newFavorite.delete();
await assertQuerySizeEventually(
db
.collection('articles')
.where(admin.firestore.FieldPath.documentId(), '==', articleId),
0
);
});
});
// Helper functions
function randstr() {
return Math.random().toString(36).substr(2);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function keypress() {
console.log('Press any key to continue...');
process.stdin.setRawMode(true);
return new Promise((resolve) =>
process.stdin.once('data', () => {
process.stdin.setRawMode(false);
resolve();
})
);
}
async function assertQuerySizeEventually(
query,
expectedResultSize,
log = console.log
) {
log(`Asserting query result to have [${expectedResultSize}] entries ... `);
await sleep(1000);
const docs = await new Promise((res) => {
unsubscribe = query.onSnapshot((snap) => {
log(`Current result size: [${snap.size}]`);
if (snap.size === expectedResultSize) {
log('Matched!');
unsubscribe();
res(snap.docs);
}
});
});
return docs;
}