Skip to content

Commit 1f3a501

Browse files
Merge pull request #99 from shiftcode/#87-check-syntax-for-complex-conditions
test(param-util): test for a complex condition and update statements
2 parents 15bc1ae + 3a5bbfe commit 1f3a501

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Model, PartitionKey } from '../../decorator/impl'
2+
import { Metadata, metadataForClass } from '../../decorator/metadata'
3+
import { ConditionalParams } from '../operation-params.type'
4+
import { and, or, update2 } from './logical-operator'
5+
import { attribute } from './logical-operator/attribute.function'
6+
import { addExpression } from './param-util'
7+
8+
describe('ParamUtils', () => {
9+
@Model()
10+
class FooBar {
11+
@PartitionKey()
12+
a: number
13+
b: number
14+
c: number
15+
}
16+
17+
let params: ConditionalParams
18+
let metadata: Metadata<FooBar>
19+
beforeEach(() => {
20+
params = {}
21+
metadata = metadataForClass(FooBar)
22+
})
23+
24+
it('shoud build correct conditions', () => {
25+
const conditionDefFns = [and(attribute('a').eq(1), or(attribute('b').eq(2), attribute('c').between(3, 5)))]
26+
const condition = and(...conditionDefFns)(undefined, metadata)
27+
addExpression('ConditionExpression', condition, params)
28+
29+
expect(params.ExpressionAttributeNames).toEqual({
30+
'#a': 'a',
31+
'#b': 'b',
32+
'#c': 'c',
33+
})
34+
expect(params.ExpressionAttributeValues).toEqual({
35+
':a': { N: '1' },
36+
':b': { N: '2' },
37+
':c': { N: '3' },
38+
':c_2': { N: '5' },
39+
})
40+
expect(params.ConditionExpression).toBe('((#a = :a AND (#b = :b OR #c BETWEEN :c AND :c_2)))')
41+
})
42+
43+
it('should build correct UpdateExpression', () => {
44+
const updt = update2(FooBar, 'b').set(3)(undefined, metadata)
45+
addExpression('UpdateExpression', updt, params)
46+
47+
expect(params).toEqual({
48+
UpdateExpression: '#b = :b',
49+
ExpressionAttributeNames: { '#b': 'b' },
50+
ExpressionAttributeValues: { ':b': { N: '3' } },
51+
})
52+
53+
const cndtn = attribute('b').eq(2)(undefined, metadata)
54+
addExpression('ConditionExpression', cndtn, params)
55+
56+
expect(params).toEqual({
57+
UpdateExpression: '#b = :b',
58+
ConditionExpression: '#b = :b_2',
59+
ExpressionAttributeNames: { '#b': 'b' },
60+
ExpressionAttributeValues: {
61+
':b': { N: '3' },
62+
':b_2': { N: '2' },
63+
},
64+
})
65+
})
66+
67+
it('should throw when trying to combine two update expressions', () => {
68+
params = {
69+
UpdateExpression: '#b = :b',
70+
ExpressionAttributeNames: { '#b': 'b' },
71+
ExpressionAttributeValues: { ':b': { N: '3' } },
72+
}
73+
74+
expect(() => {
75+
const updt = update2(FooBar, 'c').set(3)(undefined, metadata)
76+
addExpression('UpdateExpression', updt, params)
77+
}).toThrow()
78+
})
79+
})

src/dynamo/expression/param-util.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ export function addExpression(
3333
params.ExpressionAttributeValues = expressionAttributeValues
3434
}
3535

36-
const expression = Reflect.get(params, expressionType)
37-
if (isString(expression)) {
36+
const expression = params[expressionType]
37+
if (isString(expression) && expression !== '') {
3838
switch (expressionType) {
3939
case 'UpdateExpression':
40-
if (expression !== '') {
41-
throw new Error(
42-
'params.UpdateExpression is not empty, please use the UpdateRequest.operations() method to define all the update operations',
43-
)
44-
}
45-
break
40+
throw new Error(
41+
'params.UpdateExpression is not empty, please use the UpdateRequest.operations() method to define all the update operations',
42+
)
4643
default:
4744
;(<any>params)[expressionType] = `${expression} AND ${nameSafeCondition.statement}`
4845
}

0 commit comments

Comments
 (0)