Skip to content

Commit f9f44a7

Browse files
committed
test(postgresql): improved tests
1 parent e61577e commit f9f44a7

12 files changed

+229
-103
lines changed

test/postgresql/01_unittest.sql

Lines changed: 71 additions & 71 deletions
Large diffs are not rendered by default.

test/postgresql/02_roundtrip.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
-- '2 db roundtrip test'
22

3-
\echo '\nRunning two-db roundtrip test ...'
3+
\set testid '02'
4+
45
\connect cloudsync_test_1
6+
\ir helper_psql_conn_setup.sql
57
SELECT encode(cloudsync_payload_encode(tbl, pk, col_name, col_value, col_version, db_version, site_id, cl, seq), 'hex') AS payload_hex
68
FROM cloudsync_changes
79
WHERE site_id = cloudsync_siteid() \gset
@@ -19,8 +21,8 @@ SELECT md5(COALESCE(string_agg(id || ':' || COALESCE(val, ''), ',' ORDER BY id),
1921
FROM smoke_tbl \gset
2022
SELECT (:'smoke_hash' = :'smoke_hash_b') AS payload_roundtrip_ok \gset
2123
\if :payload_roundtrip_ok
22-
\echo '[PASS] Test payload roundtrip to another database'
24+
\echo [PASS] (:testid) Test payload roundtrip to another database
2325
\else
24-
\echo '[FAIL] Test payload roundtrip to another database'
26+
\echo [FAIL] (:testid) Test payload roundtrip to another database
2527
SELECT (:fail::int + 1) AS fail \gset
2628
\endif

test/postgresql/03_multiple_roundtrip.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- 'Test multi-db roundtrip with concurrent updates'
22

3-
\echo '\nRunning multi-db roundtrip with concurrent updates ...'
3+
\set testid '03'
4+
45
\connect postgres
56
\ir helper_psql_conn_setup.sql
67
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -285,8 +286,8 @@ FROM smoke_tbl \gset
285286

286287
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
287288
\if :multi_db_roundtrip_ok
288-
\echo '[PASS] Test multi-db roundtrip with concurrent updates'
289+
\echo [PASS] (:testid) Test multi-db roundtrip with concurrent updates
289290
\else
290-
\echo '[FAIL] Test multi-db roundtrip with concurrent updates'
291+
\echo [FAIL] (:testid) Test multi-db roundtrip with concurrent updates
291292
SELECT (:fail::int + 1) AS fail \gset
292293
\endif

test/postgresql/04_colversion_skew.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
-- - concurrent update pattern where A/B/C perform 2/1/3 updates respectively on id1 before syncing.
33
-- - It follows the same apply order as the existing 3‑DB test and verifies final convergence across all three databases
44

5-
\echo '\nRunning multi-db roundtrip with skewed col_version updates ...'
5+
\set testid '04'
6+
67
\connect postgres
78
\ir helper_psql_conn_setup.sql
89
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -308,8 +309,8 @@ FROM smoke_tbl \gset
308309

309310
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
310311
\if :multi_db_roundtrip_ok
311-
\echo '[PASS] Test multi-db roundtrip with skewed col_version updates'
312+
\echo [PASS] (:testid) Test multi-db roundtrip with skewed col_version updates
312313
\else
313-
\echo '[FAIL] Test multi-db roundtrip with skewed col_version updates'
314+
\echo [FAIL] (:testid) Test multi-db roundtrip with skewed col_version updates
314315
SELECT (:fail::int + 1) AS fail \gset
315316
\endif

test/postgresql/05_delete_recreate_cycle.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
-- 6. C reinserts with another value
88

99

10-
\echo '\nRunning delete/recreate/update/delete/reinsert cycle across multiple DBs ...'
10+
\set testid '05'
11+
1112
\connect postgres
1213
\ir helper_psql_conn_setup.sql
1314
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -767,8 +768,8 @@ FROM smoke_tbl \gset
767768

768769
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
769770
\if :multi_db_roundtrip_ok
770-
\echo '[PASS] Test delete/recreate/update/delete/reinsert cycle'
771+
\echo [PASS] (:testid) Test delete/recreate/update/delete/reinsert cycle
771772
\else
772-
\echo '[FAIL] Test delete/recreate/update/delete/reinsert cycle'
773+
\echo [FAIL] (:testid) Test delete/recreate/update/delete/reinsert cycle
773774
SELECT (:fail::int + 1) AS fail \gset
774775
\endif

test/postgresql/06_out_of_order_delivery.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
-- - Applies round3 before round2 on C, while A/B apply round2 then round3
55
-- - Verifies convergence across all three DBs
66

7-
\echo '\nRunning out-of-order payload delivery across multiple DBs test ...'
7+
\set testid '06'
8+
89
\connect postgres
910
\ir helper_psql_conn_setup.sql
1011
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -271,8 +272,8 @@ FROM smoke_tbl \gset
271272

272273
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
273274
\if :multi_db_roundtrip_ok
274-
\echo '[PASS] Test out-of-order payload delivery'
275+
\echo [PASS] (:testid) Test out-of-order payload delivery
275276
\else
276-
\echo '[FAIL] Test out-of-order payload delivery'
277+
\echo [FAIL] (:testid) Test out-of-order payload delivery
277278
SELECT (:fail::int + 1) AS fail \gset
278279
\endif

test/postgresql/07_delete_vs_update.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
-- 2) B deletes id1 while C updates id1, then sync
55
-- 3) A updates id1 after merge, then sync
66

