Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
60 changes: 60 additions & 0 deletions tests/storage/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,63 @@ def test_delete_one(self) -> Generator["defer.Deferred[object]", object, None]:
self.mock_txn.execute.assert_called_with(
"DELETE FROM tablename WHERE keycol = ?", ["Go away"]
)

@defer.inlineCallbacks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess twisted/trial doesn't let you write test methods using async def ...? :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do, but I think not on our lowest bound. I started testing it and got into some dependency hell and stopped.

def test_upsert(self) -> Generator["defer.Deferred[object]", object, None]:
self.mock_txn.rowcount = 1

result = yield defer.ensureDeferred(
self.datastore.db_pool.simple_upsert(
table="tablename",
keyvalues={"columnname": "oldvalue"},
values={"othercol": "newvalue"},
)
)

self.mock_txn.execute.assert_called_with(
"INSERT INTO tablename (columnname, othercol) VALUES (?, ?) ON CONFLICT (columnname) DO UPDATE SET othercol=EXCLUDED.othercol",
["oldvalue", "newvalue"],
)
self.assertTrue(result)

@defer.inlineCallbacks
def test_upsert_with_insert(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.mock_txn.rowcount = 1

result = yield defer.ensureDeferred(
self.datastore.db_pool.simple_upsert(
table="tablename",
keyvalues={"columnname": "oldvalue"},
values={"othercol": "newvalue"},
insertion_values={"thirdcol": "insertionval"},
)
)

self.mock_txn.execute.assert_called_with(
"INSERT INTO tablename (columnname, thirdcol, othercol) VALUES (?, ?, ?) ON CONFLICT (columnname) DO UPDATE SET othercol=EXCLUDED.othercol",
["oldvalue", "insertionval", "newvalue"],
)
self.assertTrue(result)

@defer.inlineCallbacks
def test_upsert_with_where(
self,
) -> Generator["defer.Deferred[object]", object, None]:
self.mock_txn.rowcount = 1

result = yield defer.ensureDeferred(
self.datastore.db_pool.simple_upsert(
table="tablename",
keyvalues={"columnname": "oldvalue"},
values={"othercol": "newvalue"},
where_clause="thirdcol IS NULL",
)
)

self.mock_txn.execute.assert_called_with(
"INSERT INTO tablename (columnname, othercol) VALUES (?, ?) ON CONFLICT (columnname) WHERE thirdcol IS NULL DO UPDATE SET othercol=EXCLUDED.othercol",
["oldvalue", "newvalue"],
)
self.assertTrue(result)