-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
233: Hookup A&R to Data Register. (#267)
* 233: Hookup A&R to Data Register. * Remove useless declaration. * Update const. * Bump readme. * Fix UT --------- Co-authored-by: Daniel Truong <[email protected]>
- Loading branch information
1 parent
ff506f2
commit 52f2da2
Showing
12 changed files
with
580 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
const AWS = require("aws-sdk"); | ||
const { DocumentClient } = require("aws-sdk/clients/dynamodb"); | ||
const { REGION, ENDPOINT, TABLE_NAME, NAME_CACHE_TABLE_NAME } = require("./global/settings"); | ||
const docClient = new DocumentClient({ | ||
region: REGION, | ||
endpoint: ENDPOINT, | ||
convertEmptyValues: true, | ||
}); | ||
async function setupDb() { | ||
// Insert a document for the handler to now find and update. | ||
await docClient | ||
.put({ | ||
TableName: TABLE_NAME, | ||
Item: { | ||
"pk": "0673::Backcountry Cabins", | ||
"sk": "201702", | ||
"activity": "Backcountry Cabins", | ||
"date": "201702", | ||
"isLegacy": true, | ||
"isLocked": true, | ||
"lastUpdated": "2023-04-04T21:32:34.317Z", | ||
"legacyData": { | ||
"legacy_backcountryCabinsNetRevenue": 0, | ||
"legacy_backcountryCabinsTotalAttendancePeople": 0 | ||
}, | ||
"orcs": "1", | ||
"parkName": "Strathcona Park", | ||
"subAreaId": "0673" | ||
} | ||
}) | ||
.promise(); | ||
} | ||
|
||
describe("Name Update Tests", () => { | ||
const OLD_ENV = process.env; | ||
beforeEach(async () => { | ||
jest.resetModules(); | ||
process.env = { ...OLD_ENV }; // Make a copy of environment | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = OLD_ENV; // Restore old environment | ||
}); | ||
|
||
beforeAll(async () => { | ||
return await setupDb(); | ||
}); | ||
|
||
test("updateLocalCache", async () => { | ||
const axios = require('axios'); | ||
jest.mock("axios"); | ||
axios.get.mockImplementation( | ||
() => Promise.resolve({ | ||
statusCode: 200, | ||
data: { | ||
data: { | ||
items: [ | ||
{ | ||
updateDate: '2023-09-18T16:54:12.574Z', | ||
displayName: 'Strathcona Park', | ||
createDate: '2023-09-15T17:13:14.633Z', | ||
status: 'current', | ||
sk: 'Details', | ||
pk: '1', | ||
effectiveDate: '1911-03-01', | ||
legalName: 'Strathcona Park', | ||
phoneticName: '' | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
); | ||
|
||
const nameUpdateHandler = require("../lambda/nameUpdate/index"); | ||
|
||
// Cached document keys | ||
const CACHED_DOCUMENT = { | ||
TableName: NAME_CACHE_TABLE_NAME, | ||
Key: { | ||
pk: "1", | ||
}, | ||
}; | ||
|
||
// AR document key | ||
const AR_DOCUMENT_KEY = { | ||
pk: "0673::Backcountry Cabins", | ||
sk: "201702" | ||
}; | ||
|
||
// Ensure this doesn't exist yet. | ||
const notFoundDoc = await docClient.get(CACHED_DOCUMENT).promise(); | ||
expect(notFoundDoc).toStrictEqual({}); | ||
|
||
// Call the handler, it will have cache-miss | ||
await nameUpdateHandler.handler({}, null); | ||
|
||
// Expect the cache to be updated. | ||
const doc = await docClient.get(CACHED_DOCUMENT).promise(); | ||
expect(doc.Item.pk).toBe("1"); | ||
|
||
// Change the last cached item to be different in order to trigger a displayName | ||
// change on the handler. | ||
const params = { | ||
TableName: NAME_CACHE_TABLE_NAME, | ||
Key: { pk: '1' }, | ||
UpdateExpression: 'set displayName =:displayName', | ||
ExpressionAttributeValues: { | ||
':displayName': 'some other park name' | ||
} | ||
}; | ||
await docClient.update(params).promise(); | ||
const cachedDocumentSet = await docClient.get(CACHED_DOCUMENT).promise(); | ||
expect(cachedDocumentSet.Item.displayName).toBe('some other park name'); | ||
|
||
// Also update the backcountry cabin record in the main table | ||
const params2 = { | ||
TableName: TABLE_NAME, | ||
Key: AR_DOCUMENT_KEY, | ||
UpdateExpression: 'set parkName =:parkName', | ||
ExpressionAttributeValues: { | ||
':parkName': 'some other park name' | ||
} | ||
}; | ||
await docClient.update(params2).promise(); | ||
const arDocumentSetParkName = await docClient.get({ | ||
TableName: TABLE_NAME, | ||
Key: AR_DOCUMENT_KEY, | ||
}).promise(); | ||
|
||
expect(arDocumentSetParkName.Item.parkName).toBe('some other park name'); | ||
|
||
// Run the update | ||
await nameUpdateHandler.handler({}, null); | ||
|
||
// Fetch the updated cache and check that it has been udpated | ||
const cachedDocument = await docClient.get(CACHED_DOCUMENT).promise(); | ||
|
||
// Ensure it was updated | ||
expect(cachedDocument.Item.displayName).toBe('Strathcona Park'); | ||
|
||
// Fetch the updated AR document and check that it has been udpated | ||
const arDocument = await docClient.get({ | ||
TableName: TABLE_NAME, | ||
Key: AR_DOCUMENT_KEY, | ||
}).promise(); | ||
|
||
expect(arDocument.Item.parkName).toBe('Strathcona Park'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.