Skip to content

Commit 220b959

Browse files
committed
test(postgresql): improved test for multi_table_multi_columns_rounds and move the test for repeated_table on multiple schemas to a separated test file
1 parent 4f1c548 commit 220b959

File tree

3 files changed

+225
-9
lines changed

3 files changed

+225
-9
lines changed

test/postgresql/11_multi_table_rounds.sql renamed to test/postgresql/11_multi_table_multi_columns_rounds.sql

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- 'Test multi-table multi-db roundtrip'
2+
-- simulate the sport tracker app from examples
23
-- Steps:
34
-- 1) Create three databases, initialize users/activities/workouts and cloudsync
45
-- 2) Round 1: seed base data on A and sync to B/C
@@ -9,7 +10,9 @@
910
\set testid '11'
1011

1112
-- Step 1: setup databases and schema
12-
-- \echo '[STEP 1] Setup databases and schema'
13+
\if :{?DEBUG_MERGE}
14+
\echo '[STEP 1] Setup databases and schema'
15+
\endif
1316
\connect postgres
1417
\ir helper_psql_conn_setup.sql
1518
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -122,7 +125,9 @@ SELECT cloudsync_init('activities', 'CLS', true) AS _init_activities_c \gset
122125
SELECT cloudsync_init('workouts', 'CLS', true) AS _init_workouts_c \gset
123126

124127
-- Step 2: Round 1 seed base data on A, sync to B/C
125-
-- \echo '[STEP 2] Round 1 seed base data on A, sync to B/C'
128+
\if :{?DEBUG_MERGE}
129+
\echo '[STEP 2] Round 1 seed base data on A, sync to B/C'
130+
\endif
126131
\connect cloudsync_test_a
127132
\if :{?DEBUG_MERGE}
128133
\echo '[INFO] cloudsync_test_a INSERT users u1=alice'
@@ -277,7 +282,9 @@ SELECT * FROM workouts ORDER BY id;
277282
\endif
278283

279284
-- Step 3: Round 2 concurrent updates and inserts across nodes
285+
\if :{?DEBUG_MERGE}
280286
\echo '[STEP 3] Round 2 concurrent updates and inserts across nodes'
287+
\endif
281288
\connect cloudsync_test_a
282289
\if :{?DEBUG_MERGE}
283290
\echo '[INFO] cloudsync_test_a UPDATE users u1=alice_a2'
@@ -460,7 +467,9 @@ SELECT * FROM workouts ORDER BY id;
460467
\endif
461468

462469
-- Step 4: Round 3 more concurrent edits
470+
\if :{?DEBUG_MERGE}
463471
\echo '[STEP 4] Round 3 more concurrent edits'
472+
\endif
464473
\connect cloudsync_test_a
465474
\if :{?DEBUG_MERGE}
466475
\echo '[INFO] cloudsync_test_a UPDATE workouts w2 completed=1'
@@ -617,7 +626,9 @@ SELECT * FROM workouts ORDER BY id;
617626
\endif
618627

619628
-- Step 5: final consistency check across all three databases
629+
\if :{?DEBUG_MERGE}
620630
\echo '[STEP 5] Final consistency check across all three databases'
631+
\endif
621632
\connect cloudsync_test_a
622633
SELECT md5(COALESCE(string_agg(id || ':' || name, ',' ORDER BY id), '')) AS users_hash_a
623634
FROM users \gset
@@ -674,24 +685,24 @@ FROM workouts \gset
674685

675686
SELECT (:'users_hash_a' = :'users_hash_b' AND :'users_hash_a' = :'users_hash_c') AS users_ok \gset
676687
\if :users_ok
677-
\echo '[PASS] Multi-table users convergence'
688+
\echo [PASS] (:testid) Multi-table users convergence
678689
\else
679-
\echo '[FAIL] Multi-table users convergence'
690+
\echo [FAIL] (:testid) Multi-table users convergence
680691
SELECT (:fail::int + 1) AS fail \gset
681692
\endif
682693

