-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindexOnWriteSubcollection.spec.js
123 lines (96 loc) · 4.16 KB
/
indexOnWriteSubcollection.spec.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
const {TestEnvironment} = require("./support/testEnvironment");
// test case configs
const TEST_FIRESTORE_PARENT_COLLECTION_PATH = "users";
const TEST_FIRESTORE_CHILD_FIELD_NAME = "books";
describe("indexOnWriteSubcollection", () => {
let testEnvironment;
const parentCollectionPath = TEST_FIRESTORE_PARENT_COLLECTION_PATH;
const childFieldName = TEST_FIRESTORE_CHILD_FIELD_NAME;
let parentIdField = null;
let config = null;
let firestore = null;
let typesense = null;
beforeAll((done) => {
testEnvironment = new TestEnvironment({
dotenvPath: "extensions/test-params-subcategory-flatten-nested-false.local.env",
outputAllEmulatorLogs: true,
});
testEnvironment.setupTestEnvironment((err) => {
const matches = testEnvironment.config.firestoreCollectionPath.match(/{([^}]+)}/g);
expect(matches).toBeDefined();
expect(matches.length).toBe(1);
parentIdField = matches[0].replace(/{|}/g, "");
expect(parentIdField).toBeDefined();
config = testEnvironment.config;
firestore = testEnvironment.firestore;
typesense = testEnvironment.typesense;
done();
});
});
afterAll(async () => {
await testEnvironment.teardownTestEnvironment();
});
beforeEach(async () => {
// For subcollections, need special handling to clear parent collection. Deleting here triggers firebase functions
await firestore.recursiveDelete(firestore.collection(parentCollectionPath));
await testEnvironment.clearAllData();
});
describe("Backfill from dynamic subcollections", () => {
it("backfills documents from dynamic subcollections to Typesense", async () => {
process.env.FLATTEN_NESTED_DOCUMENTS = "false";
const parentDocData = {
nested_field: {
field1: "value1",
},
};
const subDocData = {
nested_field: {
field1: "value1",
field2: ["value2", "value3", "value4"],
field3: {
fieldA: "valueA",
fieldB: ["valueB", "valueC", "valueD"],
},
},
};
// create parent document in Firestore
const parentDocRef = await firestore.collection(parentCollectionPath).add(parentDocData);
// create a subcollection with document under the parent document
const subCollectionRef = parentDocRef.collection(childFieldName);
const subDocRef = await subCollectionRef.add(subDocData);
// wait for the Firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2500));
// check that the document was indexed
let typesenseDocsStr = await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents().export({exclude_fields: ""});
let typesenseDocs = typesenseDocsStr.split("\n").map((s) => JSON.parse(s));
expect(typesenseDocs.length).toBe(1);
expect(typesenseDocs[0]).toStrictEqual({
id: subDocRef.id,
...subDocData,
[parentIdField]: parentDocRef.id,
});
// update document in Firestore
subDocData.nested_field.field1 = "new value1";
await subDocRef.update(subDocData);
// wait for the Firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2500));
// check that the document was updated
typesenseDocsStr = await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents().export({exclude_fields: ""});
typesenseDocs = typesenseDocsStr.split("\n").map((s) => JSON.parse(s));
expect(typesenseDocs.length).toBe(1);
expect(typesenseDocs[0]).toStrictEqual({
id: subDocRef.id,
...subDocData,
[parentIdField]: parentDocRef.id,
});
// delete both documents in Firestore
await subDocRef.delete();
await parentDocRef.delete();
// wait for the Firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2500));
// check that the subcollection document was deleted
typesenseDocsStr = await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents().export({exclude_fields: ""});
expect(typesenseDocsStr).toBe("");
});
});
});