|
1 | 1 | import { PutItemInput } from 'aws-sdk/clients/dynamodb' |
| 2 | +import moment from 'moment-es6' |
2 | 3 | import { getTableName } from '../../../../test/helper/get-table-name.function' |
3 | | -import { SimpleWithIdModel } from '../../../../test/models/simple-with-id.model' |
| 4 | +import { SimpleWithCompositePartitionKeyModel } from '../../../../test/models/simple-with-composite-partition-key.model' |
| 5 | +import { SimpleWithPartitionKeyModel } from '../../../../test/models/simple-with-partition-key.model' |
4 | 6 | import { PutRequest } from './put.request' |
5 | 7 |
|
6 | 8 | describe('put request', () => { |
7 | 9 | it('default params', () => { |
8 | | - const item: SimpleWithIdModel = { id: 'myId', age: 45 } |
9 | | - const request = new PutRequest(null, SimpleWithIdModel, getTableName(SimpleWithIdModel), item) |
| 10 | + const item: SimpleWithPartitionKeyModel = { id: 'myId', age: 45 } |
| 11 | + const request = new PutRequest(null, SimpleWithPartitionKeyModel, getTableName(SimpleWithPartitionKeyModel), item) |
10 | 12 | const params: PutItemInput = request.params |
11 | 13 |
|
12 | | - expect(params.TableName).toBe('simple-with-id-models') |
| 14 | + expect(params.TableName).toBe('simple-with-partition-key-models') |
13 | 15 | expect(params.Item).toEqual({ id: { S: 'myId' }, age: { N: '45' } }) |
14 | 16 | expect(Object.keys(params).length).toBe(2) |
15 | 17 | }) |
16 | 18 |
|
17 | 19 | describe('if exists condition', () => { |
18 | | - it('simple', () => { |
19 | | - const item: SimpleWithIdModel = { id: 'myId', age: 45 } |
20 | | - const request = new PutRequest(null, SimpleWithIdModel, getTableName(SimpleWithIdModel), item) |
| 20 | + it('simple partition key', () => { |
| 21 | + const item: SimpleWithPartitionKeyModel = { id: 'myId', age: 45 } |
| 22 | + const request = new PutRequest(null, SimpleWithPartitionKeyModel, getTableName(SimpleWithPartitionKeyModel), item) |
21 | 23 | request.ifNotExists() |
22 | 24 |
|
23 | 25 | const params: PutItemInput = request.params |
24 | | - expect(params.ConditionExpression).toBe('attribute_not_exists (#id)') |
| 26 | + expect(params.ConditionExpression).toBe('(attribute_not_exists (#id))') |
25 | 27 | expect(params.ExpressionAttributeNames).toEqual({ '#id': 'id' }) |
26 | 28 | expect(params.ExpressionAttributeValues).toBeUndefined() |
27 | 29 | }) |
28 | 30 |
|
| 31 | + it('composite partition key', () => { |
| 32 | + const now = moment() |
| 33 | + const item: SimpleWithCompositePartitionKeyModel = { id: 'myId', creationDate: now, age: 45 } |
| 34 | + const request = new PutRequest( |
| 35 | + null, |
| 36 | + SimpleWithCompositePartitionKeyModel, |
| 37 | + getTableName(SimpleWithCompositePartitionKeyModel), |
| 38 | + item |
| 39 | + ) |
| 40 | + request.ifNotExists() |
| 41 | + |
| 42 | + const params: PutItemInput = request.params |
| 43 | + expect(params.ConditionExpression).toBe('(attribute_not_exists (#id) AND attribute_not_exists (#creationDate))') |
| 44 | + expect(params.ExpressionAttributeNames).toEqual({ '#id': 'id', '#creationDate': 'creationDate' }) |
| 45 | + expect(params.ExpressionAttributeValues).toBeUndefined() |
| 46 | + }) |
| 47 | + |
29 | 48 | it('predicate', () => { |
30 | | - const item: SimpleWithIdModel = { id: 'myId', age: 45 } |
31 | | - const request = new PutRequest(null, SimpleWithIdModel, getTableName(SimpleWithIdModel), item) |
| 49 | + const item: SimpleWithPartitionKeyModel = { id: 'myId', age: 45 } |
| 50 | + const request = new PutRequest(null, SimpleWithPartitionKeyModel, getTableName(SimpleWithPartitionKeyModel), item) |
32 | 51 | request.ifNotExists(25 + 20 === 40) |
33 | 52 |
|
34 | 53 | const params: PutItemInput = request.params |
|
0 commit comments