Skip to content

Commit

Permalink
test/lang/python: upgrade to psycopg2 2.9.9
Browse files Browse the repository at this point in the history
v2.9 changed the autocommit handling of `with` blocks, so we can no
longer create the connection using `with`. See [0] for details.

[0]: psycopg/psycopg2#941
  • Loading branch information
benesch authored and nrainer-materialize committed Oct 17, 2023
1 parent 44b8b99 commit 04aa87b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion test/lang/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
psycopg==3.1.10
psycopg-binary==3.1.10
psycopg2==2.8.6
psycopg2==2.9.9
SQLAlchemy==1.3.20
78 changes: 39 additions & 39 deletions test/lang/python/smoketest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,50 +82,50 @@ def test_sqlalchemy(self) -> None:

def test_psycopg2_subscribe(self) -> None:
"""Test SUBSCRIBE with psycopg2 via server cursors."""
with psycopg2.connect(MATERIALIZED_URL) as conn:
conn.set_session(autocommit=True)
with conn.cursor() as cur:
# Create a table with one row of data.
cur.execute("CREATE TABLE psycopg2_subscribe (a int, b text)")
cur.execute("INSERT INTO psycopg2_subscribe VALUES (1, 'a')")
conn.set_session(autocommit=False)
conn = psycopg2.connect(MATERIALIZED_URL)
conn.set_session(autocommit=True)
with conn.cursor() as cur:
# Create a table with one row of data.
cur.execute("CREATE TABLE psycopg2_subscribe (a int, b text)")
cur.execute("INSERT INTO psycopg2_subscribe VALUES (1, 'a')")
conn.set_session(autocommit=False)

# Start SUBSCRIBE using the binary copy protocol.
cur.execute("DECLARE cur CURSOR FOR SUBSCRIBE psycopg2_subscribe")
cur.execute("FETCH ALL cur")

# Validate the first row, but ignore the timestamp column.
row = cur.fetchone()
if row is not None:
(ts, diff, a, b) = row
self.assertEqual(diff, 1)
self.assertEqual(a, 1)
self.assertEqual(b, "a")
else:
self.fail("row is None")

# Start SUBSCRIBE using the binary copy protocol.
cur.execute("DECLARE cur CURSOR FOR SUBSCRIBE psycopg2_subscribe")
cur.execute("FETCH ALL cur")
self.assertEqual(cur.fetchone(), None)

# Validate the first row, but ignore the timestamp column.
row = cur.fetchone()
if row is not None:
(ts, diff, a, b) = row
self.assertEqual(diff, 1)
self.assertEqual(a, 1)
self.assertEqual(b, "a")
else:
self.fail("row is None")
# Insert another row from another connection to simulate an
# update arriving.
with psycopg2.connect(MATERIALIZED_URL) as conn2:
conn2.set_session(autocommit=True)
with conn2.cursor() as cur2:
cur2.execute("INSERT INTO psycopg2_subscribe VALUES (2, 'b')")

self.assertEqual(cur.fetchone(), None)
# Validate the new row, again ignoring the timestamp column.
cur.execute("FETCH ALL cur")
row = cur.fetchone()

# Insert another row from another connection to simulate an
# update arriving.
with psycopg2.connect(MATERIALIZED_URL) as conn2:
conn2.set_session(autocommit=True)
with conn2.cursor() as cur2:
cur2.execute("INSERT INTO psycopg2_subscribe VALUES (2, 'b')")

# Validate the new row, again ignoring the timestamp column.
cur.execute("FETCH ALL cur")
row = cur.fetchone()

if row is not None:
(ts, diff, a, b) = row
self.assertEqual(diff, 1)
self.assertEqual(a, 2)
self.assertEqual(b, "b")
else:
self.fail("row None")
if row is not None:
(ts, diff, a, b) = row
self.assertEqual(diff, 1)
self.assertEqual(a, 2)
self.assertEqual(b, "b")
else:
self.fail("row None")

self.assertEqual(cur.fetchone(), None)
self.assertEqual(cur.fetchone(), None)

def test_psycopg3_subscribe_copy(self) -> None:
"""Test SUBSCRIBE with psycopg3 via its new binary COPY decoding support."""
Expand Down

0 comments on commit 04aa87b

Please sign in to comment.