Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Oct 13, 2023
1 parent b6a9411 commit 43853f2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion core/rs/core/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn create_clock_table(
col_name TEXT NOT NULL,
col_version INTEGER NOT NULL,
db_version INTEGER NOT NULL,
site_id INTEGER,
site_id INTEGER NOT NULL DEFAULT 0,
seq INTEGER NOT NULL,
PRIMARY KEY (key, col_name)
) WITHOUT ROWID, STRICT",
Expand Down
4 changes: 4 additions & 0 deletions py/correctness/src/crsql_correctness/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ def close(c):
c.close()


def get_site_id(c):
return c.execute("SELECT crsql_site_id()").fetchone()[0]


min_db_v = 0
6 changes: 5 additions & 1 deletion py/correctness/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# source env/bin/activate
# python -m pytest tests -s -k test_cl_merging
python3 -m pytest tests -s
python3 -m pytest tests -s

# -k test_schema_modification
# test_site_id_lookaside
# test_sync

# -k test_sync_prop.py
50 changes: 31 additions & 19 deletions py/correctness/tests/test_cl_merging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from crsql_correctness import connect, close, min_db_v
from crsql_correctness import connect, close, get_site_id, min_db_v
from pprint import pprint
import random
import pytest
Expand Down Expand Up @@ -155,12 +155,13 @@ def test_larger_cl_delete_deletes_all():
# We should have deleted the entry via sync of greater delete causal length from c1 to c2
assert (rows == [])

c1_site_id = get_site_id(c1)
# c1 shouldn't have column metadata but only a delete record of the dropped item whose causal length should be 2.
assert (c1_changes == [
('foo', b'\x01\t\x01', '-1', None, 2, 1, None, 2, 1)])
('foo', b'\x01\t\x01', '-1', None, 2, 1, c1_site_id, 2, 1)])
# c2 merged in the delete thus bumping causal length to 2 and bumping db version since there was a change.
assert (c2_changes == [
('foo', b'\x01\t\x01', '-1', None, 2, 2, None, 2, 1)])
('foo', b'\x01\t\x01', '-1', None, 2, 2, c1_site_id, 2, 1)])
close(c1)
close(c2)

Expand All @@ -179,9 +180,10 @@ def test_smaller_delete_does_not_delete_larger_cl():
c2.commit()

# check the pre-condition the c1 actually has a delete event
c1_site_id = get_site_id(c1)
c1_changes = c1.execute("SELECT * FROM crsql_changes").fetchall()
assert (c1_changes == [
('foo', b'\x01\t\x01', '-1', None, 2, 1, None, 2, 1)])
('foo', b'\x01\t\x01', '-1', None, 2, 1, c1_site_id, 2, 1)])

c2_changes_pre_merge = c2.execute("SELECT * FROM crsql_changes").fetchall()

Expand Down Expand Up @@ -275,10 +277,9 @@ def test_pr_299_scenario():

# c2 should have accepted all the changes given the higher causal length
# a = 1, b = 1, cl = 3
# note: why is site_id missing??? Ah, it is missing since we don't coalesce to get it. This is expected.
# TODO: should no longer be missing site_id!
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 3, None, 3, 0),
('foo', b'\x01\t\x01', 'b', 1, 1, 3, None, 3, 1)])
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 3, c1_site_id, 3, 0),
('foo', b'\x01\t\x01', 'b', 1, 1, 3, c1_site_id, 3, 1)])
# c2 and c1 should match in terms of data
assert (c1.execute("SELECT * FROM foo").fetchall() ==
c2.execute("SELECT * FROM foo").fetchall())
Expand Down Expand Up @@ -383,6 +384,7 @@ def test_resurrection_of_live_thing_via_sentinel():
c2.commit()

# a resurrection of an already live row
# only merge over the sentinal colun to c2
sentinel_resurrect = c1.execute(
"SELECT * FROM crsql_changes WHERE cid = '-1'").fetchone()
c2.execute(
Expand All @@ -392,8 +394,10 @@ def test_resurrection_of_live_thing_via_sentinel():
changes = c2.execute("SELECT * FROM crsql_changes").fetchall()

# 'b' should be zeroed column version but latest db version.
assert (changes == [('foo', b'\x01\t\x01', 'b', 1, 0, 2, None, 3, 0),
('foo', b'\x01\t\x01', '-1', None, 3, 2, None, 3, 2)])
c2_site_id = get_site_id(c2)
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01', 'b', 1, 0, 2, c2_site_id, 3, 0),
('foo', b'\x01\t\x01', '-1', None, 3, 2, c1_site_id, 3, 2)])
# now lets finish getting changes from the other node
changes = c1.execute(
"SELECT * FROM crsql_changes WHERE cid != '-1'").fetchone()
Expand All @@ -402,7 +406,7 @@ def test_resurrection_of_live_thing_via_sentinel():
c2.commit()

