-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated synonymGroups table-stream with tests
formatting additional test cleaning up additional testing with adjustments removed dynamodb calls and adjusted mocks adds additional test for no duplicate eventId adjusting logic so that nothing happens if there is no data to save instead of creating a new synonym group cleaning up tests bailing if somehow there is no data for the group
- Loading branch information
Showing
6 changed files
with
236 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { search } from '@nasa-gcn/architect-functions-search' | ||
import type { DynamoDBRecord } from 'aws-lambda' | ||
|
||
import { handler } from '~/table-streams/synonyms/index' | ||
|
||
const synonymId = 'abcde-abcde-abcde-abcde-abcde' | ||
const eventId = 'GRB 123' | ||
const existingEventId = 'GRB 99999999' | ||
|
||
const putData = { | ||
index: 'synonym-groups', | ||
id: synonymId, | ||
body: { | ||
synonymId, | ||
eventIds: [] as string[], | ||
}, | ||
} | ||
|
||
jest.mock('@nasa-gcn/architect-functions-search', () => ({ | ||
search: jest.fn(), | ||
})) | ||
|
||
const mockIndex = jest.fn() | ||
const mockDelete = jest.fn() | ||
const mockGet = jest.fn() | ||
|
||
const mockStreamEvent = { | ||
Records: [ | ||
{ | ||
eventID: '1', | ||
eventName: 'INSERT', | ||
eventVersion: '1.0', | ||
eventSource: 'aws:dynamodb', | ||
awsRegion: 'us-west-2', | ||
dynamodb: { | ||
Keys: { | ||
synonymId: { | ||
S: synonymId, | ||
}, | ||
eventId: { | ||
S: eventId, | ||
}, | ||
}, | ||
NewImage: { | ||
synonymId: { | ||
S: synonymId, | ||
}, | ||
eventId: { | ||
S: eventId, | ||
}, | ||
}, | ||
SequenceNumber: '111', | ||
SizeBytes: 26, | ||
StreamViewType: 'NEW_IMAGE', | ||
}, | ||
eventSourceARN: | ||
'arn:aws:dynamodb:us-west-2:123456789012:table/synonym-groups/stream/2020-01-01T00:00:00.000', | ||
} as DynamoDBRecord, | ||
], | ||
} | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('testing put synonymGroup table-stream', () => { | ||
test('insert new synonym group', async () => { | ||
putData.body.eventIds = [eventId] | ||
mockGet.mockResolvedValue(null) | ||
;(search as unknown as jest.Mock).mockReturnValue({ | ||
index: mockIndex, | ||
delete: mockDelete, | ||
get: mockGet, | ||
}) | ||
|
||
await handler(mockStreamEvent) | ||
|
||
expect(mockIndex).toHaveBeenCalledWith(putData) | ||
}) | ||
|
||
test('insert into existing synonym group', async () => { | ||
putData.body.eventIds = [existingEventId, eventId] | ||
mockGet.mockResolvedValue({ | ||
body: { _source: { synonymId, eventIds: [existingEventId] } }, | ||
}) | ||
;(search as unknown as jest.Mock).mockReturnValue({ | ||
index: mockIndex, | ||
delete: mockDelete, | ||
get: mockGet, | ||
}) | ||
|
||
await handler(mockStreamEvent) | ||
|
||
expect(mockIndex).toHaveBeenCalledWith(putData) | ||
}) | ||
|
||
test('insert only once', async () => { | ||
putData.body.eventIds = [existingEventId, eventId] | ||
mockGet.mockResolvedValue({ | ||
body: { _source: { synonymId, eventIds: [existingEventId, eventId] } }, | ||
}) | ||
;(search as unknown as jest.Mock).mockReturnValue({ | ||
index: mockIndex, | ||
delete: mockDelete, | ||
get: mockGet, | ||
}) | ||
|
||
await handler(mockStreamEvent) | ||
|
||
expect(mockIndex).toHaveBeenCalledWith(putData) | ||
}) | ||
}) | ||
|
||
describe('testing delete synonymGroup table-stream', () => { | ||
test('remove one eventId while leaving others', async () => { | ||
mockStreamEvent.Records[0].eventName = 'REMOVE' | ||
putData.body.eventIds = [existingEventId] | ||
mockGet.mockResolvedValue({ | ||
body: { _source: { synonymId, eventIds: [existingEventId, eventId] } }, | ||
}) | ||
;(search as unknown as jest.Mock).mockReturnValue({ | ||
index: mockIndex, | ||
delete: mockDelete, | ||
get: mockGet, | ||
}) | ||
|
||
await handler(mockStreamEvent) | ||
|
||
expect(mockIndex).toHaveBeenCalledWith(putData) | ||
}) | ||
|
||
test('remove final synonym and delete synonym group', async () => { | ||
mockStreamEvent.Records[0].eventName = 'REMOVE' | ||
const deleteData = { | ||
index: 'synonym-groups', | ||
id: synonymId, | ||
} | ||
mockGet.mockResolvedValue({ | ||
body: { _source: { synonymId, eventIds: [eventId] } }, | ||
}) | ||
;(search as unknown as jest.Mock).mockReturnValue({ | ||
index: mockIndex, | ||
delete: mockDelete, | ||
get: mockGet, | ||
}) | ||
|
||
await handler(mockStreamEvent) | ||
|
||
expect(mockDelete).toHaveBeenCalledWith(deleteData) | ||
}) | ||
}) |
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