-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_metric_in_google_sheet.test.js
More file actions
352 lines (299 loc) · 12.7 KB
/
update_metric_in_google_sheet.test.js
File metadata and controls
352 lines (299 loc) · 12.7 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import assert from 'node:assert/strict'
import { randomUUID } from 'node:crypto'
import { loadEnvFile } from 'node:process'
import { describe, it } from 'node:test'
try {
loadEnvFile('.env')
} catch (error) {
if (error.code === 'ENOENT') {
console.log('No env file found, proceeding without it')
} else {
throw error
}
}
const {
default: component,
calculateUniqueRunID,
} = await import('./update_metric_in_google_sheet.js')
describe(component.name, () => {
describe('run', () => {
it('should add a column if no column is found for that record type', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = 'abcd1234'
component.source_file_name = 'test.csv'
component.record_type = Math.random() + ' Lines'
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
assert.equal(response.error, undefined)
assert.ok(
String(response.warnings?.join("\n")).includes('No column found for'),
'Expected a warning that no column was found for that record type'
)
assert.ok(response.insertedNewColumn)
assert.equal(response.newCount, 1)
})
it('should report an error if not given a Run ID (nor told to generate a new one)', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = ''
component.source_file_name = 'test.csv'
component.record_type = 'ICJEs'
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
assert.ok(
String(response.error).includes('Run ID'),
'Expected an error about the Run ID'
)
})
it('should report an error if given a Record Type when generating a new row', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = 'NEW'
component.source_file_name = 'test.csv'
component.record_type = 'ICJE Lines'
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
const errorMessage = String(response.error)
assert.ok(
errorMessage.includes('Record Type') && errorMessage.includes('new'),
'Expected an error about providing a Record Type when generating a new Run ID'
)
})
it('should return an error if no row has the given the File Name and Run ID', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = 'zzzzzzzz'
component.source_file_name = 'test.csv'
component.record_type = 'ICJE Lines'
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
assert.ok(
String(response.error).includes('No row found for'),
'Expected an error that no row was found for that run id'
)
})
it('should add a row (and use the Event ID as the actual Run ID) if given a Run ID of "NEW"', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
const exampleEventId = randomUUID()
component.run_id = 'NEW'
component.source_file_name = 'test.csv'
component.record_type = ''
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
component.event_id = exampleEventId
const response = await component.run()
console.debug(response)
assert.equal(response.error, undefined)
assert.ok(response.insertedNewRow)
assert.equal(response.runID, exampleEventId)
assert.notEqual(response.runID, 'NEW')
assert.equal(response.newCount, undefined)
})
it('should indicate that it was a dry run (when adding a new row during a dry run)', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
const exampleEventId = randomUUID()
component.run_id = 'NEW'
component.was_dry_run = true
component.source_file_name = 'test.csv'
component.record_type = ''
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
component.event_id = exampleEventId
const response = await component.run()
console.debug(response)
assert.equal(response.error, undefined)
assert.ok(response.insertedNewRow)
assert.equal(response.dryRun, 'Yes')
})
it('should indicate that it was not a dry run (when adding a new row during a non-dry-run)', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
const exampleEventId = randomUUID()
component.run_id = 'NEW'
component.was_dry_run = false
component.source_file_name = 'test.csv'
component.record_type = ''
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
component.event_id = exampleEventId
const response = await component.run()
console.debug(response)
assert.equal(response.error, undefined)
assert.ok(response.insertedNewRow)
assert.equal(response.dryRun, 'No')
})
it('should add a row and calculate a unique Run ID if given an existing Run ID when adding a row', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
const existingEventId = 'abcd1234'
component.run_id = 'NEW'
component.source_file_name = 'test.csv'
component.record_type = ''
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
component.event_id = existingEventId
const response = await component.run()
console.debug(response)
assert.equal(response.error, undefined)
assert.ok(response.insertedNewRow)
assert.ok(response.runID.startsWith(existingEventId))
assert.notEqual(response.runID, existingEventId)
assert.notEqual(response.runID, 'NEW')
assert.equal(response.newCount, undefined)
})
it('should update the existing row if one matches the File Name and Run ID', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = 'abcd1234'
component.source_file_name = 'test.csv'
component.record_type = 'ICJE Lines'
component.number_of_items = 1
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
assert.equal(response.error, undefined)
assert.equal(response.insertedNewRow, false)
assert.equal(response.runID, component.run_id)
assert.ok(response.newCount > 0)
})
it('should update by the specified amount', async (testContext) => {
const googleServiceAccountKey = process.env.TEST_GOOGLE_SERVICE_ACCOUNT_KEY
if (!googleServiceAccountKey) {
testContext.skip('Lacking GOOGLE_SERVICE_ACCOUNT_KEY, skipping test')
return
}
const googleSheetId = process.env.TEST_GOOGLE_SHEET_ID
assert.ok(googleSheetId, 'No GOOGLE_SHEET_ID provided')
component.run_id = 'abcd1234'
component.source_file_name = 'test.csv'
component.record_type = 'Invoice Lines'
component.number_of_items = 3
component.google_sheet_id = googleSheetId
component.google_service_account_key = googleServiceAccountKey
const response = await component.run({
steps: { trigger: {} },
$: {}
})
console.debug(response)
assert.equal(response.error, undefined)
assert.equal(response.insertedNewRow, false)
assert.equal(response.runID, component.run_id)
assert.notEqual(response.previousCount, undefined, 'Did not return previous count')
assert.equal(
response.newCount - response.previousCount,
component.number_of_items
)
})
it('should gracefully handle any Errors thrown', async () => {
component.google_service_account_key = 'NOT a valid JSON string, to trigger an error'
const response = await component.run()
console.debug(response)
assert.notEqual(response.error, undefined)
})
})
describe('calculateUniqueRunID', () => {
it('should return the given Run ID if it is not in the list of existing Run IDs', async () => {
const givenRunID = 'abcd1234'
const existingRunIDs = []
const result = calculateUniqueRunID(givenRunID, existingRunIDs)
assert.equal(result, givenRunID)
})
it('should return a modified Run ID if the given one is in the list of existing Run IDs', async () => {
const givenRunID = 'abcd1234'
const existingRunIDs = ['abcd1234']
const result = calculateUniqueRunID(givenRunID, existingRunIDs)
assert.notEqual(result, givenRunID)
})
it('should return a unique Run ID even if the given one and what it would change it to are in the list of existing Run IDs', async () => {
const givenRunID = 'abcd1234'
const existingRunIDs = ['abcd1234']
const nextRunID = calculateUniqueRunID(givenRunID, existingRunIDs)
existingRunIDs.push(nextRunID)
const result = calculateUniqueRunID(givenRunID, existingRunIDs)
assert.notEqual(result, givenRunID)
for (const existingRunID of existingRunIDs) {
assert.notEqual(result, existingRunID)
}
})
})
})