1- import { AttributePath } from "./AttributePath" ;
2- import { ExpressionAttributes } from "./ExpressionAttributes" ;
3- import { MathematicalExpression } from "./MathematicalExpression" ;
1+ import { AttributePath } from './AttributePath' ;
2+ import { ExpressionAttributes } from './ExpressionAttributes' ;
3+ import { FunctionExpression } from './FunctionExpression' ;
4+ import { MathematicalExpression } from './MathematicalExpression' ;
5+ import {
6+ ExpressionAttributeNameMap ,
7+ ExpressionAttributeValueMap
8+ } from 'aws-sdk/clients/dynamodb' ;
49
510describe ( 'MathematicalExpression' , ( ) => {
6- const basicMathematicalExpression = new MathematicalExpression (
7- new AttributePath ( 'foo' ) ,
8- '+' ,
9- 1
10- ) ;
11+ const validExpressions : Array < [
12+ MathematicalExpression ,
13+ string ,
14+ ExpressionAttributeNameMap ,
15+ ExpressionAttributeValueMap
16+ ] > = [
17+ [
18+ new MathematicalExpression ( new AttributePath ( 'foo' ) , '+' , 1 ) ,
19+ '#attr0 + :val1' ,
20+ { '#attr0' : 'foo' } ,
21+ { ':val1' : { N : '1' } } ,
22+ ] ,
23+ [
24+ new MathematicalExpression (
25+ new FunctionExpression (
26+ 'if_not_exists' ,
27+ new AttributePath ( 'current_id' ) ,
28+ 0
29+ ) ,
30+ '+' ,
31+ 1
32+ ) ,
33+ 'if_not_exists(#attr0, :val1) + :val2' ,
34+ { '#attr0' : 'current_id' } ,
35+ {
36+ ':val1' : { N : '0' } ,
37+ ':val2' : { N : '1' } ,
38+ } ,
39+ ]
40+ ] ;
1141
1242 describe ( '::isMathematicalExpression' , ( ) => {
1343 it ( 'should accept valid mathematical expressions' , ( ) => {
14- expect (
15- MathematicalExpression
16- . isMathematicalExpression ( basicMathematicalExpression )
17- ) . toBe ( true ) ;
44+ for ( const [ expr , _1 , _2 , _3 ] of validExpressions ) {
45+ expect ( MathematicalExpression . isMathematicalExpression ( expr ) )
46+ . toBe ( true ) ;
47+ }
1848 } ) ;
1949
2050 it ( 'should reject non-matching values' , ( ) => {
@@ -40,17 +70,17 @@ describe('MathematicalExpression', () => {
4070
4171 describe ( '#serialize' , ( ) => {
4272 it ( 'should serialize basic mathematical expressions' , ( ) => {
43- const attributes = new ExpressionAttributes ( ) ;
44- expect ( basicMathematicalExpression . serialize ( attributes ) )
45- . toBe ( '#attr0 + :val1' ) ;
46-
47- expect ( attributes . names ) . toEqual ( {
48- '#attr0' : 'foo' ,
49- } ) ;
50-
51- expect ( attributes . values ) . toEqual ( {
52- ':val1' : { N : '1' } ,
53- } ) ;
73+ for ( const [
74+ expression ,
75+ serialized ,
76+ expectedNames ,
77+ expectedValues ,
78+ ] of validExpressions ) {
79+ const attributes = new ExpressionAttributes ( ) ;
80+ expect ( expression . serialize ( attributes ) ) . toBe ( serialized ) ;
81+ expect ( attributes . names ) . toEqual ( expectedNames ) ;
82+ expect ( attributes . values ) . toEqual ( expectedValues ) ;
83+ }
5484 } ) ;
5585 } ) ;
5686} ) ;
0 commit comments