diff --git a/core/src/crsqlite.test.c b/core/src/crsqlite.test.c index ed7037ccb..539322255 100644 --- a/core/src/crsqlite.test.c +++ b/core/src/crsqlite.test.c @@ -229,11 +229,10 @@ static void teste2e() { assert(rc == SQLITE_ROW); const char *tmpSiteid = (const char *)sqlite3_column_text(pStmt3, 0); - printf("db1sid: %s\n", db1siteid); - printf("db2sid: %s\n", db2siteid); - printf("db3sid: %s\n", db3siteid); - printf("tempsid: %s\n", tmpSiteid); - // printf("tmp: %s, db3: %s", tmpSiteid, db3siteid); + // printf("db1sid: %s\n", db1siteid); + // printf("db2sid: %s\n", db2siteid); + // printf("db3sid: %s\n", db3siteid); + // printf("tempsid: %s\n", tmpSiteid); assert(strcmp(tmpSiteid, db1siteid) == 0); rc = sqlite3_step(pStmt3); diff --git a/py/correctness/tests/test_siteid.py b/py/correctness/tests/test_siteid.py index 2b4866558..16500d2af 100644 --- a/py/correctness/tests/test_siteid.py +++ b/py/correctness/tests/test_siteid.py @@ -1,6 +1,17 @@ import pathlib from uuid import UUID from crsql_correctness import connect +from pprint import pprint + + +def sync_left_to_right(l, r, since): + r_site_id = r.execute("SELECT crsql_site_id()").fetchone()[0] + changes = l.execute( + "SELECT * FROM crsql_changes WHERE db_version > ? AND site_id IS NOT ?", (since, r_site_id)) + for change in changes: + r.execute( + "INSERT INTO crsql_changes VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", change) + r.commit() def test_c1(): @@ -32,9 +43,62 @@ def test_c3c4(): assert siteid_initial == siteid_restored +# Site id is set to crsql_site_id on local writes def test_site_id_for_local_writes(): - None + c = connect(":memory:") + c.execute("CREATE TABLE foo (id not null, x, y, primary key (id))") + c.execute("SELECT crsql_as_crr('foo')") + c.commit() + + c.execute("INSERT INTO foo VALUES (1, 2, 3)") + c.commit() + + def check_counts(): + total_changes_count = c.execute( + "SELECT count(*) FROM crsql_changes").fetchone()[0] + changes_with_local_site_count = c.execute( + "SELECT count(*) FROM crsql_changes WHERE site_id = crsql_site_id()").fetchone()[0] + assert total_changes_count == changes_with_local_site_count + + c.execute("UPDATE foo SET x = 3 WHERE id = 1") + c.commit() + check_counts() + + c.execute("INSERT OR REPLACE INTO foo VALUES (1, 5, 9)") + c.commit() + check_counts() + + c.execute("DELETE FROM foo") + c.commit() + check_counts() def test_site_id_from_merge(): - None + def simple_schema(): + a = connect(":memory:") + a.execute("create table foo (a primary key not null, b);") + a.commit() + a.execute("SELECT crsql_as_crr('foo')") + a.commit() + return a + + a = simple_schema() + a.execute("INSERT INTO foo VALUES (1, 2.0e2);") + a.commit() + a.execute("INSERT INTO foo VALUES (2, X'1232');") + a.commit() + + b = simple_schema() + c = simple_schema() + + sync_left_to_right(a, b, 0) + sync_left_to_right(b, c, 0) + + site_ids_fromC = c.execute( + "SELECT site_id FROM crsql_changes ORDER BY pk ASC").fetchall() + site_ids_fromB = b.execute( + "SELECT site_id FROM crsql_changes ORDER BY pk ASC").fetchall() + site_ids_fromA = a.execute( + "SELECT site_id FROM crsql_changes ORDER BY pk ASC").fetchall() + assert site_ids_fromC == site_ids_fromA + assert site_ids_fromB == site_ids_fromA