1
1
2
2
import { expect } from 'chai' ;
3
- import fn from '../src/index' ;
4
- import dynamo from '../src/adapters/fn-dynamo' ;
3
+ import fn from '../../ src/index' ;
4
+ import dynamo from '../../ src/adapters/fn-dynamo' ;
5
5
let DynamoDB = require ( 'aws-sdk/clients/dynamodb' ) ;
6
6
7
7
let endpoint = process . env . ENDPOINT || 'http://localhost:4569/' ;
@@ -133,6 +133,7 @@ describe('table', () => {
133
133
expect ( res ) . to . be . an ( 'array' ) ;
134
134
expect ( res ) . to . have . lengthOf ( 3 ) ;
135
135
} ) ;
136
+
136
137
it ( 'If an id passed doesn\'t have an item in the table, an error will be returned' , ( ) => {
137
138
return fn . table ( 'departments' , '7' )
138
139
. then ( ( res : Department ) => {
@@ -200,28 +201,30 @@ describe('orderBy', () => {
200
201
} ) ;
201
202
202
203
it ( "If you pass more than one attribute, the result array should be sorted having in mind all the atributtes" , async ( ) => {
203
- const res1 = < Employee [ ] > await fn . table ( 'employees' )
204
+ const res = < Employee [ ] > await fn . table ( 'employees' )
204
205
. orderBy ( [ 'department' , 'id' ] , 'desc' )
205
206
. promise ( ) ;
206
- expect ( res1 ) . to . be . an ( 'array' ) ;
207
- expect ( res1 ) . to . have . lengthOf ( 4 ) ;
208
- let i = res1 . length ;
207
+ expect ( res ) . to . be . an ( 'array' ) ;
208
+ expect ( res ) . to . have . lengthOf ( 4 ) ;
209
+ let i = res . length ;
209
210
while ( -- i ) {
210
- expect ( res1 [ i ] . department <= res1 [ i - 1 ] . department ) . to . be . true ;
211
- if ( res1 [ i ] . department === res1 [ i - 1 ] . department )
212
- expect ( res1 [ i ] . id <= res1 [ i - 1 ] . id ) . to . be . true ;
211
+ expect ( res [ i ] . department <= res [ i - 1 ] . department ) . to . be . true ;
212
+ if ( res [ i ] . department === res [ i - 1 ] . department )
213
+ expect ( res [ i ] . id <= res [ i - 1 ] . id ) . to . be . true ;
213
214
}
214
-
215
- const res2 = < Employee [ ] > await fn . table ( 'employees' )
215
+ } ) ;
216
+
217
+ it ( "If you pass more than one attribute and more than one order, the result array should be sorted having in mind all the atributtes and all orders" , async ( ) => {
218
+ const res = < Employee [ ] > await fn . table ( 'employees' )
216
219
. orderBy ( [ 'department' , 'id' ] , [ 'desc' , 'asc' ] )
217
220
. promise ( ) ;
218
- expect ( res2 ) . to . be . an ( 'array' ) ;
219
- expect ( res2 ) . to . have . lengthOf ( 4 ) ;
220
- let j = res2 . length ;
221
+ expect ( res ) . to . be . an ( 'array' ) ;
222
+ expect ( res ) . to . have . lengthOf ( 4 ) ;
223
+ let j = res . length ;
221
224
while ( -- j ) {
222
- expect ( res2 [ j ] . department <= res2 [ j - 1 ] . department ) . to . be . true ;
223
- if ( res2 [ j ] . department === res2 [ j - 1 ] . department )
224
- expect ( res2 [ j ] . id >= res2 [ j - 1 ] . id ) . to . be . true ;
225
+ expect ( res [ j ] . department <= res [ j - 1 ] . department ) . to . be . true ;
226
+ if ( res [ j ] . department === res [ j - 1 ] . department )
227
+ expect ( res [ j ] . id >= res [ j - 1 ] . id ) . to . be . true ;
225
228
}
226
229
} ) ;
227
230
@@ -538,15 +541,17 @@ describe('insert', () => {
538
541
expect ( fn . insert ( 'departments' , [ { id : '7' , name : 'Sales' } , { id : '5' , name : 'Comercial' } ] ) ) . to . be . an ( 'object' ) ;
539
542
} ) ;
540
543
541
- it ( 'If the operation is succesful , the result is an id or an array of ids' , async ( ) => {
542
- const resArray = await fn . insert ( 'departments' , [ { id : '7' , name : 'Sales' } , { id : '5' , name : 'Comercial' } ] )
544
+ it ( 'If the operation is performed over an array of items , the result is an array of ids' , async ( ) => {
545
+ const res = await fn . insert ( 'departments' , [ { id : '7' , name : 'Sales' } , { id : '5' , name : 'Comercial' } ] )
543
546
. promise ( ) ;
544
- expect ( resArray ) . to . be . an ( 'array' ) ;
545
- expect ( resArray ) . to . have . lengthOf ( 2 ) ;
546
-
547
- const resObject = await fn . insert ( 'departments' , { id : '8' , name : 'Sales' } )
547
+ expect ( res ) . to . be . an ( 'array' ) ;
548
+ expect ( res ) . to . have . lengthOf ( 2 ) ;
549
+ } ) ;
550
+
551
+ it ( 'If the operation is performed over a single object, the result is an id' , async ( ) => {
552
+ const res = await fn . insert ( 'departments' , { id : '8' , name : 'Sales' } )
548
553
. promise ( ) ;
549
- expect ( resObject ) . to . be . a ( 'string' ) . equal ( '8' ) ;
554
+ expect ( res ) . to . be . a ( 'string' ) . equal ( '8' ) ;
550
555
} ) ;
551
556
552
557
it ( 'If the operation is not starter, the operation insert the items on which we are operating' , async ( ) => {
@@ -561,16 +566,18 @@ describe('insert', () => {
561
566
expect ( res ) . to . have . lengthOf ( 7 ) ;
562
567
} ) ;
563
568
564
- it ( 'If the operation it\'s done over an empty array , the result should be an empty array' , async ( ) => {
565
- const res1 = await fn . insert ( 'employees' , [ ] )
569
+ it ( 'If an empty array is passed to the operation , the result should be an empty array' , async ( ) => {
570
+ const res = await fn . insert ( 'employees' , [ ] )
566
571
. promise ( ) ;
567
- expect ( res1 ) . to . be . an ( 'array' ) . empty ;
572
+ expect ( res ) . to . be . an ( 'array' ) . empty ;
573
+ } ) ;
568
574
569
- const res2 = await fn . table ( 'employees' )
575
+ it ( 'If the operation is done over an empty array, the result should be an empty array' , async ( ) => {
576
+ const res = await fn . table ( 'employees' )
570
577
. where ( 'id' , 'x' , '=' )
571
578
. insert ( )
572
579
. promise ( ) ;
573
- expect ( res2 ) . to . be . an ( 'array' ) . empty ;
580
+ expect ( res ) . to . be . an ( 'array' ) . empty ;
574
581
} ) ;
575
582
576
583
it ( 'If the operation fail, the resolution should be an error' , ( ) => {
@@ -589,34 +596,40 @@ describe('delete', () => {
589
596
expect ( fn . delete ( 'employees' , '1' ) ) . to . be . an ( 'object' ) ;
590
597
} ) ;
591
598
592
- it ( 'If the operation is succesful , the result is an id or an array of ids' , async ( ) => {
593
- const resArray = await fn . delete ( 'departments' , [ '5' , '7' ] )
599
+ it ( 'If an array is passed to the operation , the result is an array of ids' , async ( ) => {
600
+ const res = await fn . delete ( 'departments' , [ '5' , '7' ] )
594
601
. promise ( ) ;
595
- expect ( resArray ) . to . be . an ( 'array' ) ;
596
- expect ( resArray ) . to . have . lengthOf ( 2 ) ;
597
- expect ( resArray ) . to . have . members ( [ '5' , '7' ] ) ;
598
-
599
- const resObject = await fn . delete ( 'departments' , '8' )
600
- . promise ( ) ;
601
- expect ( resObject ) . to . be . a ( 'string' ) . equals ( '8' ) ;
602
+ expect ( res ) . to . be . an ( 'array' ) ;
603
+ expect ( res ) . to . have . lengthOf ( 2 ) ;
604
+ expect ( res ) . to . have . members ( [ '5' , '7' ] ) ;
602
605
} ) ;
603
606
604
- it ( 'If the operation is not starter, the operation delete the items on which we are operating' , async ( ) => {
605
- const res1 = await fn . table ( 'departments' , '9' )
607
+ it ( 'If the operation is succesful, the result is an id' , async ( ) => {
608
+ const res = await fn . delete ( 'departments' , '8' )
609
+ . promise ( ) ;
610
+ expect ( res ) . to . be . a ( 'string' ) . equals ( '8' ) ;
611
+ } ) ;
612
+
613
+ it ( 'If the operation is not starter, and it\'s performed over a single element, the operation will delete that item' , async ( ) => {
614
+ const res = await fn . table ( 'departments' , '9' )
606
615
. project ( 'id' )
607
616
. delete ( )
608
617
. promise ( ) ;
609
- expect ( res1 ) . to . be . an ( 'string' ) . equal ( '9' ) ;
610
-
611
- const res2 = await fn . table ( 'departments' , [ '10' , '11' , '12' ] )
618
+ expect ( res ) . to . be . an ( 'string' ) . equal ( '9' ) ;
619
+ } ) ;
620
+
621
+ it ( 'If the operation is not starter, and it\'s performed over an array of elements, the operation will delete these items' , async ( ) => {
622
+ const res = await fn . table ( 'departments' , [ '10' , '11' , '12' ] )
612
623
. project ( 'id' )
613
624
. delete ( )
614
625
. promise ( ) ;
615
- expect ( res2 ) . to . be . an ( 'array' ) ;
616
- expect ( res2 ) . to . have . lengthOf ( 3 ) ;
617
- expect ( res2 ) . to . have . members ( [ '10' , '11' , '12' ] ) ;
618
-
619
- const res3 = await fn . table ( 'departments' , '13' )
626
+ expect ( res ) . to . be . an ( 'array' ) ;
627
+ expect ( res ) . to . have . lengthOf ( 3 ) ;
628
+ expect ( res ) . to . have . members ( [ '10' , '11' , '12' ] ) ;
629
+ } ) ;
630
+
631
+ it ( 'If the operation is not starter, it\'s possible to get the item to delete reducing the object which we want to delete' , async ( ) => {
632
+ const res = await fn . table ( 'departments' , '13' )
620
633
. reduce ( ( accum : string , o : string | number [ ] , key : string ) => {
621
634
if ( key === 'id' )
622
635
return o ;
@@ -625,31 +638,35 @@ describe('delete', () => {
625
638
} , '' )
626
639
. delete ( )
627
640
. promise ( ) ;
628
- expect ( res3 ) . to . be . an ( 'string' ) . equal ( '13' ) ;
629
-
630
- const res4 = await fn . table ( 'departments' )
641
+ expect ( res ) . to . be . an ( 'string' ) . equal ( '13' ) ;
642
+ } ) ;
643
+
644
+ it ( 'If the operation is not starter, it\'s possible to get the items to delete reducing an array of objects' , async ( ) => {
645
+ const res = await fn . table ( 'departments' )
631
646
. reduce ( ( accum : string [ ] , o : Department ) => {
632
647
if ( Number . parseInt ( o . id ) > 13 )
633
648
accum . push ( o . id )
634
649
return accum
635
650
} )
636
651
. delete ( )
637
652
. promise ( ) ;
638
- expect ( res4 ) . to . be . an ( 'array' ) ;
639
- expect ( res4 ) . to . have . lengthOf ( 2 ) ;
640
- expect ( res4 ) . to . have . members ( [ '15' , '16' ] ) ;
653
+ expect ( res ) . to . be . an ( 'array' ) ;
654
+ expect ( res ) . to . have . lengthOf ( 2 ) ;
655
+ expect ( res ) . to . have . members ( [ '15' , '16' ] ) ;
641
656
} ) ;
642
657
643
- it ( 'If the operation it\'s done over an empty array , the result should be an empty array' , async ( ) => {
644
- const res1 = await fn . delete ( 'employees' , [ ] )
658
+ it ( 'If an empty array is passed to the operation , the result should be an empty array' , async ( ) => {
659
+ const res = await fn . delete ( 'employees' , [ ] )
645
660
. promise ( ) ;
646
- expect ( res1 ) . to . be . an ( 'array' ) . empty ;
647
-
648
- const res2 = await fn . table ( 'employees' )
661
+ expect ( res ) . to . be . an ( 'array' ) . empty ;
662
+ } ) ;
663
+
664
+ it ( 'If the operation is done over an empty array, the result should be an empty array' , async ( ) => {
665
+ const res = await fn . table ( 'employees' )
649
666
. where ( 'id' , 'x' )
650
667
. delete ( )
651
668
. promise ( ) ;
652
- expect ( res2 ) . to . be . an ( 'array' ) . empty ;
669
+ expect ( res ) . to . be . an ( 'array' ) . empty ;
653
670
} ) ;
654
671
655
672
it ( 'If the operation is performed over an inexisten table, the resolution should be an error' , ( ) => {
0 commit comments