-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathwriteLogging.spec.js
166 lines (141 loc) · 6.31 KB
/
writeLogging.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
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
const {TestEnvironment} = require("./support/testEnvironment");
describe("indexOnWriteLogging - when shouldLogTypesenseInserts is false", () => {
let testEnvironment;
beforeAll((done) => {
try {
testEnvironment = new TestEnvironment({
dotenvPath: "extensions/test-params-flatten-nested-true.local.env",
});
testEnvironment.setupTestEnvironment(done);
} catch (e) {
console.error(e);
done(e);
}
});
afterAll(async () => {
await testEnvironment.teardownTestEnvironment();
});
beforeEach(async () => {
await testEnvironment.clearAllData();
});
describe("testing onWrite logging", () => {
it("logs only itemId", async () => {
const docData = {
author: "value1",
title: "value2",
};
testEnvironment.resetCapturedEmulatorLogs();
const docRef = await testEnvironment.firestore.collection(testEnvironment.config.firestoreCollectionPath).add(docData);
await new Promise((r) => setTimeout(r, 5000));
expect(testEnvironment.capturedEmulatorLogs).toContain(`Upserting document ${docRef.id}`);
});
});
describe("testing backfill logging", () => {
it("backfills existing Firestore data in all collections to Typesense", async () => {
const book = {
author: "Author A",
title: "Title X",
country: "USA",
};
const firestoreDoc = await testEnvironment.firestore.collection(testEnvironment.config.firestoreCollectionPath).add(book);
// Wait for firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2000));
// The above will automatically add the document to Typesense,
// so delete it so we can test backfill
await testEnvironment.typesense.collections(encodeURIComponent(testEnvironment.config.typesenseCollectionName)).delete();
await testEnvironment.typesense.collections().create({
name: testEnvironment.config.typesenseCollectionName,
fields: [{name: ".*", type: "auto"}],
});
await testEnvironment.firestore.collection(testEnvironment.config.typesenseBackfillTriggerDocumentInFirestore.split("/")[0]).doc("backfill").set({trigger: true});
// Wait for firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2000));
// Check that the data was backfilled
const typesenseDocsStr = await testEnvironment.typesense.collections(encodeURIComponent(testEnvironment.config.typesenseCollectionName)).documents().export();
const typesenseDocs = typesenseDocsStr.split("\n").map((s) => JSON.parse(s));
expect(typesenseDocs.length).toBe(1);
expect(typesenseDocs[0]).toStrictEqual({
id: firestoreDoc.id,
author: book.author,
title: book.title,
});
// Check that the backfill log was written
expect(testEnvironment.capturedEmulatorLogs).not.toContain("Backfilling document");
expect(testEnvironment.capturedEmulatorLogs).toContain("Imported 1 documents into Typesense");
});
});
});
describe("indexOnWriteLogging - when shouldLogTypesenseInserts is true", () => {
let testEnvironment;
beforeAll((done) => {
testEnvironment = new TestEnvironment({
dotenvConfig: `
LOCATION=us-central1
FIRESTORE_DATABASE_REGION=nam5
FIRESTORE_COLLECTION_PATH=books
FIRESTORE_COLLECTION_FIELDS=author,title,rating,isAvailable,location,createdAt,nested_field,tags,nullField,ref
FLATTEN_NESTED_DOCUMENTS=true
LOG_TYPESENSE_INSERTS=true
TYPESENSE_HOSTS=localhost
TYPESENSE_PORT=8108
TYPESENSE_PROTOCOL=http
TYPESENSE_COLLECTION_NAME=books_firestore/1
TYPESENSE_API_KEY=xyz
`,
});
testEnvironment.setupTestEnvironment(done);
});
afterAll(async () => {
await testEnvironment.teardownTestEnvironment();
});
beforeEach(async () => {
await testEnvironment.clearAllData();
});
describe("testing basic onWrite logging", () => {
it("logs detailed inserts", async () => {
const docData = {
author: "value1",
title: "value2",
};
testEnvironment.resetCapturedEmulatorLogs();
const docRef = await testEnvironment.firestore.collection(testEnvironment.config.firestoreCollectionPath).add(docData);
await new Promise((r) => setTimeout(r, 5000));
expect(testEnvironment.capturedEmulatorLogs).toContain(`Upserting document ${JSON.stringify({...docData, id: docRef.id})}`);
});
});
describe("testing backfill logging", () => {
it("backfills existing Firestore data in all collections to Typesense", async () => {
const book = {
author: "Author A",
title: "Title X",
country: "USA",
};
const firestoreDoc = await testEnvironment.firestore.collection(testEnvironment.config.firestoreCollectionPath).add(book);
// Wait for firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2000));
// The above will automatically add the document to Typesense,
// so delete it so we can test backfill
await testEnvironment.typesense.collections(encodeURIComponent(testEnvironment.config.typesenseCollectionName)).delete();
await testEnvironment.typesense.collections().create({
name: testEnvironment.config.typesenseCollectionName,
fields: [{name: ".*", type: "auto"}],
});
await testEnvironment.firestore.collection(testEnvironment.config.typesenseBackfillTriggerDocumentInFirestore.split("/")[0]).doc("backfill").set({trigger: true});
// Wait for firestore cloud function to write to Typesense
await new Promise((r) => setTimeout(r, 2000));
// Check that the data was backfilled
const typesenseDocsStr = await testEnvironment.typesense.collections(encodeURIComponent(testEnvironment.config.typesenseCollectionName)).documents().export();
const typesenseDocs = typesenseDocsStr.split("\n").map((s) => JSON.parse(s));
expect(typesenseDocs.length).toBe(1);
const expectedResult = {
author: book.author,
title: book.title,
id: firestoreDoc.id,
};
expect(typesenseDocs[0]).toStrictEqual(expectedResult);
// Check that the backfill log was written
expect(testEnvironment.capturedEmulatorLogs).toContain(`Backfilling document ${JSON.stringify(expectedResult)}`);
expect(testEnvironment.capturedEmulatorLogs).toContain("Imported 1 documents into Typesense");
});
});
});