changes = c2.execute("SELECT * FROM crsql_changes").fetchall()
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, None, 3, 2),
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, c1_site_id, 3, 2),
# col version bump to 1 since the other guy won on col version.
# db version bumped as well since the col version changed.
# holding the db version stable would prevent nodes that proxy other nodes
Expand All @@ -413,7 +417,7 @@ def test_resurrection_of_live_thing_via_sentinel():
# Then B receives changes from A which move B's clock forward w/o changing B's value
# C then merges to B and loses there
# If B db version didn't change then C would never get the changes that B is proxying from A
('foo', b'\x01\t\x01', 'b', 1, 1, 3, None, 3, 3)])
('foo', b'\x01\t\x01', 'b', 1, 1, 3, c1_site_id, 3, 3)])
close(c1)
close(c2)

Expand All @@ -440,8 +444,9 @@ def test_resurrection_of_live_thing_via_non_sentinel():
# we get the new values as expected
# db version pushed
# col version is at 1 given we rolled the causal length forward for the resurrection
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, None, 3, 3),
('foo', b'\x01\t\x01', 'b', 1, 1, 2, None, 3, 3)])
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, c1_site_id, 3, 3),
('foo', b'\x01\t\x01', 'b', 1, 1, 2, c1_site_id, 3, 3)])

# sync all other entries should be a no-op
sync_left_to_right(c1, c2, 0)
Expand Down Expand Up @@ -471,10 +476,12 @@ def test_resurrection_of_dead_thing_via_sentinel():
c2.commit()

changes = c2.execute("SELECT * FROM crsql_changes").fetchall()
c1_site_id = get_site_id(c1)
# row comes back
# cl = 3 given resurrected from dead (2)
# db_version = 2 given it was a change
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, None, 3, 2)])
assert (changes == [('foo', b'\x01\t\x01',
'-1', None, 3, 2, c1_site_id, 3, 2)])
close(c1)
close(c2)

Expand Down Expand Up @@ -503,8 +510,9 @@ def test_resurrection_of_dead_thing_via_non_sentinel():
# cl = 3 given resurrected from dead (2)
# db_version = 2 given it was a change
# col version rolled back given cl moved forward
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, None, 3, 3),
('foo', b'\x01\t\x01', 'b', 1, 1, 2, None, 3, 3)])
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 2, c1_site_id, 3, 3),
('foo', b'\x01\t\x01', 'b', 1, 1, 2, c1_site_id, 3, 3)])
close(c1)
close(c2)

Expand Down Expand Up @@ -547,7 +555,9 @@ def test_delete_via_sentinel():
c2.commit()

changes = c2.execute("SELECT * FROM crsql_changes").fetchall()
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 2, 2, None, 2, 0)])
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01',
'-1', None, 2, 2, c1_site_id, 2, 0)])
close(c1)
close(c2)

Expand Down Expand Up @@ -899,7 +909,9 @@ def test_pko_resurrect():
c2.execute("SELECT * FROM foo").fetchall())

changes = c2.execute("SELECT * FROM crsql_changes").fetchall()
assert (changes == [('foo', b'\x01\t\x01', '-1', None, 3, 3, None, 3, 0)])
c1_site_id = get_site_id(c1)
assert (changes == [('foo', b'\x01\t\x01',
'-1', None, 3, 3, c1_site_id, 3, 0)])

close(c1)
close(c2)
Expand Down
2 changes: 1 addition & 1 deletion py/correctness/tests/test_insert_new_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_c1_c2_c3_c4_c6_c7_crr_values():

rows = c.execute(
"select key, col_name, col_version, db_version, site_id from foo__crsql_clock").fetchall()
assert [(1, 'a', 1, init_version + 1, None)] == rows
assert [(1, 'a', 1, init_version + 1, 0)] == rows
new_version = c.execute("SELECT crsql_db_version()").fetchone()[0]

assert new_version == init_version + 1
Expand Down
7 changes: 4 additions & 3 deletions py/correctness/tests/test_schema_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def test_drop_clock_on_col_remove():
assert (changes == expected)

clock_entries = c.execute(clock_query).fetchall()
assert (clock_entries == [(1, 1, 1, 'complete', None),
(1, 1, 1, 'list', None), (1, 1, 1, 'name', None)])
assert (clock_entries == [(1, 1, 1, 'complete', 0),
(1, 1, 1, 'list', 0), (1, 1, 1, 'name', 0)])

c.execute("SELECT crsql_begin_alter('todo');")
# Dropping a column should remove its entries from our replication logs.
Expand All @@ -105,7 +105,7 @@ def test_drop_clock_on_col_remove():
clock_entries = c.execute(clock_query).fetchall()
assert (
clock_entries == [
(1, 1, 1, 'complete', None), (1, 1, 1, 'name', None)]
(1, 1, 1, 'complete', 0), (1, 1, 1, 'name', 0)]
)


Expand Down Expand Up @@ -616,6 +616,7 @@ def test_add_new_col_to_pk():
c.execute("SELECT crsql_commit_alter('foo');")

changes = c.execute(full_changes_query).fetchall()
pprint(changes)
assert (changes == [('foo', b'\x02\t\x01\t\x03', 'b', 2, 1, 1, None),
('foo', b'\x02\t\x04\t\x06', 'b', 5, 1, 1, None)])

Expand Down

0 comments on commit 43853f2

Please sign in to comment.