683694
SELECT (:'activities_hash_a' = :'activities_hash_b' AND :'activities_hash_a' = :'activities_hash_c') AS activities_ok \gset
684695
\if :activities_ok
685-
\echo '[PASS] Multi-table activities convergence'
696+
\echo [PASS] (:testid) Multi-table activities convergence
686697
\else
687-
\echo '[FAIL] Multi-table activities convergence'
698+
\echo [FAIL] (:testid) Multi-table activities convergence
688699
SELECT (:fail::int + 1) AS fail \gset
689700
\endif
690701

691702
SELECT (:'workouts_hash_a' = :'workouts_hash_b' AND :'workouts_hash_a' = :'workouts_hash_c') AS workouts_ok \gset
692703
\if :workouts_ok
693-
\echo '[PASS] Multi-table workouts convergence'
704+
\echo [PASS] (:testid) Multi-table workouts convergence
694705
\else
695-
\echo '[FAIL] Multi-table workouts convergence'
706+
\echo [FAIL] (:testid) Multi-table workouts convergence
696707
SELECT (:fail::int + 1) AS fail \gset
697708
\endif
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
\set testid '12'
2+
3+
\connect postgres
4+
\ir helper_psql_conn_setup.sql
5+
6+
DROP DATABASE IF EXISTS cloudsync_test_repeated;
7+
CREATE DATABASE cloudsync_test_repeated;
8+
9+
\connect cloudsync_test_repeated
10+
\ir helper_psql_conn_setup.sql
11+
12+
-- Reset extension and install
13+
DROP EXTENSION IF EXISTS cloudsync CASCADE;
14+
CREATE EXTENSION IF NOT EXISTS cloudsync;
15+
16+
-- 'Test multi-schema table init (setup)'
17+
CREATE SCHEMA IF NOT EXISTS test_schema;
18+
DROP TABLE IF EXISTS public.repeated_table;
19+
DROP TABLE IF EXISTS test_schema.repeated_table;
20+
CREATE TABLE public.repeated_table (id TEXT PRIMARY KEY, data TEXT);
21+
CREATE TABLE test_schema.repeated_table (id TEXT PRIMARY KEY, data TEXT);
22+
23+
-- Reset the connection to test if we load the configuration correctly
24+
\connect cloudsync_test_repeated
25+
\ir helper_psql_conn_setup.sql
26+
27+
-- 'Test init on table that exists in multiple schemas (default: public)'
28+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated \gset
29+
SELECT cloudsync_init('repeated_table', 'CLS', true) AS _init_repeated_public \gset
30+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NOT NULL) AS init_repeated_public_ok \gset
31+
\if :init_repeated_public_ok
32+
\echo [PASS] (:testid) Test init on repeated_table in public schema
33+
\else
34+
\echo [FAIL] (:testid) Test init on repeated_table in public schema
35+
SELECT (:fail::int + 1) AS fail \gset
36+
\endif
37+
38+
-- 'Test insert on repeated_table in public schema'
39+
SELECT cloudsync_uuid() AS repeated_id1 \gset
40+
INSERT INTO public.repeated_table (id, data) VALUES (:'repeated_id1', 'public_data');
41+
SELECT (COUNT(*) = 1) AS insert_repeated_public_ok
42+
FROM public.repeated_table_cloudsync
43+
WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id1']::text[])
44+
AND col_name = 'data' \gset
45+
\if :insert_repeated_public_ok
46+
\echo [PASS] (:testid) Test insert metadata on repeated_table in public
47+
\else
48+
\echo [FAIL] (:testid) Test insert metadata on repeated_table in public
49+
SELECT (:fail::int + 1) AS fail \gset
50+
\endif
51+
52+
-- 'Test cloudsync_changes view read for public.repeated_table'
53+
SELECT COUNT(*) AS changes_view_repeated_count
54+
FROM cloudsync_changes
55+
WHERE tbl = 'repeated_table' \gset
56+
SELECT COUNT(*) AS changes_meta_repeated_count
57+
FROM public.repeated_table_cloudsync \gset
58+
SELECT (:changes_view_repeated_count::int = :changes_meta_repeated_count::int) AS changes_read_repeated_ok \gset
59+
\if :changes_read_repeated_ok
60+
\echo [PASS] (:testid) Test cloudsync_changes view read for public.repeated_table
61+
\else
62+
\echo [FAIL] (:testid) Test cloudsync_changes view read for public.repeated_table
63+
SELECT (:fail::int + 1) AS fail \gset
64+
\endif
65+
66+
-- 'Test cloudsync_changes view write for public.repeated_table'
67+
SELECT cloudsync_uuid() AS repeated_id2 \gset
68+
INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
69+
VALUES (
70+
'repeated_table',
71+
cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id2']::text[]),
72+
'data',
73+
-- "public_write" encoded as cloudsync text value (type 0x0b + len 0x0c)
74+
decode('0b0c7075626c69635f7772697465', 'hex'),
75+
1,
76+
cloudsync_db_version_next(),
77+
cloudsync_siteid(),
78+
1,
79+
0
80+
);
81+
SELECT (COUNT(*) = 1) AS changes_write_repeated_ok
82+
FROM public.repeated_table
83+
WHERE id = :'repeated_id2' AND data = 'public_write' \gset
84+
\if :changes_write_repeated_ok
85+
\echo [PASS] (:testid) Test cloudsync_changes view write for public.repeated_table
86+
\else
87+
\echo [FAIL] (:testid) Test cloudsync_changes view write for public.repeated_table
88+
SELECT (:fail::int + 1) AS fail \gset
89+
\endif
90+
91+
-- 'Test cleanup on table with ambiguous name'
92+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated2 \gset
93+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NULL) AS cleanup_repeated_ok \gset
94+
\if :cleanup_repeated_ok
95+
\echo [PASS] (:testid) Test cleanup on repeated_table
96+
\else
97+
\echo [FAIL] (:testid) Test cleanup on repeated_table
98+
SELECT (:fail::int + 1) AS fail \gset
99+
\endif
100+
101+
-- 'Test cloudsync_set_schema and init on test_schema'
102+
SELECT cloudsync_set_schema('test_schema') AS _set_schema \gset
103+
SELECT cloudsync_init('repeated_table', 'CLS', true) AS _init_repeated_test_schema \gset
104+
SELECT (to_regclass('test_schema.repeated_table_cloudsync') IS NOT NULL) AS init_repeated_test_schema_ok \gset
105+
\if :init_repeated_test_schema_ok
106+
\echo [PASS] (:testid) Test init on repeated_table in test_schema
107+
\else
108+
\echo [FAIL] (:testid) Test init on repeated_table in test_schema
109+
SELECT (:fail::int + 1) AS fail \gset
110+
\endif
111+
112+
-- 'Test that public.repeated_table_cloudsync was not recreated'
113+
SELECT (to_regclass('public.repeated_table_cloudsync') IS NULL) AS public_still_clean_ok \gset
114+
\if :public_still_clean_ok
115+
\echo [PASS] (:testid) Test public.repeated_table_cloudsync still cleaned up
116+
\else
117+
\echo [FAIL] (:testid) Test public.repeated_table_cloudsync should not exist
118+
SELECT (:fail::int + 1) AS fail \gset
119+
\endif
120+
121+
-- reset the current schema to check if the next connection load the correct configuration
122+
--SELECT cloudsync_set_schema('public') AS _reset_schema \gset
123+
124+
-- Reset the connection to test if if loads the correct configuration for the table on the correct schema
125+
\connect cloudsync_test_repeated
126+
\ir helper_psql_conn_setup.sql
127+
128+
-- 'Test insert on repeated_table in test_schema'
129+
SELECT cloudsync_uuid() AS repeated_id3 \gset
130+
INSERT INTO test_schema.repeated_table (id, data) VALUES (:'repeated_id3', 'test_schema_data');
131+
SELECT (COUNT(*) = 1) AS insert_repeated_test_schema_ok
132+
FROM test_schema.repeated_table_cloudsync
133+
WHERE pk = cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id3']::text[])
134+
AND col_name = 'data' \gset
135+
\if :insert_repeated_test_schema_ok
136+
\echo [PASS] (:testid) Test insert metadata on repeated_table in test_schema
137+
\else
138+
\echo [FAIL] (:testid) Test insert metadata on repeated_table in test_schema
139+
SELECT (:fail::int + 1) AS fail \gset
140+
\endif
141+
142+
-- 'Test cloudsync_changes view read for test_schema.repeated_table'
143+
SELECT COUNT(*) AS changes_view_test_schema_count
144+
FROM cloudsync_changes
145+
WHERE tbl = 'repeated_table' \gset
146+
SELECT COUNT(*) AS changes_meta_test_schema_count
147+
FROM test_schema.repeated_table_cloudsync \gset
148+
SELECT (:changes_view_test_schema_count::int = :changes_meta_test_schema_count::int) AS changes_read_test_schema_ok \gset
149+
\if :changes_read_test_schema_ok
150+
\echo [PASS] (:testid) Test cloudsync_changes view read for test_schema.repeated_table
151+
\else
152+
\echo [FAIL] (:testid) Test cloudsync_changes view read for test_schema.repeated_table
153+
SELECT (:fail::int + 1) AS fail \gset
154+
\endif
155+
156+
-- 'Test cloudsync_changes view write for test_schema.repeated_table'
157+
SELECT cloudsync_uuid() AS repeated_id4 \gset
158+
INSERT INTO cloudsync_changes (tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq)
159+
VALUES (
160+
'repeated_table',
161+
cloudsync_pk_encode(VARIADIC ARRAY[:'repeated_id4']::text[]),
162+
'data',
163+
-- "testschema_write" encoded as cloudsync text value (type 0x0b + len 0x10)
164+
decode('0b1074657374736368656d615f7772697465', 'hex'),
165+
1,
166+
cloudsync_db_version_next(),
167+
cloudsync_siteid(),
168+
1,
169+
0
170+
);
171+
SELECT (COUNT(*) = 1) AS changes_write_test_schema_ok
172+
FROM test_schema.repeated_table
173+
WHERE id = :'repeated_id4' AND data = 'testschema_write' \gset
174+
\if :changes_write_test_schema_ok
175+
\echo [PASS] (:testid) Test cloudsync_changes view write for test_schema.repeated_table
176+
\else
177+
\echo [FAIL] (:testid) Test cloudsync_changes view write for test_schema.repeated_table
178+
SELECT (:fail::int + 1) AS fail \gset
179+
\endif
180+
181+
-- 'Test cleanup on repeated_table on test_schema'
182+
SELECT cloudsync_cleanup('repeated_table') AS _cleanup_repeated3 \gset
183+
SELECT (to_regclass('test_schema.repeated_table_cloudsync') IS NULL) AS cleanup_repeated3_ok \gset
184+
\if :cleanup_repeated3_ok
185+
\echo [PASS] (:testid) Test cleanup on repeated_table on test_schema
186+
\else
187+
\echo [FAIL] (:testid) Test cleanup on repeated_table on test_schema
188+
SELECT (:fail::int + 1) AS fail \gset
189+
\endif
190+
191+
-- 'Reset schema to public for subsequent tests'
192+
SELECT cloudsync_set_schema('public') AS _reset_schema \gset
193+
SELECT current_schema() AS current_schema_after_reset \gset
194+
SELECT (:'current_schema_after_reset' = 'public') AS schema_reset_ok \gset
195+
\if :schema_reset_ok
196+
\echo [PASS] (:testid) Test schema reset to public
197+
\else
198+
\echo [FAIL] (:testid) Test schema reset to public
199+
SELECT (:fail::int + 1) AS fail \gset
200+
\endif
201+
202+
\if :{?DEBUG_MERGE}
203+
\connect postgres
204+
DROP DATABASE IF EXISTS cloudsync_test_repeated;
205+
\endif

test/postgresql/smoke_test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
\ir 08_resurrect_delayed_delete.sql
1919
\ir 09_multicol_concurrent_edits.sql
2020
\ir 10_empty_payload_noop.sql
21-
-- \ir 11_multi_table_rounds.sql
21+
\ir 11_multi_table_multi_columns_rounds.sql
2222

2323
-- 'Test summary'
2424
\echo '\nTest summary:'

0 commit comments

Comments
 (0)