Skip to content

chore(test): add additional tests improving test coverage #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions test/db/00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
);
6 changes: 6 additions & 0 deletions test/db/01-dummy-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
105 changes: 105 additions & 0 deletions test/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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(`
Expand Down Expand Up @@ -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')
Expand Down
31 changes: 31 additions & 0 deletions test/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down