7-
\echo '\nRunning concurrent delete vs update test ...'
7+
\set testid '07'
8+
89
\connect postgres
910
\ir helper_psql_conn_setup.sql
1011
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -273,8 +274,8 @@ FROM smoke_tbl \gset
273274

274275
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
275276
\if :multi_db_roundtrip_ok
276-
\echo '[PASS] Concurrent delete vs update'
277+
\echo [PASS] (:testid) Concurrent delete vs update
277278
\else
278-
\echo '[FAIL] Concurrent delete vs update'
279+
\echo [FAIL] (:testid) Concurrent delete vs update
279280
SELECT (:fail::int + 1) AS fail \gset
280281
\endif

test/postgresql/08_resurrect_delayed_delete.sql

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
-- 4) Apply delayed delete payload from A to B/C
77
-- 5) Verify convergence
88

9-
\echo '\nRunning resurrect after delete with delayed payload test ...'
9+
\set testid '08'
10+
1011
\connect postgres
1112
\ir helper_psql_conn_setup.sql
1213
DROP DATABASE IF EXISTS cloudsync_test_a;
@@ -39,6 +40,9 @@ SELECT cloudsync_init('smoke_tbl', 'CLS', true) AS _init_site_id_c \gset
3940

4041
-- Round 1: seed id1 on A, sync to B/C
4142
\connect cloudsync_test_a
43+
\if :{?DEBUG_MERGE}
44+
\echo '[INFO] cloudsync_test_a INSERT id1=seed_v1'
45+
\endif
4246
INSERT INTO smoke_tbl VALUES ('id1', 'seed_v1');
4347
SELECT CASE WHEN payload IS NULL OR octet_length(payload) = 0
4448
THEN ''
@@ -76,43 +80,88 @@ FROM (
7680
) AS p \gset
7781

7882
\connect cloudsync_test_a
83+
\if :{?DEBUG_MERGE}
84+
\echo '[INFO] round1 before merge cloudsync_test_a smoke_tbl'
85+
SELECT * FROM smoke_tbl ORDER BY id;
86+
\endif
7987
\if :payload_b_r1_ok
88+
\if :{?DEBUG_MERGE}
89+
\echo '[MERGE] round1 apply b -> a'
90+
\endif
8091
SELECT cloudsync_payload_apply(decode(substr(:'payload_b_r1', 3), 'hex')) AS _apply_a_r1_b \gset
8192
\else
8293
SELECT 0 AS _apply_a_r1_b \gset
8394
\endif
8495
\if :payload_c_r1_ok
96+
\if :{?DEBUG_MERGE}
97+
\echo '[MERGE] round1 apply c -> a'
98+
\endif
8599
SELECT cloudsync_payload_apply(decode(substr(:'payload_c_r1', 3), 'hex')) AS _apply_a_r1_c \gset
86100
\else
87101
SELECT 0 AS _apply_a_r1_c \gset
88102
\endif
103+
\if :{?DEBUG_MERGE}
104+
\echo '[INFO] round1 after merge cloudsync_test_a smoke_tbl'
105+
SELECT * FROM smoke_tbl ORDER BY id;
106+
\endif
89107

90108
\connect cloudsync_test_b
109+
\if :{?DEBUG_MERGE}
110+
\echo '[INFO] round1 before merge cloudsync_test_b smoke_tbl'
111+
SELECT * FROM smoke_tbl ORDER BY id;
112+
\endif
91113
\if :payload_a_r1_ok
114+
\if :{?DEBUG_MERGE}
115+
\echo '[MERGE] round1 apply a -> b'
116+
\endif
92117
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r1', 3), 'hex')) AS _apply_b_r1_a \gset
93118
\else
94119
SELECT 0 AS _apply_b_r1_a \gset
95120
\endif
96121
\if :payload_c_r1_ok
122+
\if :{?DEBUG_MERGE}
123+
\echo '[MERGE] round1 apply c -> b'
124+
\endif
97125
SELECT cloudsync_payload_apply(decode(substr(:'payload_c_r1', 3), 'hex')) AS _apply_b_r1_c \gset
98126
\else
99127
SELECT 0 AS _apply_b_r1_c \gset
100128
\endif
129+
\if :{?DEBUG_MERGE}
130+
\echo '[INFO] round1 after merge cloudsync_test_b smoke_tbl'
131+
SELECT * FROM smoke_tbl ORDER BY id;
132+
\endif
101133

