Skip to content

Commit 230fe40

Browse files
committed
test: materialized views
1 parent 9e6431d commit 230fe40

File tree

5 files changed

+133
-32
lines changed

5 files changed

+133
-32
lines changed

test/db/00-init.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ $$ language plpgsql;
5252

5353
CREATE VIEW todos_view AS SELECT * FROM public.todos;
5454

55+
create materialized view todos_matview as select * from public.todos;
56+
5557
create function public.blurb(public.todos) returns text as
5658
$$
5759
select substring($1.details, 1, 3);

test/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ import './lib/publications'
1616
import './lib/triggers'
1717
import './lib/views'
1818
import './lib/foreign-tables'
19+
import './server/materialized-views'
1920
import './server/typegen'

test/lib/foreign-tables.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import { pgMeta } from './utils'
22

3+
const cleanNondetFromResponse = <T>(x: T) => {
4+
const { data, ...rest } = x as any
5+
6+
const cleanNondetFromData = ({ id, columns, ...rest }: any) => {
7+
const cleaned = rest
8+
if (columns) {
9+
cleaned.columns = columns.map(({ id, table_id, ...rest }: any) => rest)
10+
}
11+
return cleaned
12+
}
13+
14+
return {
15+
data: Array.isArray(data) ? data.map(cleanNondetFromData) : cleanNondetFromData(data),
16+
...rest,
17+
} as T
18+
}
19+
320
test('list', async () => {
421
const res = await pgMeta.foreignTables.list()
5-
expect(res.data?.find(({ name }) => name === 'foreign_table')).toMatchInlineSnapshot(
6-
{ id: expect.any(Number) },
7-
`
22+
expect(cleanNondetFromResponse(res).data?.find(({ name }) => name === 'foreign_table'))
23+
.toMatchInlineSnapshot(`
824
{
925
"columns": [
1026
{
@@ -13,7 +29,6 @@ test('list', async () => {
1329
"default_value": null,
1430
"enums": [],
1531
"format": "int8",
16-
"id": "16434.1",
1732
"identity_generation": null,
1833
"is_generated": false,
1934
"is_identity": false,
@@ -24,15 +39,13 @@ test('list', async () => {
2439
"ordinal_position": 1,
2540
"schema": "public",
2641
"table": "foreign_table",
27-
"table_id": 16434,
2842
},
2943
{
3044
"comment": null,
3145
"data_type": "text",
3246
"default_value": null,
3347
"enums": [],
3448
"format": "text",
35-
"id": "16434.2",
3649
"identity_generation": null,
3750
"is_generated": false,
3851
"is_identity": false,
@@ -43,7 +56,6 @@ test('list', async () => {
4356
"ordinal_position": 2,
4457
"schema": "public",
4558
"table": "foreign_table",
46-
"table_id": 16434,
4759
},
4860
{
4961
"comment": null,
@@ -54,7 +66,6 @@ test('list', async () => {
5466
"INACTIVE",
5567
],
5668
"format": "user_status",
57-
"id": "16434.3",
5869
"identity_generation": null,
5970
"is_generated": false,
6071
"is_identity": false,
@@ -65,40 +76,30 @@ test('list', async () => {
6576
"ordinal_position": 3,
6677
"schema": "public",
6778
"table": "foreign_table",
68-
"table_id": 16434,
6979
},
7080
],
7181
"comment": null,
72-
"id": Any<Number>,
7382
"name": "foreign_table",
7483
"schema": "public",
7584
}
76-
`
77-
)
85+
`)
7886
})
7987

8088
test('list without columns', async () => {
8189
const res = await pgMeta.foreignTables.list({ includeColumns: false })
82-
expect(res.data?.find(({ name }) => name === 'foreign_table')).toMatchInlineSnapshot(
83-
{
84-
id: expect.any(Number),
85-
},
86-
`
90+
expect(cleanNondetFromResponse(res).data?.find(({ name }) => name === 'foreign_table'))
91+
.toMatchInlineSnapshot(`
8792
{
8893
"comment": null,
89-
"id": Any<Number>,
9094
"name": "foreign_table",
9195
"schema": "public",
9296
}
93-
`
94-
)
97+
`)
9598
})
9699

97100
test('retrieve', async () => {
98101
const res = await pgMeta.foreignTables.retrieve({ schema: 'public', name: 'foreign_table' })
99-
expect(res).toMatchInlineSnapshot(
100-
{ data: { id: expect.any(Number) } },
101-
`
102+
expect(cleanNondetFromResponse(res)).toMatchInlineSnapshot(`
102103
{
103104
"data": {
104105
"columns": [
@@ -108,7 +109,6 @@ test('retrieve', async () => {
108109
"default_value": null,
109110
"enums": [],
110111
"format": "int8",
111-
"id": "16434.1",
112112
"identity_generation": null,
113113
"is_generated": false,
114114
"is_identity": false,
@@ -119,15 +119,13 @@ test('retrieve', async () => {
119119
"ordinal_position": 1,
120120
"schema": "public",
121121
"table": "foreign_table",
122-
"table_id": 16434,
123122
},
124123
{
125124
"comment": null,
126125
"data_type": "text",
127126
"default_value": null,
128127
"enums": [],
129128
"format": "text",
130-
"id": "16434.2",
131129
"identity_generation": null,
132130
"is_generated": false,
133131
"is_identity": false,
@@ -138,7 +136,6 @@ test('retrieve', async () => {
138136
"ordinal_position": 2,
139137
"schema": "public",
140138
"table": "foreign_table",
141-
"table_id": 16434,
142139
},
143140
{
144141
"comment": null,
@@ -149,7 +146,6 @@ test('retrieve', async () => {
149146
"INACTIVE",
150147
],
151148
"format": "user_status",
152-
"id": "16434.3",
153149
"identity_generation": null,
154150
"is_generated": false,
155151
"is_identity": false,
@@ -160,16 +156,13 @@ test('retrieve', async () => {
160156
"ordinal_position": 3,
161157
"schema": "public",
162158
"table": "foreign_table",
163-
"table_id": 16434,
164159
},
165160
],
166161
"comment": null,
167-
"id": Any<Number>,
168162
"name": "foreign_table",
169163
"schema": "public",
170164
},
171165
"error": null,
172166
}
173-
`
174-
)
167+
`)
175168
})

test/server/materialized-views.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { app } from './utils'
2+
3+
const cleanNondetFromBody = <T>(x: T) => {
4+
const cleanNondet = ({ id, columns, ...rest }: any) => {
5+
const cleaned = rest
6+
if (columns) {
7+
cleaned.columns = columns.map(({ id, table_id, ...rest }: any) => rest)
8+
}
9+
return cleaned
10+
}
11+
12+
return (Array.isArray(x) ? x.map(cleanNondet) : cleanNondet(x)) as T
13+
}
14+
15+
test('materialized views', async () => {
16+
const { body } = await app.inject({ method: 'GET', path: '/materialized-views' })
17+
expect(cleanNondetFromBody(JSON.parse(body))).toMatchInlineSnapshot(`
18+
[
19+
{
20+
"comment": null,
21+
"is_populated": true,
22+
"name": "todos_matview",
23+
"schema": "public",
24+
},
25+
]
26+
`)
27+
})
28+
29+
test('materialized views with columns', async () => {
30+
const { body } = await app.inject({
31+
method: 'GET',
32+
path: '/materialized-views',
33+
query: { include_columns: 'true' },
34+
})
35+
expect(cleanNondetFromBody(JSON.parse(body))).toMatchInlineSnapshot(`
36+
[
37+
{
38+
"columns": [
39+
{
40+
"comment": null,
41+
"data_type": "bigint",
42+
"default_value": null,
43+
"enums": [],
44+
"format": "int8",
45+
"identity_generation": null,
46+
"is_generated": false,
47+
"is_identity": false,
48+
"is_nullable": true,
49+
"is_unique": false,
50+
"is_updatable": false,
51+
"name": "id",
52+
"ordinal_position": 1,
53+
"schema": "public",
54+
"table": "todos_matview",
55+
},
56+
{
57+
"comment": null,
58+
"data_type": "text",
59+
"default_value": null,
60+
"enums": [],
61+
"format": "text",
62+
"identity_generation": null,
63+
"is_generated": false,
64+
"is_identity": false,
65+
"is_nullable": true,
66+
"is_unique": false,
67+
"is_updatable": false,
68+
"name": "details",
69+
"ordinal_position": 2,
70+
"schema": "public",
71+
"table": "todos_matview",
72+
},
73+
{
74+
"comment": null,
75+
"data_type": "bigint",
76+
"default_value": null,
77+
"enums": [],
78+
"format": "int8",
79+
"identity_generation": null,
80+
"is_generated": false,
81+
"is_identity": false,
82+
"is_nullable": true,
83+
"is_unique": false,
84+
"is_updatable": false,
85+
"name": "user-id",
86+
"ordinal_position": 3,
87+
"schema": "public",
88+
"table": "todos_matview",
89+
},
90+
],
91+
"comment": null,
92+
"is_populated": true,
93+
"name": "todos_matview",
94+
"schema": "public",
95+
},
96+
]
97+
`)
98+
})

test/server/typegen.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ test('typegen', async () => {
114114
}
115115
}
116116
Views: {
117+
todos_matview: {
118+
Row: {
119+
details: string | null
120+
id: number | null
121+
"user-id": number | null
122+
}
123+
}
117124
todos_view: {
118125
Row: {
119126
details: string | null

0 commit comments

Comments
 (0)