Skip to content

Commit 633991c

Browse files
authored
chore(test): add additional tests improving test coverage (#595)
1 parent ba82f93 commit 633991c

File tree

6 files changed

+245
-0
lines changed

6 files changed

+245
-0
lines changed

test/basic.ts

+78
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,62 @@ test('basic select table', async () => {
6060
`)
6161
})
6262

63+
test('basic select returns types override', async () => {
64+
const res = await postgrest.from('users').select().returns<{ status: 'ONLINE' | 'OFFLINE' }>()
65+
expect(res).toMatchInlineSnapshot(`
66+
Object {
67+
"count": null,
68+
"data": Array [
69+
Object {
70+
"age_range": "[1,2)",
71+
"catchphrase": "'cat' 'fat'",
72+
"data": null,
73+
"status": "ONLINE",
74+
"username": "supabot",
75+
},
76+
Object {
77+
"age_range": "[25,35)",
78+
"catchphrase": "'bat' 'cat'",
79+
"data": null,
80+
"status": "OFFLINE",
81+
"username": "kiwicopple",
82+
},
83+
Object {
84+
"age_range": "[25,35)",
85+
"catchphrase": "'bat' 'rat'",
86+
"data": null,
87+
"status": "ONLINE",
88+
"username": "awailas",
89+
},
90+
Object {
91+
"age_range": "[20,30)",
92+
"catchphrase": "'fat' 'rat'",
93+
"data": null,
94+
"status": "ONLINE",
95+
"username": "dragarcia",
96+
},
97+
Object {
98+
"age_range": "[20,30)",
99+
"catchphrase": "'json' 'test'",
100+
"data": Object {
101+
"foo": Object {
102+
"bar": Object {
103+
"nested": "value",
104+
},
105+
"baz": "string value",
106+
},
107+
},
108+
"status": "ONLINE",
109+
"username": "jsonuser",
110+
},
111+
],
112+
"error": null,
113+
"status": 200,
114+
"statusText": "OK",
115+
}
116+
`)
117+
})
118+
63119
test('basic select view', async () => {
64120
const res = await postgrest.from('updatable_view').select()
65121
expect(res).toMatchInlineSnapshot(`
@@ -546,6 +602,28 @@ describe('basic insert, update, delete', () => {
546602
`)
547603
})
548604

605+
test('insert quoted column', async () => {
606+
let res = await postgrest
607+
.from('cornercase')
608+
.insert([{ 'column whitespace': 'foo', id: 1 }])
609+
.select('"column whitespace", id ')
610+
611+
expect(res).toMatchInlineSnapshot(`
612+
Object {
613+
"count": null,
614+
"data": null,
615+
"error": Object {
616+
"code": "23505",
617+
"details": "Key (id)=(1) already exists.",
618+
"hint": null,
619+
"message": "duplicate key value violates unique constraint \\"cornercase_pkey\\"",
620+
},
621+
"status": 409,
622+
"statusText": "Conflict",
623+
}
624+
`)
625+
})
626+
549627
test('basic update', async () => {
550628
let res = await postgrest
551629
.from('messages')

test/db/00-schema.sql

+7
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,10 @@ $$ language sql immutable;
156156

157157
create function public.function_with_array_param(param uuid[])
158158
returns void as '' language sql immutable;
159+
160+
161+
create table public.cornercase (
162+
id int primary key,
163+
"column whitespace" text,
164+
array_column text[]
165+
);

test/db/01-dummy-data.sql

+6
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ VALUES
8181
(2, 1), -- Smartphone is in Electronics
8282
(3, 1), -- Headphones are in Electronics
8383
(3, 3); -- Headphones are also in Audio
84+
85+
INSERT INTO public.cornercase (id, array_column)
86+
VALUES
87+
(1, ARRAY['test', 'one']),
88+
(2, ARRAY['another']),
89+
(3, ARRAY['test2']);

test/filters.ts

+105
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,56 @@ test('contains', async () => {
370370
`)
371371
})
372372

373+
test('contains with json', async () => {
374+
const res = await postgrest
375+
.from('users')
376+
.select('data')
377+
.contains('data', { foo: { baz: 'string value' } })
378+
expect(res).toMatchInlineSnapshot(`
379+
Object {
380+
"count": null,
381+
"data": Array [
382+
Object {
383+
"data": Object {
384+
"foo": Object {
385+
"bar": Object {
386+
"nested": "value",
387+
},
388+
"baz": "string value",
389+
},
390+
},
391+
},
392+
],
393+
"error": null,
394+
"status": 200,
395+
"statusText": "OK",
396+
}
397+
`)
398+
})
399+
400+
test('contains with array', async () => {
401+
const res = await postgrest
402+
.from('cornercase')
403+
.select('array_column')
404+
.contains('array_column', ['test'])
405+
expect(res).toMatchInlineSnapshot(`
406+
Object {
407+
"count": null,
408+
"data": Array [
409+
Object {
410+
"array_column": Array [
411+
"test",
412+
"one",
413+
],
414+
},
415+
],
416+
"error": null,
417+
"status": 200,
418+
"statusText": "OK",
419+
}
420+
`)
421+
})
422+
373423
test('containedBy', async () => {
374424
const res = await postgrest.from('users').select('age_range').containedBy('age_range', '[1,2)')
375425
expect(res).toMatchInlineSnapshot(`
@@ -387,6 +437,38 @@ test('containedBy', async () => {
387437
`)
388438
})
389439

440+
test('containedBy with json', async () => {
441+
const res = await postgrest
442+
.from('users')
443+
.select('data')
444+
.containedBy('data', { foo: { baz: 'string value' } })
445+
expect(res).toMatchInlineSnapshot(`
446+
Object {
447+
"count": null,
448+
"data": Array [],
449+
"error": null,
450+
"status": 200,
451+
"statusText": "OK",
452+
}
453+
`)
454+
})
455+
456+
test('containedBy with array', async () => {
457+
const res = await postgrest
458+
.from('cornercase')
459+
.select('array_column')
460+
.containedBy('array_column', ['test'])
461+
expect(res).toMatchInlineSnapshot(`
462+
Object {
463+
"count": null,
464+
"data": Array [],
465+
"error": null,
466+
"status": 200,
467+
"statusText": "OK",
468+
}
469+
`)
470+
})
471+
390472
test('rangeLt', async () => {
391473
const res = await postgrest.from('users').select('age_range').rangeLt('age_range', '[2,25)')
392474
expect(res).toMatchInlineSnapshot(`
@@ -510,6 +592,29 @@ test('overlaps', async () => {
510592
`)
511593
})
512594

595+
test('overlaps with array', async () => {
596+
const res = await postgrest
597+
.from('cornercase')
598+
.select('array_column')
599+
.overlaps('array_column', ['test'])
600+
expect(res).toMatchInlineSnapshot(`
601+
Object {
602+
"count": null,
603+
"data": Array [
604+
Object {
605+
"array_column": Array [
606+
"test",
607+
"one",
608+
],
609+
},
610+
],
611+
"error": null,
612+
"status": 200,
613+
"statusText": "OK",
614+
}
615+
`)
616+
})
617+
513618
test('textSearch', async () => {
514619
const res = await postgrest
515620
.from('users')

test/transforms.ts

+31
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,37 @@ test('csv', async () => {
294294
`)
295295
})
296296

297+
test('geojson', async () => {
298+
const res = await postgrest.from('shops').select().geojson()
299+
expect(res).toMatchInlineSnapshot(`
300+
Object {
301+
"count": null,
302+
"data": Object {
303+
"features": Array [
304+
Object {
305+
"geometry": Object {
306+
"coordinates": Array [
307+
-71.10044,
308+
42.373695,
309+
],
310+
"type": "Point",
311+
},
312+
"properties": Object {
313+
"address": "1369 Cambridge St",
314+
"id": 1,
315+
},
316+
"type": "Feature",
317+
},
318+
],
319+
"type": "FeatureCollection",
320+
},
321+
"error": null,
322+
"status": 200,
323+
"statusText": "OK",
324+
}
325+
`)
326+
})
327+
297328
test('abort signal', async () => {
298329
const ac = new AbortController() as globalThis.AbortController
299330
ac.abort()

test/types.ts

+18
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,24 @@ export type Database = {
400400
}
401401
Relationships: []
402402
}
403+
cornercase: {
404+
Row: {
405+
'column whitespace': string | null
406+
array_column: unknown | null
407+
id: number
408+
}
409+
Insert: {
410+
'column whitespace'?: string | null
411+
array_column?: unknown | null
412+
id: number
413+
}
414+
Update: {
415+
'column whitespace'?: string | null
416+
array_column?: unknown | null
417+
id?: number
418+
}
419+
Relationships: []
420+
}
403421
users: {
404422
Row: {
405423
age_range: unknown | null

0 commit comments

Comments
 (0)