From c2abed4237c184b3bdc9a2369b36496490475bca Mon Sep 17 00:00:00 2001 From: avallete Date: Thu, 16 Jan 2025 11:54:05 +0900 Subject: [PATCH] chore(test): add additional tests improving test coverage --- test/basic.ts | 78 ++++++++++++++++++++++++++++ test/db/00-schema.sql | 7 +++ test/db/01-dummy-data.sql | 6 +++ test/filters.ts | 105 ++++++++++++++++++++++++++++++++++++++ test/transforms.ts | 31 +++++++++++ test/types.ts | 18 +++++++ 6 files changed, 245 insertions(+) diff --git a/test/basic.ts b/test/basic.ts index 44daf354..5e578473 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -60,6 +60,62 @@ test('basic select table', async () => { `) }) +test('basic select returns types override', async () => { + const res = await postgrest.from('users').select().returns<{ status: 'ONLINE' | 'OFFLINE' }>() + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [ + Object { + "age_range": "[1,2)", + "catchphrase": "'cat' 'fat'", + "data": null, + "status": "ONLINE", + "username": "supabot", + }, + Object { + "age_range": "[25,35)", + "catchphrase": "'bat' 'cat'", + "data": null, + "status": "OFFLINE", + "username": "kiwicopple", + }, + Object { + "age_range": "[25,35)", + "catchphrase": "'bat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "awailas", + }, + Object { + "age_range": "[20,30)", + "catchphrase": "'fat' 'rat'", + "data": null, + "status": "ONLINE", + "username": "dragarcia", + }, + Object { + "age_range": "[20,30)", + "catchphrase": "'json' 'test'", + "data": Object { + "foo": Object { + "bar": Object { + "nested": "value", + }, + "baz": "string value", + }, + }, + "status": "ONLINE", + "username": "jsonuser", + }, + ], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('basic select view', async () => { const res = await postgrest.from('updatable_view').select() expect(res).toMatchInlineSnapshot(` @@ -546,6 +602,28 @@ describe('basic insert, update, delete', () => { `) }) + test('insert quoted column', async () => { + let res = await postgrest + .from('cornercase') + .insert([{ 'column whitespace': 'foo', id: 1 }]) + .select('"column whitespace", id ') + + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": null, + "error": Object { + "code": "23505", + "details": "Key (id)=(1) already exists.", + "hint": null, + "message": "duplicate key value violates unique constraint \\"cornercase_pkey\\"", + }, + "status": 409, + "statusText": "Conflict", + } + `) + }) + test('basic update', async () => { let res = await postgrest .from('messages') diff --git a/test/db/00-schema.sql b/test/db/00-schema.sql index 07170703..ee3f6e2e 100644 --- a/test/db/00-schema.sql +++ b/test/db/00-schema.sql @@ -156,3 +156,10 @@ $$ language sql immutable; create function public.function_with_array_param(param uuid[]) returns void as '' language sql immutable; + + +create table public.cornercase ( + id int primary key, + "column whitespace" text, + array_column text[] +); diff --git a/test/db/01-dummy-data.sql b/test/db/01-dummy-data.sql index 8a2343eb..1ad59ca8 100644 --- a/test/db/01-dummy-data.sql +++ b/test/db/01-dummy-data.sql @@ -81,3 +81,9 @@ VALUES (2, 1), -- Smartphone is in Electronics (3, 1), -- Headphones are in Electronics (3, 3); -- Headphones are also in Audio + +INSERT INTO public.cornercase (id, array_column) +VALUES + (1, ARRAY['test', 'one']), + (2, ARRAY['another']), + (3, ARRAY['test2']); \ No newline at end of file diff --git a/test/filters.ts b/test/filters.ts index 8348f0a4..3338ec37 100644 --- a/test/filters.ts +++ b/test/filters.ts @@ -370,6 +370,56 @@ test('contains', async () => { `) }) +test('contains with json', async () => { + const res = await postgrest + .from('users') + .select('data') + .contains('data', { foo: { baz: 'string value' } }) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [ + Object { + "data": Object { + "foo": Object { + "bar": Object { + "nested": "value", + }, + "baz": "string value", + }, + }, + }, + ], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + +test('contains with array', async () => { + const res = await postgrest + .from('cornercase') + .select('array_column') + .contains('array_column', ['test']) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [ + Object { + "array_column": Array [ + "test", + "one", + ], + }, + ], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('containedBy', async () => { const res = await postgrest.from('users').select('age_range').containedBy('age_range', '[1,2)') expect(res).toMatchInlineSnapshot(` @@ -387,6 +437,38 @@ test('containedBy', async () => { `) }) +test('containedBy with json', async () => { + const res = await postgrest + .from('users') + .select('data') + .containedBy('data', { foo: { baz: 'string value' } }) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + +test('containedBy with array', async () => { + const res = await postgrest + .from('cornercase') + .select('array_column') + .containedBy('array_column', ['test']) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('rangeLt', async () => { const res = await postgrest.from('users').select('age_range').rangeLt('age_range', '[2,25)') expect(res).toMatchInlineSnapshot(` @@ -510,6 +592,29 @@ test('overlaps', async () => { `) }) +test('overlaps with array', async () => { + const res = await postgrest + .from('cornercase') + .select('array_column') + .overlaps('array_column', ['test']) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Array [ + Object { + "array_column": Array [ + "test", + "one", + ], + }, + ], + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('textSearch', async () => { const res = await postgrest .from('users') diff --git a/test/transforms.ts b/test/transforms.ts index e6518910..10892d8f 100644 --- a/test/transforms.ts +++ b/test/transforms.ts @@ -294,6 +294,37 @@ test('csv', async () => { `) }) +test('geojson', async () => { + const res = await postgrest.from('shops').select().geojson() + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": Object { + "features": Array [ + Object { + "geometry": Object { + "coordinates": Array [ + -71.10044, + 42.373695, + ], + "type": "Point", + }, + "properties": Object { + "address": "1369 Cambridge St", + "id": 1, + }, + "type": "Feature", + }, + ], + "type": "FeatureCollection", + }, + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + test('abort signal', async () => { const ac = new AbortController() as globalThis.AbortController ac.abort() diff --git a/test/types.ts b/test/types.ts index 2ed1d1c4..8f84eced 100644 --- a/test/types.ts +++ b/test/types.ts @@ -400,6 +400,24 @@ export type Database = { } Relationships: [] } + cornercase: { + Row: { + 'column whitespace': string | null + array_column: unknown | null + id: number + } + Insert: { + 'column whitespace'?: string | null + array_column?: unknown | null + id: number + } + Update: { + 'column whitespace'?: string | null + array_column?: unknown | null + id?: number + } + Relationships: [] + } users: { Row: { age_range: unknown | null