Skip to content

Commit

Permalink
test: materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Mar 29, 2023
1 parent 9e6431d commit 230fe40
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 32 deletions.
2 changes: 2 additions & 0 deletions test/db/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ $$ language plpgsql;

CREATE VIEW todos_view AS SELECT * FROM public.todos;

create materialized view todos_matview as select * from public.todos;

create function public.blurb(public.todos) returns text as
$$
select substring($1.details, 1, 3);
Expand Down
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ import './lib/publications'
import './lib/triggers'
import './lib/views'
import './lib/foreign-tables'
import './server/materialized-views'
import './server/typegen'
57 changes: 25 additions & 32 deletions test/lib/foreign-tables.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { pgMeta } from './utils'

const cleanNondetFromResponse = <T>(x: T) => {
const { data, ...rest } = x as any

const cleanNondetFromData = ({ id, columns, ...rest }: any) => {
const cleaned = rest
if (columns) {
cleaned.columns = columns.map(({ id, table_id, ...rest }: any) => rest)
}
return cleaned
}

return {
data: Array.isArray(data) ? data.map(cleanNondetFromData) : cleanNondetFromData(data),
...rest,
} as T
}

test('list', async () => {
const res = await pgMeta.foreignTables.list()
expect(res.data?.find(({ name }) => name === 'foreign_table')).toMatchInlineSnapshot(
{ id: expect.any(Number) },
`
expect(cleanNondetFromResponse(res).data?.find(({ name }) => name === 'foreign_table'))
.toMatchInlineSnapshot(`
{
"columns": [
{
Expand All @@ -13,7 +29,6 @@ test('list', async () => {
"default_value": null,
"enums": [],
"format": "int8",
"id": "16434.1",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -24,15 +39,13 @@ test('list', async () => {
"ordinal_position": 1,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
{
"comment": null,
"data_type": "text",
"default_value": null,
"enums": [],
"format": "text",
"id": "16434.2",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -43,7 +56,6 @@ test('list', async () => {
"ordinal_position": 2,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
{
"comment": null,
Expand All @@ -54,7 +66,6 @@ test('list', async () => {
"INACTIVE",
],
"format": "user_status",
"id": "16434.3",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -65,40 +76,30 @@ test('list', async () => {
"ordinal_position": 3,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
],
"comment": null,
"id": Any<Number>,
"name": "foreign_table",
"schema": "public",
}
`
)
`)
})

test('list without columns', async () => {
const res = await pgMeta.foreignTables.list({ includeColumns: false })
expect(res.data?.find(({ name }) => name === 'foreign_table')).toMatchInlineSnapshot(
{
id: expect.any(Number),
},
`
expect(cleanNondetFromResponse(res).data?.find(({ name }) => name === 'foreign_table'))
.toMatchInlineSnapshot(`
{
"comment": null,
"id": Any<Number>,
"name": "foreign_table",
"schema": "public",
}
`
)
`)
})

test('retrieve', async () => {
const res = await pgMeta.foreignTables.retrieve({ schema: 'public', name: 'foreign_table' })
expect(res).toMatchInlineSnapshot(
{ data: { id: expect.any(Number) } },
`
expect(cleanNondetFromResponse(res)).toMatchInlineSnapshot(`
{
"data": {
"columns": [
Expand All @@ -108,7 +109,6 @@ test('retrieve', async () => {
"default_value": null,
"enums": [],
"format": "int8",
"id": "16434.1",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -119,15 +119,13 @@ test('retrieve', async () => {
"ordinal_position": 1,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
{
"comment": null,
"data_type": "text",
"default_value": null,
"enums": [],
"format": "text",
"id": "16434.2",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -138,7 +136,6 @@ test('retrieve', async () => {
"ordinal_position": 2,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
{
"comment": null,
Expand All @@ -149,7 +146,6 @@ test('retrieve', async () => {
"INACTIVE",
],
"format": "user_status",
"id": "16434.3",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
Expand All @@ -160,16 +156,13 @@ test('retrieve', async () => {
"ordinal_position": 3,
"schema": "public",
"table": "foreign_table",
"table_id": 16434,
},
],
"comment": null,
"id": Any<Number>,
"name": "foreign_table",
"schema": "public",
},
"error": null,
}
`
)
`)
})
98 changes: 98 additions & 0 deletions test/server/materialized-views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { app } from './utils'

const cleanNondetFromBody = <T>(x: T) => {
const cleanNondet = ({ id, columns, ...rest }: any) => {
const cleaned = rest
if (columns) {
cleaned.columns = columns.map(({ id, table_id, ...rest }: any) => rest)
}
return cleaned
}

return (Array.isArray(x) ? x.map(cleanNondet) : cleanNondet(x)) as T
}

test('materialized views', async () => {
const { body } = await app.inject({ method: 'GET', path: '/materialized-views' })
expect(cleanNondetFromBody(JSON.parse(body))).toMatchInlineSnapshot(`
[
{
"comment": null,
"is_populated": true,
"name": "todos_matview",
"schema": "public",
},
]
`)
})

test('materialized views with columns', async () => {
const { body } = await app.inject({
method: 'GET',
path: '/materialized-views',
query: { include_columns: 'true' },
})
expect(cleanNondetFromBody(JSON.parse(body))).toMatchInlineSnapshot(`
[
{
"columns": [
{
"comment": null,
"data_type": "bigint",
"default_value": null,
"enums": [],
"format": "int8",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
"is_nullable": true,
"is_unique": false,
"is_updatable": false,
"name": "id",
"ordinal_position": 1,
"schema": "public",
"table": "todos_matview",
},
{
"comment": null,
"data_type": "text",
"default_value": null,
"enums": [],
"format": "text",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
"is_nullable": true,
"is_unique": false,
"is_updatable": false,
"name": "details",
"ordinal_position": 2,
"schema": "public",
"table": "todos_matview",
},
{
"comment": null,
"data_type": "bigint",
"default_value": null,
"enums": [],
"format": "int8",
"identity_generation": null,
"is_generated": false,
"is_identity": false,
"is_nullable": true,
"is_unique": false,
"is_updatable": false,
"name": "user-id",
"ordinal_position": 3,
"schema": "public",
"table": "todos_matview",
},
],
"comment": null,
"is_populated": true,
"name": "todos_matview",
"schema": "public",
},
]
`)
})
7 changes: 7 additions & 0 deletions test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ test('typegen', async () => {
}
}
Views: {
todos_matview: {
Row: {
details: string | null
id: number | null
"user-id": number | null
}
}
todos_view: {
Row: {
details: string | null
Expand Down

0 comments on commit 230fe40

Please sign in to comment.