@@ -7,11 +7,14 @@ import {
7
7
greaterThanOrEqualTo ,
8
8
between ,
9
9
inList ,
10
+ isConditionExpression ,
10
11
isConditionExpressionPredicate ,
12
+ isConditionExpressionSubject ,
11
13
serializeConditionExpression ,
12
14
} from "./ConditionExpression" ;
13
15
import { ExpressionAttributes } from "./ExpressionAttributes" ;
14
16
import { AttributePath } from "./AttributePath" ;
17
+ import { FunctionExpression } from "./FunctionExpression" ;
15
18
16
19
describe ( 'equals' , ( ) => {
17
20
it ( 'should return an equality condition predicate' , ( ) => {
@@ -89,6 +92,154 @@ describe('inList', () => {
89
92
} ) ;
90
93
} ) ;
91
94
95
+ describe ( 'isConditionExpressionPredicate' , ( ) => {
96
+ it ( 'should return true for a valid predicate' , ( ) => {
97
+ expect ( isConditionExpressionPredicate ( { type : 'Equals' , object : 0 } ) )
98
+ . toBe ( true ) ;
99
+ } ) ;
100
+
101
+ it ( 'should reject non-matching values' , ( ) => {
102
+ for ( const notPredicate of [
103
+ false ,
104
+ true ,
105
+ null ,
106
+ void 0 ,
107
+ 'string' ,
108
+ 123 ,
109
+ [ ] ,
110
+ { } ,
111
+ new Uint8Array ( 12 ) ,
112
+ { foo : 'bar' } ,
113
+ { name : 'foo' , arguments : 'bar' } ,
114
+ { S : 'string' }
115
+ ] ) {
116
+ expect ( isConditionExpressionPredicate ( notPredicate ) ) . toBe ( false ) ;
117
+ }
118
+ } ) ;
119
+ } ) ;
120
+
121
+ describe ( 'isConditionExpressionSubject' , ( ) => {
122
+ it ( 'should return true for a string subject' , ( ) => {
123
+ expect ( isConditionExpressionSubject ( { subject : 'foo' } ) ) . toBe ( true ) ;
124
+ } ) ;
125
+
126
+ it ( 'should return true for an AttributePath subject' , ( ) => {
127
+ expect ( isConditionExpressionSubject ( {
128
+ subject : new AttributePath ( 'foo.bar[3]' ) ,
129
+ } ) ) . toBe ( true ) ;
130
+ } ) ;
131
+
132
+ it ( 'should reject non-matching values' , ( ) => {
133
+ for ( const notSubject of [
134
+ false ,
135
+ true ,
136
+ null ,
137
+ void 0 ,
138
+ 'string' ,
139
+ 123 ,
140
+ [ ] ,
141
+ { } ,
142
+ new Uint8Array ( 12 ) ,
143
+ { foo : 'bar' } ,
144
+ { name : 'foo' , arguments : 'bar' } ,
145
+ { S : 'string' } ,
146
+ { subject : 123 } ,
147
+ ] ) {
148
+ expect ( isConditionExpressionSubject ( notSubject ) ) . toBe ( false ) ;
149
+ }
150
+ } ) ;
151
+ } ) ;
152
+
153
+ describe ( 'isConditionExpression' , ( ) => {
154
+ it ( 'should return true for valid expressions' , ( ) => {
155
+ expect ( isConditionExpression ( {
156
+ type : 'Equals' ,
157
+ subject : 'foo' ,
158
+ object : 'bar' ,
159
+ } ) ) . toBe ( true ) ;
160
+ } ) ;
161
+
162
+ it ( 'should return true for function expressions' , ( ) => {
163
+ expect ( isConditionExpression (
164
+ new FunctionExpression ( 'attribute_not_exists' , 'foo' )
165
+ ) ) . toBe ( true ) ;
166
+ } ) ;
167
+
168
+ it ( 'should return true for negation expressions' , ( ) => {
169
+ expect ( isConditionExpression ( {
170
+ type : 'Not' ,
171
+ condition : {
172
+ type : 'Between' ,
173
+ subject : 'foo' ,
174
+ lowerBound : 100 ,
175
+ upperBound : 200 ,
176
+ }
177
+ } ) ) . toBe ( true ) ;
178
+ } ) ;
179
+
180
+ it ( 'should return true for compound expressions' , ( ) => {
181
+ for ( const type of [ 'And' , 'Or' ] ) {
182
+ expect ( isConditionExpression ( {
183
+ type,
184
+ conditions : [
185
+ {
186
+ type : 'Between' ,
187
+ subject : 'foo' ,
188
+ lowerBound : 100 ,
189
+ upperBound : 200 ,
190
+ } ,
191
+ {
192
+ type : 'Between' ,
193
+ subject : 'foo' ,
194
+ lowerBound : 400 ,
195
+ upperBound : 600 ,
196
+ } ,
197
+ ]
198
+ } ) ) . toBe ( true ) ;
199
+ }
200
+ } ) ;
201
+
202
+ it ( 'should reject compound expressions without a conditions list' , ( ) => {
203
+ for ( const type of [ 'And' , 'Or' ] ) {
204
+ expect ( isConditionExpression ( { type} ) ) . toBe ( false ) ;
205
+ }
206
+ } ) ;
207
+
208
+ it (
209
+ 'should reject compound expressions whose list contains invalid members' ,
210
+ ( ) => {
211
+
212
+ for ( const type of [ 'And' , 'Or' ] ) {
213
+ expect ( isConditionExpression ( {
214
+ type,
215
+ conditions : [ 'foo' , 123 ] ,
216
+ } ) ) . toBe ( false ) ;
217
+ }
218
+ }
219
+ ) ;
220
+
221
+ it ( 'should reject non-matching values' , ( ) => {
222
+ for ( const notExpression of [
223
+ false ,
224
+ true ,
225
+ null ,
226
+ void 0 ,
227
+ 'string' ,
228
+ 123 ,
229
+ [ ] ,
230
+ { } ,
231
+ new Uint8Array ( 12 ) ,
232
+ { foo : 'bar' } ,
233
+ { name : 'foo' , arguments : 'bar' } ,
234
+ { S : 'string' } ,
235
+ { subject : 'foo' , object : 'bar' } ,
236
+ { type : 'UnknownType' , subject : 'foo' , object : 'bar' } ,
237
+ ] ) {
238
+ expect ( isConditionExpression ( notExpression ) ) . toBe ( false ) ;
239
+ }
240
+ } ) ;
241
+ } ) ;
242
+
92
243
describe ( 'serializeConditionExpression' , ( ) => {
93
244
it ( 'should serialize equality expressions' , ( ) => {
94
245
const attributes = new ExpressionAttributes ( ) ;
@@ -320,13 +471,11 @@ describe('serializeConditionExpression', () => {
320
471
it ( 'should serialize function expressions' , ( ) => {
321
472
const attributes = new ExpressionAttributes ( ) ;
322
473
const serialized = serializeConditionExpression (
323
- {
324
- name : 'attribute_type' ,
325
- arguments : [
326
- new AttributePath ( 'foo' ) ,
327
- 'S'
328
- ]
329
- } ,
474
+ new FunctionExpression (
475
+ 'attribute_type' ,
476
+ new AttributePath ( 'foo' ) ,
477
+ 'S'
478
+ ) ,
330
479
attributes
331
480
) ;
332
481
0 commit comments