From e185a157fb43f092982b09f30102afa12a716b19 Mon Sep 17 00:00:00 2001 From: Baptiste Jonglez Date: Tue, 31 Jan 2023 17:34:14 +0100 Subject: [PATCH] tests: Avoid plain "assert" and use unittest helper functions --- ihatemoney/tests/budget_test.py | 62 ++++++++++++++++---------------- ihatemoney/tests/history_test.py | 2 +- ihatemoney/tests/main_test.py | 10 +++--- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/ihatemoney/tests/budget_test.py b/ihatemoney/tests/budget_test.py index a4dcf9e0..06681f06 100644 --- a/ihatemoney/tests/budget_test.py +++ b/ihatemoney/tests/budget_test.py @@ -113,7 +113,7 @@ class BudgetTestCase(IhatemoneyTestCase): ), follow_redirects=True, ) - assert "Create a new project" in resp.data.decode("utf-8") + self.assertIn("Create a new project", resp.data.decode("utf-8")) # A token MUST have a point between payload and signature resp = self.client.get("/raclette/join/token.invalid", follow_redirects=True) @@ -133,12 +133,12 @@ class BudgetTestCase(IhatemoneyTestCase): self.client.get(invite_link) data = self.client.get("/tartiflette/").data.decode("utf-8") # First join is OK - assert 'href="/raclette/"' in data + self.assertIn('href="/raclette/"', data) # Second join shouldn't add a double link self.client.get(invite_link) data = self.client.get("/tartiflette/").data.decode("utf-8") - assert data.count('href="/raclette/"') == 1 + self.assertEqual(data.count('href="/raclette/"'), 1) def test_invite_code_invalidation(self): """Test that invitation link expire after code change""" @@ -150,7 +150,7 @@ class BudgetTestCase(IhatemoneyTestCase): self.client.post("/exit") response = self.client.get(link) # Link is valid - assert response.status_code == 302 + self.assertEqual(response.status_code, 302) # Change password to invalidate token # Other data are required, but useless for the test @@ -164,8 +164,8 @@ class BudgetTestCase(IhatemoneyTestCase): }, follow_redirects=True, ) - assert response.status_code == 200 - assert "alert-danger" not in response.data.decode("utf-8") + self.assertEqual(response.status_code, 200) + self.assertNotIn("alert-danger", response.data.decode("utf-8")) self.client.post("/exit") response = self.client.get(link, follow_redirects=True) @@ -942,13 +942,13 @@ class BudgetTestCase(IhatemoneyTestCase): resp = self.client.get("/dashboard") pattern = re.compile(r"
]*?action=\"(.*?)\"") match = pattern.search(resp.data.decode("utf-8")) - assert match is not None - assert match.group(1) is not None + self.assertIsNotNone(match) + self.assertIsNotNone(match.group(1)) resp = self.client.post(match.group(1)) # project removed - assert len(models.Project.query.all()) == 0 + self.assertEqual(len(models.Project.query.all()), 0) def test_statistics_page(self): self.post_project("raclette") @@ -1194,7 +1194,7 @@ class BudgetTestCase(IhatemoneyTestCase): members[t["receiver"]] += t["amount"] balance = self.get_project("raclette").balance for m, a in members.items(): - assert abs(a - balance[m.id]) < 0.01 + self.assertAlmostEqual(a, balance[m.id], delta=0.01) return def test_settle_zero(self): @@ -1421,14 +1421,14 @@ class BudgetTestCase(IhatemoneyTestCase): # First all converted_amount should be the same as amount, with no currency for bill in project.get_bills(): - assert bill.original_currency == CurrencyConverter.no_currency - assert bill.amount == bill.converted_amount + self.assertEqual(bill.original_currency, CurrencyConverter.no_currency) + self.assertEqual(bill.amount, bill.converted_amount) # Then, switch to EUR, all bills must have been changed to this currency project.switch_currency("EUR") for bill in project.get_bills(): - assert bill.original_currency == "EUR" - assert bill.amount == bill.converted_amount + self.assertEqual(bill.original_currency, "EUR") + self.assertEqual(bill.amount, bill.converted_amount) # Add a bill in EUR, the current default currency self.client.post( @@ -1443,13 +1443,13 @@ class BudgetTestCase(IhatemoneyTestCase): }, ) last_bill = project.get_bills().first() - assert last_bill.converted_amount == last_bill.amount + self.assertEqual(last_bill.converted_amount, last_bill.amount) # Erase all currencies project.switch_currency(CurrencyConverter.no_currency) for bill in project.get_bills(): - assert bill.original_currency == CurrencyConverter.no_currency - assert bill.amount == bill.converted_amount + self.assertEqual(bill.original_currency, CurrencyConverter.no_currency) + self.assertEqual(bill.amount, bill.converted_amount) # Let's go back to EUR to test conversion project.switch_currency("EUR") @@ -1469,16 +1469,16 @@ class BudgetTestCase(IhatemoneyTestCase): expected_amount = self.converter.exchange_currency( last_bill.amount, "CAD", "EUR" ) - assert last_bill.converted_amount == expected_amount + self.assertEqual(last_bill.converted_amount, expected_amount) # Switch to USD. Now, NO bill should be in USD, since they already had a currency project.switch_currency("USD") for bill in project.get_bills(): - assert bill.original_currency != "USD" + self.assertNotEqual(bill.original_currency, "USD") expected_amount = self.converter.exchange_currency( bill.amount, bill.original_currency, "USD" ) - assert bill.converted_amount == expected_amount + self.assertEqual(bill.converted_amount, expected_amount) # Switching back to no currency must fail with pytest.raises(ValueError): @@ -1524,14 +1524,15 @@ class BudgetTestCase(IhatemoneyTestCase): project = self.get_project("raclette") bill = project.get_bills().first() - assert bill.converted_amount == self.converter.exchange_currency( - bill.amount, "EUR", "USD" + self.assertEqual( + self.converter.exchange_currency(bill.amount, "EUR", "USD"), + bill.converted_amount, ) # And switch project to the currency from the bill we created project.switch_currency("EUR") bill = project.get_bills().first() - assert bill.converted_amount == bill.amount + self.assertEqual(bill.converted_amount, bill.amount) def test_currency_switch_to_no_currency(self): # Default currency is 'XXX', but we should start from a project with a currency @@ -1569,8 +1570,9 @@ class BudgetTestCase(IhatemoneyTestCase): project = self.get_project("raclette") for bill in project.get_bills_unordered(): - assert bill.converted_amount == self.converter.exchange_currency( - bill.amount, "EUR", "USD" + self.assertEqual( + self.converter.exchange_currency(bill.amount, "EUR", "USD"), + bill.converted_amount, ) # And switch project to no currency: amount should be equal to what was submitted @@ -1578,7 +1580,7 @@ class BudgetTestCase(IhatemoneyTestCase): no_currency_bills = [ (bill.amount, bill.converted_amount) for bill in project.get_bills() ] - assert no_currency_bills == [(5.0, 5.0), (10.0, 10.0)] + self.assertEqual(no_currency_bills, [(5.0, 5.0), (10.0, 10.0)]) def test_amount_is_null(self): self.post_project("raclette") @@ -1598,11 +1600,11 @@ class BudgetTestCase(IhatemoneyTestCase): "original_currency": "EUR", }, ) - assert '

