Skip to content

Commit

Permalink
refactor: reduce duplicated code in tests (#1846)
Browse files Browse the repository at this point in the history
* refactor: reduce duplicated code in tests

* fix codacy issues
  • Loading branch information
waketzheng authored Jan 20, 2025
1 parent e34c2c0 commit 7340e6e
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions tests/test_two_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,30 @@ async def asyncTearDown(self) -> None:
await Tortoise._drop_databases()
await super().asyncTearDown()

def build_select_sql(self) -> str:
if isinstance(self.db, OracleClient):
return 'SELECT * FROM "eventtwo"'
return "SELECT * FROM eventtwo"

async def test_two_databases(self):
tournament = await Tournament.create(name="Tournament")
await EventTwo.create(name="Event", tournament_id=tournament.id)

select_sql = self.build_select_sql()
with self.assertRaises(OperationalError):
if isinstance(self.db, OracleClient):
await self.db.execute_query('SELECT * FROM "eventtwo"')
else:
await self.db.execute_query("SELECT * FROM eventtwo")
if isinstance(self.db, OracleClient):
_, results = await self.second_db.execute_query('SELECT * FROM "eventtwo"')
else:
_, results = await self.second_db.execute_query("SELECT * FROM eventtwo")
await self.db.execute_query(select_sql)
_, results = await self.second_db.execute_query(select_sql)
self.assertEqual(dict(results[0]), {"id": 1, "name": "Event", "tournament_id": 1})

async def test_two_databases_relation(self):
tournament = await Tournament.create(name="Tournament")
event = await EventTwo.create(name="Event", tournament_id=tournament.id)

select_sql = self.build_select_sql()
with self.assertRaises(OperationalError):
if isinstance(self.db, OracleClient):
await self.db.execute_query('SELECT * FROM "eventtwo"')
else:
await self.db.execute_query("SELECT * FROM eventtwo")
await self.db.execute_query(select_sql)

if isinstance(self.db, OracleClient):
_, results = await self.second_db.execute_query('SELECT * FROM "eventtwo"')
else:
_, results = await self.second_db.execute_query("SELECT * FROM eventtwo")
_, results = await self.second_db.execute_query(select_sql)
self.assertEqual(dict(results[0]), {"id": 1, "name": "Event", "tournament_id": 1})

teams = []
Expand Down

0 comments on commit 7340e6e

Please sign in to comment.