Skip to content

Commit

Permalink
Add basic test for writes to overlay table without PKs
Browse files Browse the repository at this point in the history
  • Loading branch information
gruuya committed Mar 24, 2022
1 parent 945c383 commit a7bde6a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
5 changes: 1 addition & 4 deletions splitgraph/hooks/data_source/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,11 @@ def init_write_overlay(
AS $$
BEGIN
IF OLD IS NOT NULL AND NEW IS NULL THEN
-- Delete
INSERT INTO {0}.{2} VALUES (OLD.*, FALSE);
ELSIF OLD IS NOT NULL AND NEW IS NOT NULL THEN
-- Update
INSERT INTO {0}.{2} VALUES (OLD.*, FALSE);
INSERT INTO {0}.{2} VALUES (NEW.*, TRUE);
ELSE
-- Insert
ELSIF OLD IS NULL AND NEW IS NOT NULL THEN
INSERT INTO {0}.{2} VALUES (NEW.*, TRUE);
END IF;
Expand Down
57 changes: 57 additions & 0 deletions test/splitgraph/commands/test_writable_lq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from psycopg2.sql import SQL, Identifier

from splitgraph.hooks.data_source.base import WRITE_LOWER_PREFIX, WRITE_UPPER_PREFIX


def test_basic_writes_no_pks(pg_repo_local):
table_name = "fruits"
pg_repo_local.head.checkout(layered=True)

# Ensure that the table is now in the overlay/LQ mode
assert pg_repo_local.is_overlay_view(table_name)

# Perform some basic writes

# Insert a couple of new rows
pg_repo_local.run_sql("INSERT INTO fruits VALUES (3, 'banana')")
pg_repo_local.run_sql("INSERT INTO fruits VALUES (4, 'pear')")

# Update a row existing in the lower table, and one existing only in the upper (due to insert above)
pg_repo_local.run_sql("UPDATE fruits SET name = 'mango' WHERE name = 'orange'")
pg_repo_local.run_sql("UPDATE fruits SET name = 'watermelon' WHERE name = 'pear'")

# Delete a row existing in the lower table, and one existing only in the upper (due to insert/update above)
pg_repo_local.run_sql("DELETE FROM fruits WHERE fruit_id = 1")
pg_repo_local.run_sql("DELETE FROM fruits WHERE name = 'watermelon'")

# Won't affect the output of the view due to no PKs on the table
pg_repo_local.run_sql("INSERT INTO fruits VALUES (3, 'banana')")

# Assert the lower table has the old values intact
assert pg_repo_local.run_sql(
SQL("SELECT * FROM {}").format(Identifier(WRITE_LOWER_PREFIX + table_name))
) == [
(1, "apple"),
(2, "orange"),
]

# Assert the upper table stores the pending writes
assert pg_repo_local.run_sql(
SQL("SELECT * FROM {}").format(Identifier(WRITE_UPPER_PREFIX + table_name))
) == [
(3, "banana", True, 1),
(4, "pear", True, 2),
(2, "orange", False, 3),
(2, "mango", True, 4),
(4, "pear", False, 5),
(4, "watermelon", True, 6),
(1, "apple", False, 7),
(4, "watermelon", False, 8),
(3, "banana", True, 9),
]

# Assert the correct result from the overlay view
assert pg_repo_local.run_sql("SELECT * FROM fruits") == [
(2, "mango"),
(3, "banana"),
]

0 comments on commit a7bde6a

Please sign in to comment.