' in resp.data.decode("utf-8") + self.assertIn('

', resp.data.decode("utf-8")) resp = self.client.get("/raclette/") # No bills, the previous one was not added - assert "No bills" in resp.data.decode("utf-8") + self.assertIn("No bills", resp.data.decode("utf-8")) def test_decimals_on_weighted_members_list(self): self.post_project("raclette") @@ -1645,12 +1647,12 @@ class BudgetTestCase(IhatemoneyTestCase): "original_currency": "EUR", }, ) - assert '

' in resp.data.decode("utf-8") + self.assertIn('

', resp.data.decode("utf-8")) # Without any check, the following request will fail. resp = self.client.get("/raclette/") # No bills, the previous one was not added - assert "No bills" in resp.data.decode("utf-8") + self.assertIn("No bills", resp.data.decode("utf-8")) if __name__ == "__main__": diff --git a/ihatemoney/tests/history_test.py b/ihatemoney/tests/history_test.py index 754d5a55..e9856fe1 100644 --- a/ihatemoney/tests/history_test.py +++ b/ihatemoney/tests/history_test.py @@ -656,7 +656,7 @@ class HistoryTestCase(IhatemoneyTestCase): # History should be equal to project creation history_list = history.get_history(self.get_project("raclette")) - assert len(history_list) == 1 + self.assertEqual(len(history_list), 1) if __name__ == "__main__": diff --git a/ihatemoney/tests/main_test.py b/ihatemoney/tests/main_test.py index b5e35c7a..b757f520 100644 --- a/ihatemoney/tests/main_test.py +++ b/ihatemoney/tests/main_test.py @@ -315,7 +315,7 @@ class CaptchaTestCase(IhatemoneyTestCase): "captcha": "éùüß", }, ) - assert len(models.Project.query.all()) == 1 + self.assertEqual(len(models.Project.query.all()), 1) def test_project_creation_with_captcha(self): with self.client as c: @@ -329,7 +329,7 @@ class CaptchaTestCase(IhatemoneyTestCase): "default_currency": "USD", }, ) - assert len(models.Project.query.all()) == 0 + self.assertEqual(len(models.Project.query.all()), 0) c.post( "/create", @@ -342,7 +342,7 @@ class CaptchaTestCase(IhatemoneyTestCase): "captcha": "nope", }, ) - assert len(models.Project.query.all()) == 0 + self.assertEqual(len(models.Project.query.all()), 0) c.post( "/create", @@ -355,7 +355,7 @@ class CaptchaTestCase(IhatemoneyTestCase): "captcha": "euro", }, ) - assert len(models.Project.query.all()) == 1 + self.assertEqual(len(models.Project.query.all()), 1) def test_api_project_creation_does_not_need_captcha(self): self.client.get("/") @@ -369,7 +369,7 @@ class CaptchaTestCase(IhatemoneyTestCase): }, ) self.assertTrue(resp.status, 201) - assert len(models.Project.query.all()) == 1 + self.assertEqual(len(models.Project.query.all()), 1) class TestCurrencyConverter(unittest.TestCase):