tests: Avoid plain "assert" and use unittest helper functions

This commit is contained in:
Baptiste Jonglez 2023-01-31 17:34:14 +01:00 committed by zorun
parent 2d5240d3d6
commit e185a157fb
3 changed files with 38 additions and 36 deletions

View file

@ -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"<form id=\"delete-project\" [^>]*?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 '<p class="alert alert-danger">' in resp.data.decode("utf-8")
self.assertIn('<p class="alert alert-danger">', 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 '<p class="alert alert-danger">' in resp.data.decode("utf-8")
self.assertIn('<p class="alert alert-danger">', 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__":

View file

@ -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__":

View file

@ -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):