|
| 1 | +import * as DynamoDB from 'aws-sdk/clients/dynamodb' |
| 2 | +import { of } from 'rxjs' |
| 3 | +import { getTableName } from '../../../test/helper' |
| 4 | +import { SimpleWithCompositePartitionKeyModel, SimpleWithPartitionKeyModel } from '../../../test/models' |
1 | 5 | import { Organization } from '../../../test/models/organization.model'
|
| 6 | +import { Attributes } from '../../mapper' |
| 7 | +import { DynamoRx } from '../dynamo-rx' |
2 | 8 | import { BatchGetRequest } from './batch-get.request'
|
3 | 9 |
|
4 | 10 | describe('batch get', () => {
|
5 | 11 | let request: BatchGetRequest
|
6 | 12 |
|
7 |
| - beforeEach(() => { |
8 |
| - request = new BatchGetRequest() |
| 13 | + |
| 14 | + describe('params', () => { |
| 15 | + |
| 16 | + beforeEach(() => request = new BatchGetRequest()) |
| 17 | + |
| 18 | + it('base params', () => { |
| 19 | + const params = request.params |
| 20 | + expect(params).toEqual({ RequestItems: {} }) |
| 21 | + }) |
| 22 | + |
| 23 | + it('key', () => { |
| 24 | + request.forModel(Organization, ['idValue']) |
| 25 | + const params = request.params |
| 26 | + expect(params.RequestItems).toBeDefined() |
| 27 | + expect(params.RequestItems.Organization).toBeDefined() |
| 28 | + expect(params.RequestItems.Organization).toEqual({ Keys: [{ id: { S: 'idValue' } }] }) |
| 29 | + }) |
9 | 30 | })
|
10 | 31 |
|
11 |
| - it('base params', () => { |
12 |
| - const params = request.params |
13 |
| - expect(params).toEqual({ RequestItems: {} }) |
| 32 | + |
| 33 | + describe('forModel', () => { |
| 34 | + beforeEach(() => request = new BatchGetRequest()) |
| 35 | + |
| 36 | + it('should throw when same table is used 2 times', () => { |
| 37 | + request.forModel(SimpleWithPartitionKeyModel, ['idVal']) |
| 38 | + expect(() => request.forModel(SimpleWithPartitionKeyModel, ['otherVal'])).toThrow() |
| 39 | + }) |
| 40 | + |
| 41 | + it('should throw when providing null value ', () => { |
| 42 | + expect(() => request.forModel(SimpleWithPartitionKeyModel, [<any>null])).toThrow() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should throw when sortKey is missing', () => { |
| 46 | + expect(() => request.forModel(SimpleWithCompositePartitionKeyModel, [{ partitionKey: 'idVal' }])) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should throw when partitionKey is neither string nor object', () => { |
| 50 | + expect(() => request.forModel(SimpleWithCompositePartitionKeyModel, [<any>78])) |
| 51 | + expect(() => request.forModel(SimpleWithCompositePartitionKeyModel, [<any>true])) |
| 52 | + expect(() => request.forModel(SimpleWithCompositePartitionKeyModel, [<any>new Date()])) |
| 53 | + }) |
| 54 | + |
14 | 55 | })
|
15 | 56 |
|
16 |
| - it('key', () => { |
17 |
| - request.forModel(Organization, ['idValue']) |
18 |
| - const params = request.params |
19 |
| - expect(params.RequestItems).toBeDefined() |
20 |
| - expect(params.RequestItems.Organization).toBeDefined() |
21 |
| - expect(params.RequestItems.Organization).toEqual({ Keys: [{ id: { S: 'idValue' } }] }) |
| 57 | + |
| 58 | + describe('should map the result items', () => { |
| 59 | + let batchGetItemsSpy: jasmine.Spy |
| 60 | + const jsItem: SimpleWithPartitionKeyModel = { id: 'idVal', age: 20 } |
| 61 | + const dbItem: Attributes<SimpleWithPartitionKeyModel> = { |
| 62 | + id: { S: 'idVal' }, |
| 63 | + age: { N: '20' }, |
| 64 | + } |
| 65 | + const sampleResponse: DynamoDB.BatchGetItemOutput = { |
| 66 | + Responses: { |
| 67 | + [getTableName(SimpleWithPartitionKeyModel)]: [dbItem], |
| 68 | + }, |
| 69 | + UnprocessedKeys: {}, |
| 70 | + } |
| 71 | + |
| 72 | + beforeEach(() => { |
| 73 | + batchGetItemsSpy = jasmine.createSpy().and.returnValue(of(sampleResponse)) |
| 74 | + const dynamoRx: DynamoRx = <any>{ batchGetItems: batchGetItemsSpy } |
| 75 | + request = new BatchGetRequest() |
| 76 | + Object.assign(request, { dynamoRx }) |
| 77 | + request.forModel(SimpleWithPartitionKeyModel, ['idVal']) |
| 78 | + }) |
| 79 | + |
| 80 | + it('exec', async () => { |
| 81 | + const result = await request.exec().toPromise() |
| 82 | + expect(batchGetItemsSpy).toHaveBeenCalled() |
| 83 | + expect(result).toEqual({ [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('execFullResponse', async () => { |
| 87 | + const result = await request.execFullResponse().toPromise() |
| 88 | + expect(batchGetItemsSpy).toHaveBeenCalled() |
| 89 | + expect(result).toEqual({ |
| 90 | + Responses: { [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] }, |
| 91 | + UnprocessedKeys: {}, |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
22 | 95 | })
|
| 96 | + |
23 | 97 | })
|
0 commit comments