Skip to content

Commit

Permalink
tests: Avoid plain "assert" and use unittest helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Jonglez authored and zorun committed Jan 31, 2023
1 parent 2d5240d commit e185a15
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
62 changes: 32 additions & 30 deletions ihatemoney/tests/budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_invite(self):
),
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)
Expand All @@ -133,12 +133,12 @@ def test_multiple_join(self):
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"""
Expand All @@ -150,7 +150,7 @@ def test_invite_code_invalidation(self):
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
Expand All @@ -164,8 +164,8 @@ def test_invite_code_invalidation(self):
},
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)
Expand Down Expand Up @@ -942,13 +942,13 @@ def test_dashboard_project_deletion(self):
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")
Expand Down Expand Up @@ -1194,7 +1194,7 @@ def test_settle(self):
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):
Expand Down Expand Up @@ -1421,14 +1421,14 @@ def test_currency_switch(self):

# 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(
Expand All @@ -1443,13 +1443,13 @@ def test_currency_switch(self):
},
)
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")
Expand All @@ -1469,16 +1469,16 @@ def test_currency_switch(self):
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):
Expand Down Expand Up @@ -1524,14 +1524,15 @@ def test_currency_switch_to_bill_currency(self):
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
Expand Down Expand Up @@ -1569,16 +1570,17 @@ def test_currency_switch_to_no_currency(self):
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
project.switch_currency(CurrencyConverter.no_currency)
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")
Expand All @@ -1598,11 +1600,11 @@ def test_amount_is_null(self):
"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")
Expand Down Expand Up @@ -1645,12 +1647,12 @@ def test_amount_too_high(self):
"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__":
Expand Down
2 changes: 1 addition & 1 deletion ihatemoney/tests/history_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def test_delete_history_with_project(self):

# 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__":
Expand Down
10 changes: 5 additions & 5 deletions ihatemoney/tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_project_creation_with_captcha_case_insensitive(self):
"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:
Expand All @@ -329,7 +329,7 @@ def test_project_creation_with_captcha(self):
"default_currency": "USD",
},
)
assert len(models.Project.query.all()) == 0
self.assertEqual(len(models.Project.query.all()), 0)

c.post(
"/create",
Expand All @@ -342,7 +342,7 @@ def test_project_creation_with_captcha(self):
"captcha": "nope",
},
)
assert len(models.Project.query.all()) == 0
self.assertEqual(len(models.Project.query.all()), 0)

c.post(
"/create",
Expand All @@ -355,7 +355,7 @@ def test_project_creation_with_captcha(self):
"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("/")
Expand All @@ -369,7 +369,7 @@ def test_api_project_creation_does_not_need_captcha(self):
},
)
self.assertTrue(resp.status, 201)
assert len(models.Project.query.all()) == 1
self.assertEqual(len(models.Project.query.all()), 1)


class TestCurrencyConverter(unittest.TestCase):
Expand Down

0 comments on commit e185a15

Please sign in to comment.