@@ -14,7 +14,7 @@ describe('PostgrestClient', () => {
14
14
assert . deepEqual ( body , [ { id : 2 } ] )
15
15
} )
16
16
17
- it ( 'should return realtional joins' , async ( ) => {
17
+ it ( 'should return relational joins' , async ( ) => {
18
18
let client = new PostgrestClient ( rootUrl )
19
19
let { body } = await client
20
20
. from ( 'channels' )
@@ -25,37 +25,105 @@ describe('PostgrestClient', () => {
25
25
assert . equal ( true , hasCorrectMessages )
26
26
} )
27
27
28
- it ( 'should return be able to insert data' , async ( ) => {
28
+ it ( 'should be able to insert data' , async ( ) => {
29
29
let client = new PostgrestClient ( rootUrl )
30
30
let res = await client
31
31
. from ( 'messages' )
32
- . insert ( [ { message : 'Test message' , channel_id : 1 , user_id : 1 } ] )
32
+ . insert ( [ { message : 'Test message 0 ' , channel_id : 1 , user_id : 2 } ] )
33
33
assert . equal ( 201 , res . status )
34
34
} )
35
35
36
- it ( 'should insert data and return the object' , async ( ) => {
36
+ it ( 'should be able to insert data in the form of an object and return the object' , async ( ) => {
37
37
let client = new PostgrestClient ( rootUrl )
38
38
let res = await client
39
39
. from ( 'messages' )
40
- . insert ( [ { message : 'Test message' , channel_id : 1 , user_id : 1 } ] )
41
- assert . equal ( 'Test message' , res . body [ 0 ] . message )
40
+ . insert ( { message : 'Test message 1 ' , channel_id : 1 , user_id : 3 } )
41
+ assert . equal ( 'Test message 1 ' , res . body [ 0 ] . message )
42
42
} )
43
43
44
- it ( 'should return be able to update messages' , async ( ) => {
44
+ it ( 'should be able to insert an array of data and return the array' , async ( ) => {
45
+ let client = new PostgrestClient ( rootUrl )
46
+ let payload = [
47
+ { message : 'Test message 2' , channel_id : 1 , user_id : 4 } ,
48
+ { message : 'Test message 3' , channel_id : 1 , user_id : 4 } ,
49
+ ]
50
+
51
+ let res = await client . from ( 'messages' ) . insert ( payload )
52
+
53
+ assert . equal ( payload . length , res . body . length )
54
+ assert . equal ( 'Test message 2' , res . body [ 0 ] . message )
55
+ assert . equal ( 'Test message 3' , res . body [ 1 ] . message )
56
+ } )
57
+
58
+ it ( 'should not be able to update messages without any filters used' , async ( ) => {
45
59
let client = new PostgrestClient ( rootUrl )
46
60
let res = await client
47
61
. from ( 'messages' )
48
- . eq ( 'message' , 'Test message' )
49
- . update ( { message : 'Test message 2' , channel_id : 1 , user_id : 1 } )
50
- assert . equal ( 'Test message 2' , res . body [ 0 ] . message )
62
+ . update ( { message : 'Updated test message x' , channel_id : 1 , user_id : 2 } )
63
+
64
+ assert . equal ( 400 , res . status )
65
+ assert . equal ( '.update() cannot be invoked without any filters.' , res . statusText )
66
+ } )
67
+
68
+ it ( 'should not accept an array when updating messages' , async ( ) => {
69
+ let client = new PostgrestClient ( rootUrl )
70
+ let res = await client
71
+ . from ( 'messages' )
72
+ . gt ( 'user_id' , 2 )
73
+ . update ( [
74
+ { message : 'Updated test message xx' , channel_id : 1 , user_id : 4 } ,
75
+ { message : 'Updated test message xxx' , channel_id : 1 , user_id : 4 } ,
76
+ ] )
77
+
78
+ assert . equal ( 400 , res . status )
79
+ assert . equal ( 'Data type should be an object.' , res . statusText )
80
+ } )
81
+
82
+ it ( 'should be able to update messages' , async ( ) => {
83
+ let client = new PostgrestClient ( rootUrl )
84
+ let res = await client
85
+ . from ( 'messages' )
86
+ . eq ( 'message' , 'Test message 0' )
87
+ . update ( { message : 'Updated test message 1' , channel_id : 1 , user_id : 2 } )
88
+
89
+ assert . equal ( 200 , res . status )
90
+ assert . equal ( 'Updated test message 1' , res . body [ 0 ] . message )
51
91
} )
52
92
53
- it ( 'should return be able to delete messages' , async ( ) => {
93
+ it ( 'should be able to update multiple messages' , async ( ) => {
54
94
let client = new PostgrestClient ( rootUrl )
95
+
96
+ let readRes = await client
97
+ . from ( 'messages' )
98
+ . gt ( 'user_id' , 2 )
99
+ . select ( '*' )
100
+
55
101
let res = await client
56
102
. from ( 'messages' )
57
- . eq ( 'message' , 'Test message' )
103
+ . gt ( 'user_id' , 2 )
104
+ . update ( { message : 'Updated test message 2' } )
105
+
106
+ assert . equal ( readRes . body . length , res . body . length )
107
+ res . body . forEach ( item => {
108
+ assert . equal ( 'Updated test message 2' , item . message )
109
+ } )
110
+ } )
111
+
112
+ it ( 'should not be able to delete messages without any filters used' , async ( ) => {
113
+ let client = new PostgrestClient ( rootUrl )
114
+ let res = await client . from ( 'messages' ) . delete ( )
115
+
116
+ assert . equal ( 400 , res . status )
117
+ assert . equal ( '.delete() cannot be invoked without any filters.' , res . statusText )
118
+ } )
119
+
120
+ it ( 'should be able to delete messages when any form of filters are used' , async ( ) => {
121
+ let client = new PostgrestClient ( rootUrl )
122
+ let res = await client
123
+ . from ( 'messages' )
124
+ . not ( 'user_id' , 1 )
58
125
. delete ( )
126
+
59
127
assert . equal ( 204 , res . status )
60
128
} )
61
129
} )
0 commit comments