102134
\connect cloudsync_test_c
135+
\if :{?DEBUG_MERGE}
136+
\echo '[INFO] round1 before merge cloudsync_test_c smoke_tbl'
137+
SELECT * FROM smoke_tbl ORDER BY id;
138+
\endif
103139
\if :payload_a_r1_ok
140+
\if :{?DEBUG_MERGE}
141+
\echo '[MERGE] round1 apply a -> c'
142+
\endif
104143
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r1', 3), 'hex')) AS _apply_c_r1_a \gset
105144
\else
106145
SELECT 0 AS _apply_c_r1_a \gset
107146
\endif
108147
\if :payload_b_r1_ok
148+
\if :{?DEBUG_MERGE}
149+
\echo '[MERGE] round1 apply b -> c'
150+
\endif
109151
SELECT cloudsync_payload_apply(decode(substr(:'payload_b_r1', 3), 'hex')) AS _apply_c_r1_b \gset
110152
\else
111153
SELECT 0 AS _apply_c_r1_b \gset
112154
\endif
155+
\if :{?DEBUG_MERGE}
156+
\echo '[INFO] round1 after merge cloudsync_test_c smoke_tbl'
157+
SELECT * FROM smoke_tbl ORDER BY id;
158+
\endif
113159

114160
-- Round 2: A deletes id1 (payload delayed for B/C)
115161
\connect cloudsync_test_a
162+
\if :{?DEBUG_MERGE}
163+
\echo '[INFO] cloudsync_test_a DELETE id1'
164+
\endif
116165
DELETE FROM smoke_tbl WHERE id = 'id1';
117166
SELECT CASE WHEN payload IS NULL OR octet_length(payload) = 0
118167
THEN ''
@@ -127,7 +176,12 @@ FROM (
127176

128177
-- Round 3: B recreates id1, sync to A/C (but A's delete still not applied on B/C)
129178
\connect cloudsync_test_b
130-
INSERT INTO smoke_tbl VALUES ('id1', 'recreate_v2');
179+
\if :{?DEBUG_MERGE}
180+
\echo '[INFO] cloudsync_test_b UPSERT id1=recreate_v2'
181+
\endif
182+
INSERT INTO smoke_tbl (id, val)
183+
VALUES ('id1', 'recreate_v2')
184+
ON CONFLICT (id) DO UPDATE SET val = EXCLUDED.val;
131185
SELECT CASE WHEN payload IS NULL OR octet_length(payload) = 0
132186
THEN ''
133187
ELSE '\x' || encode(payload, 'hex')
@@ -164,55 +218,119 @@ FROM (
164218
) AS p \gset
165219

166220
\connect cloudsync_test_a
221+
\if :{?DEBUG_MERGE}
222+
\echo '[INFO] round3 before merge cloudsync_test_a smoke_tbl'
223+
SELECT * FROM smoke_tbl ORDER BY id;
224+
\endif
167225
\if :payload_b_r3_ok
226+
\if :{?DEBUG_MERGE}
227+
\echo '[MERGE] round3 apply b -> a'
228+
\endif
168229
SELECT cloudsync_payload_apply(decode(substr(:'payload_b_r3', 3), 'hex')) AS _apply_a_r3_b \gset
169230
\else
170231
SELECT 0 AS _apply_a_r3_b \gset
171232
\endif
172233
\if :payload_c_r3_ok
234+
\if :{?DEBUG_MERGE}
235+
\echo '[MERGE] round3 apply c -> a'
236+
\endif
173237
SELECT cloudsync_payload_apply(decode(substr(:'payload_c_r3', 3), 'hex')) AS _apply_a_r3_c \gset
174238
\else
175239
SELECT 0 AS _apply_a_r3_c \gset
176240
\endif
241+
\if :{?DEBUG_MERGE}
242+
\echo '[INFO] round3 after merge cloudsync_test_a smoke_tbl'
243+
SELECT * FROM smoke_tbl ORDER BY id;
244+
\endif
177245

178246
\connect cloudsync_test_b
247+
\if :{?DEBUG_MERGE}
248+
\echo '[INFO] round3 before merge cloudsync_test_b smoke_tbl'
249+
SELECT * FROM smoke_tbl ORDER BY id;
250+
\endif
179251
\if :payload_a_r3_ok
252+
\if :{?DEBUG_MERGE}
253+
\echo '[MERGE] round3 apply a -> b'
254+
\endif
180255
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r3', 3), 'hex')) AS _apply_b_r3_a \gset
181256
\else
182257
SELECT 0 AS _apply_b_r3_a \gset
183258
\endif
184259
\if :payload_c_r3_ok
260+
\if :{?DEBUG_MERGE}
261+
\echo '[MERGE] round3 apply c -> b'
262+
\endif
185263
SELECT cloudsync_payload_apply(decode(substr(:'payload_c_r3', 3), 'hex')) AS _apply_b_r3_c \gset
186264
\else
187265
SELECT 0 AS _apply_b_r3_c \gset
188266
\endif
267+
\if :{?DEBUG_MERGE}
268+
\echo '[INFO] round3 after merge cloudsync_test_b smoke_tbl'
269+
SELECT * FROM smoke_tbl ORDER BY id;
270+
\endif
189271

190272
\connect cloudsync_test_c
273+
\if :{?DEBUG_MERGE}
274+
\echo '[INFO] round3 before merge cloudsync_test_c smoke_tbl'
275+
SELECT * FROM smoke_tbl ORDER BY id;
276+
\endif
191277
\if :payload_a_r3_ok
278+
\if :{?DEBUG_MERGE}
279+
\echo '[MERGE] round3 apply a -> c'
280+
\endif
192281
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r3', 3), 'hex')) AS _apply_c_r3_a \gset
193282
\else
194283
SELECT 0 AS _apply_c_r3_a \gset
195284
\endif
196285
\if :payload_b_r3_ok
286+
\if :{?DEBUG_MERGE}
287+
\echo '[MERGE] round3 apply b -> c'
288+
\endif
197289
SELECT cloudsync_payload_apply(decode(substr(:'payload_b_r3', 3), 'hex')) AS _apply_c_r3_b \gset
198290
\else
199291
SELECT 0 AS _apply_c_r3_b \gset
200292
\endif
293+
\if :{?DEBUG_MERGE}
294+
\echo '[INFO] round3 after merge cloudsync_test_c smoke_tbl'
295+
SELECT * FROM smoke_tbl ORDER BY id;
296+
\endif
201297

202298
-- Round 4: apply delayed delete payload from A to B/C
203299
\connect cloudsync_test_b
300+
\if :{?DEBUG_MERGE}
301+
\echo '[INFO] round4 before merge cloudsync_test_b smoke_tbl'
302+
SELECT * FROM smoke_tbl ORDER BY id;
303+
\endif
204304
\if :payload_a_r2_ok
305+
\if :{?DEBUG_MERGE}
306+
\echo '[MERGE] round4 apply delayed a -> b'
307+
\endif
205308
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r2', 3), 'hex')) AS _apply_b_r4_a_delayed \gset
206309
\else
207310
SELECT 0 AS _apply_b_r4_a_delayed \gset
208311
\endif
312+
\if :{?DEBUG_MERGE}
313+
\echo '[INFO] round4 after merge cloudsync_test_b smoke_tbl'
314+
SELECT * FROM smoke_tbl ORDER BY id;
315+
\endif
209316

210317
\connect cloudsync_test_c
318+
\if :{?DEBUG_MERGE}
319+
\echo '[INFO] round4 before merge cloudsync_test_c smoke_tbl'
320+
SELECT * FROM smoke_tbl ORDER BY id;
321+
\endif
211322
\if :payload_a_r2_ok
323+
\if :{?DEBUG_MERGE}
324+
\echo '[MERGE] round4 apply delayed a -> c'
325+
\endif
212326
SELECT cloudsync_payload_apply(decode(substr(:'payload_a_r2', 3), 'hex')) AS _apply_c_r4_a_delayed \gset
213327
\else
214328
SELECT 0 AS _apply_c_r4_a_delayed \gset
215329
\endif
330+
\if :{?DEBUG_MERGE}
331+
\echo '[INFO] round4 after merge cloudsync_test_c smoke_tbl'
332+
SELECT * FROM smoke_tbl ORDER BY id;
333+
\endif
216334

217335
-- Final consistency check across all three databases
218336
\connect cloudsync_test_a
@@ -229,8 +347,8 @@ FROM smoke_tbl \gset
229347

230348
SELECT (:'smoke_hash_a' = :'smoke_hash_b' AND :'smoke_hash_a' = :'smoke_hash_c') AS multi_db_roundtrip_ok \gset
231349
\if :multi_db_roundtrip_ok
232-
\echo '[PASS] Resurrect after delete with delayed payload'
350+
\echo [PASS] (:testid) Resurrect after delete with delayed payload
233351
\else
234-
\echo '[FAIL] Resurrect after delete with delayed payload'
352+
\echo [FAIL] (:testid) Resurrect after delete with delayed payload
235353
SELECT (:fail::int + 1) AS fail \gset
236354
\endif

0 commit comments

Comments
 (0)