Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 03aad8d

Browse files
committed
Update Integration Test Suite
Now checks if: - .insert() can now either take in an array or object. - full body payload is returned for both successful .insert() & .update() functions. -
1 parent 274c03b commit 03aad8d

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

test/integration/PostgrestClient.test.js

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('PostgrestClient', () => {
1414
assert.deepEqual(body, [{ id: 2 }])
1515
})
1616

17-
it('should return realtional joins', async () => {
17+
it('should return relational joins', async () => {
1818
let client = new PostgrestClient(rootUrl)
1919
let { body } = await client
2020
.from('channels')
@@ -25,37 +25,105 @@ describe('PostgrestClient', () => {
2525
assert.equal(true, hasCorrectMessages)
2626
})
2727

28-
it('should return be able to insert data', async () => {
28+
it('should be able to insert data', async () => {
2929
let client = new PostgrestClient(rootUrl)
3030
let res = await client
3131
.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 }])
3333
assert.equal(201, res.status)
3434
})
3535

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 () => {
3737
let client = new PostgrestClient(rootUrl)
3838
let res = await client
3939
.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)
4242
})
4343

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 () => {
4559
let client = new PostgrestClient(rootUrl)
4660
let res = await client
4761
.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)
5191
})
5292

53-
it('should return be able to delete messages', async () => {
93+
it('should be able to update multiple messages', async () => {
5494
let client = new PostgrestClient(rootUrl)
95+
96+
let readRes = await client
97+
.from('messages')
98+
.gt('user_id', 2)
99+
.select('*')
100+
55101
let res = await client
56102
.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)
58125
.delete()
126+
59127
assert.equal(204, res.status)
60128
})
61129
})

0 commit comments

Comments
 (0)