Skip to content

Commit cbf098a

Browse files
committed
test(postgresql): add tests for multi-schema scenario
1 parent 54f93e0 commit cbf098a

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

test/postgresql/01_unittest.sql

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,182 @@ SELECT (to_regclass('public.smoke_tbl_cloudsync') IS NOT NULL) AS init_create_ok
4747
SELECT (:fail::int + 1) AS fail \gset
4848
\endif
4949

50+
-- 'Test multi-schema table init (setup)'
51+
CREATE SCHEMA IF NOT EXISTS test_schema;
52+
DROP TABLE IF EXISTS public.repeated_table;
53+
DROP TABLE IF EXISTS test_schema.repeated_table;
54+
CREATE TABLE public.repeated_table (id TEXT PRIMARY KEY, data TEXT);
55+
CREATE TABLE test_schema.repeated_table (id TEXT PRIMARY KEY, data TEXT);
56+
\echo '[INFO] Created repeated_table in both public and test_schema'
57+
58+
-- 'Test init on table that exists in multiple schemas (default: public)'
59+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated \gset
60+
SELECT cloudsync_init('repeated_table', 'CLS', true) AS _init_repeated_public \gset
61+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NOT NULL) AS init_repeated_public_ok \gset
62+
\if :init_repeated_public_ok
63+
\echo '[PASS] Test init on repeated_table in public schema'
64+
\else
65+
\echo '[FAIL] Test init on repeated_table in public schema'
66+
SELECT (:fail::int + 1) AS fail \gset
67+
\endif
68+
69+
-- 'Test insert on repeated_table in public schema'
70+
SELECT cloudsync_uuid() AS repeated_id1 \gset
71+
INSERT INTO public.repeated_table (id, data) VALUES (:'repeated_id1', 'public_data');
72+
SELECT (COUNT(*) = 1) AS insert_repeated_public_ok
73+
FROM public.repeated_table_cloudsync
74+
WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id1']::text[])
75+
AND col_name = 'data' \gset
76+
\if :insert_repeated_public_ok
77+
\echo '[PASS] Test insert metadata on repeated_table in public'
78+
\else
79+
\echo '[FAIL] Test insert metadata on repeated_table in public'
80+
SELECT (:fail::int + 1) AS fail \gset
81+
\endif
82+
83+
-- 'Test cloudsync_changes view read for public.repeated_table'
84+
SELECT COUNT(*) AS changes_view_repeated_count
85+
FROM cloudsync_changes
86+
WHERE tbl = 'repeated_table' \gset
87+
SELECT COUNT(*) AS changes_meta_repeated_count
88+
FROM public.repeated_table_cloudsync \gset
89+
SELECT (:changes_view_repeated_count::int = :changes_meta_repeated_count::int) AS changes_read_repeated_ok \gset
90+
\if :changes_read_repeated_ok
91+
\echo '[PASS] Test cloudsync_changes view read for public.repeated_table'
92+
\else
93+
\echo '[FAIL] Test cloudsync_changes view read for public.repeated_table'
94+
SELECT (:fail::int + 1) AS fail \gset
95+
\endif
96+
97+
-- 'Test cloudsync_changes view write for public.repeated_table'
98+
SELECT cloudsync_uuid() AS repeated_id2 \gset
99+
INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
100+
VALUES (
101+
'repeated_table',
102+
cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id2']::text[]),
103+
'data',
104+
-- "public_write" encoded as cloudsync text value (type 0x0b + len 0x0c)
105+
decode('0b0c7075626c69635f7772697465', 'hex'),
106+
1,
107+
cloudsync_db_version_next(),
108+
cloudsync_siteid(),
109+
1,
110+
0
111+
);
112+
SELECT (COUNT(*) = 1) AS changes_write_repeated_ok
113+
FROM public.repeated_table
114+
WHERE id = :'repeated_id2' AND data = 'public_write' \gset
115+
\if :changes_write_repeated_ok
116+
\echo '[PASS] Test cloudsync_changes view write for public.repeated_table'
117+
\else
118+
\echo '[FAIL] Test cloudsync_changes view write for public.repeated_table'
119+
SELECT (:fail::int + 1) AS fail \gset
120+
\endif
121+
122+
-- 'Test cleanup on table with ambiguous name'
123+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated2 \gset
124+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NULL) AS cleanup_repeated_ok \gset
125+
\if :cleanup_repeated_ok
126+
\echo '[PASS] Test cleanup on repeated_table'
127+
\else
128+
\echo '[FAIL] Test cleanup on repeated_table'
129+
SELECT (:fail::int + 1) AS fail \gset
130+
\endif
131+
132+
-- 'Test cloudsync_set_schema and init on test_schema'
133+
SELECT cloudsync_set_schema('test_schema') AS _set_schema \gset
134+
SELECT cloudsync_init('repeated_table', 'CLS', true) AS _init_repeated_test_schema \gset
135+
SELECT (to_regclass('test_schema.repeated_table_cloudsync') IS NOT NULL) AS init_repeated_test_schema_ok \gset
136+
\if :init_repeated_test_schema_ok
137+
\echo '[PASS] Test init on repeated_table in test_schema'
138+
\else
139+
\echo '[FAIL] Test init on repeated_table in test_schema'
140+
SELECT (:fail::int + 1) AS fail \gset
141+
\endif
142+
143+
-- 'Test that public.repeated_table_cloudsync was not recreated'
144+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NULL) AS public_still_clean_ok \gset
145+
\if :public_still_clean_ok
146+
\echo '[PASS] Test public.repeated_table_cloudsync still cleaned up'
147+
\else
148+
\echo '[FAIL] Test public.repeated_table_cloudsync should not exist'
149+
SELECT (:fail::int + 1) AS fail \gset
150+
\endif
151+
152+
-- 'Test insert on repeated_table in test_schema'
153+
SELECT cloudsync_uuid() AS repeated_id3 \gset
154+
INSERT INTO test_schema.repeated_table (id, data) VALUES (:'repeated_id3', 'test_schema_data');
155+
SELECT (COUNT(*) = 1) AS insert_repeated_test_schema_ok
156+
FROM test_schema.repeated_table_cloudsync
157+
WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id3']::text[])
158+
AND col_name = 'data' \gset
159+
\if :insert_repeated_test_schema_ok
160+
\echo '[PASS] Test insert metadata on repeated_table in test_schema'
161+
\else
162+
\echo '[FAIL] Test insert metadata on repeated_table in test_schema'
163+
SELECT (:fail::int + 1) AS fail \gset
164+
\endif
165+
166+
-- 'Test cloudsync_changes view read for test_schema.repeated_table'
167+
SELECT COUNT(*) AS changes_view_test_schema_count
168+
FROM cloudsync_changes
169+
WHERE tbl = 'repeated_table' \gset
170+
SELECT COUNT(*) AS changes_meta_test_schema_count
171+
FROM test_schema.repeated_table_cloudsync \gset
172+
SELECT (:changes_view_test_schema_count::int = :changes_meta_test_schema_count::int) AS changes_read_test_schema_ok \gset
173+
\if :changes_read_test_schema_ok
174+
\echo '[PASS] Test cloudsync_changes view read for test_schema.repeated_table'
175+
\else
176+
\echo '[FAIL] Test cloudsync_changes view read for test_schema.repeated_table'
177+
SELECT (:fail::int + 1) AS fail \gset
178+
\endif
179+
180+
-- 'Test cloudsync_changes view write for test_schema.repeated_table'
181+
SELECT cloudsync_uuid() AS repeated_id4 \gset
182+
INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
183+
VALUES (
184+
'repeated_table',
185+
cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id4']::text[]),
186+
'data',
187+
-- "testschema_write" encoded as cloudsync text value (type 0x0b + len 0x10)
188+
decode('0b1074657374736368656d615f7772697465', 'hex'),
189+
1,
190+
cloudsync_db_version_next(),
191+
cloudsync_siteid(),
192+
1,
193+
0
194+
);
195+
SELECT (COUNT(*) = 1) AS changes_write_test_schema_ok
196+
FROM test_schema.repeated_table
197+
WHERE id = :'repeated_id4' AND data = 'testschema_write' \gset
198+
\if :changes_write_test_schema_ok
199+
\echo '[PASS] Test cloudsync_changes view write for test_schema.repeated_table'
200+
\else
201+
\echo '[FAIL] Test cloudsync_changes view write for test_schema.repeated_table'
202+
SELECT (:fail::int + 1) AS fail \gset
203+
\endif
204+
205+
-- 'Test cleanup on repeated_table on test_schema'
206+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated3 \gset
207+
SELECT (to_regclass('test_schema.repeated_table_cloudsync') IS NULL) AS cleanup_repeated3_ok \gset
208+
\if :cleanup_repeated3_ok
209+
\echo '[PASS] Test cleanup on repeated_table on test_schema'
210+
\else
211+
\echo '[FAIL] Test cleanup on repeated_table on test_schema'
212+
SELECT (:fail::int + 1) AS fail \gset
213+
\endif
214+
215+
-- 'Reset schema to public for subsequent tests'
216+
SELECT cloudsync_set_schema('public') AS _reset_schema \gset
217+
SELECT current_schema() AS current_schema_after_reset \gset
218+
SELECT (:'current_schema_after_reset' = 'public') AS schema_reset_ok \gset
219+
\if :schema_reset_ok
220+
\echo '[PASS] Test schema reset to public'
221+
\else
222+
\echo '[FAIL] Test schema reset to public'
223+
SELECT (:fail::int + 1) AS fail \gset
224+
\endif
225+
50226
-- 'Test insert metadata row creation'
51227
SELECT cloudsync_uuid() AS smoke_id \gset
52228
INSERT INTO smoke_tbl (id, val) VALUES (:'smoke_id', 'hello');

0 commit comments

Comments
 (0)