Move CurrencyConverter mocking to the base test class

This mock was already applied to all tests, because it was done statically
in the TestCurrencyConverter class definition.  But it was really not
clear that it's applied everywhere.

Moving this to the setUp() function makes it much clearer.

Also, remove useless redefinition in other tests.
This commit is contained in:
Baptiste Jonglez 2021-07-18 00:03:47 +02:00 committed by zorun
parent 748d30ba87
commit 6448d0d7df
2 changed files with 16 additions and 19 deletions

View file

@ -4,7 +4,6 @@ import json
import re import re
from time import sleep from time import sleep
import unittest import unittest
from unittest.mock import MagicMock
from flask import session from flask import session
import pytest import pytest
@ -1464,10 +1463,6 @@ class BudgetTestCase(IhatemoneyTestCase):
def test_currency_switch(self): def test_currency_switch(self):
mock_data = {"USD": 1, "EUR": 0.8, "CAD": 1.2, CurrencyConverter.no_currency: 1}
converter = CurrencyConverter()
converter.get_rates = MagicMock(return_value=mock_data)
# A project should be editable # A project should be editable
self.post_project("raclette") self.post_project("raclette")
@ -1559,14 +1554,16 @@ class BudgetTestCase(IhatemoneyTestCase):
}, },
) )
last_bill = project.get_bills().first() last_bill = project.get_bills().first()
expected_amount = converter.exchange_currency(last_bill.amount, "CAD", "EUR") expected_amount = self.converter.exchange_currency(
last_bill.amount, "CAD", "EUR"
)
assert last_bill.converted_amount == expected_amount assert last_bill.converted_amount == expected_amount
# Switch to USD. Now, NO bill should be in USD, since they already had a currency # Switch to USD. Now, NO bill should be in USD, since they already had a currency
project.switch_currency("USD") project.switch_currency("USD")
for bill in project.get_bills(): for bill in project.get_bills():
assert bill.original_currency != "USD" assert bill.original_currency != "USD"
expected_amount = converter.exchange_currency( expected_amount = self.converter.exchange_currency(
bill.amount, bill.original_currency, "USD" bill.amount, bill.original_currency, "USD"
) )
assert bill.converted_amount == expected_amount assert bill.converted_amount == expected_amount
@ -1583,7 +1580,7 @@ class BudgetTestCase(IhatemoneyTestCase):
"password": "demo", "password": "demo",
"contact_email": "demo@notmyidea.org", "contact_email": "demo@notmyidea.org",
"project_history": "y", "project_history": "y",
"default_currency": converter.no_currency, "default_currency": CurrencyConverter.no_currency,
}, },
) )
# A user displayed error should be generated, and its currency should be the same. # A user displayed error should be generated, and its currency should be the same.
@ -1593,10 +1590,6 @@ class BudgetTestCase(IhatemoneyTestCase):
def test_currency_switch_to_bill_currency(self): def test_currency_switch_to_bill_currency(self):
mock_data = {"USD": 1, "EUR": 0.8, "CAD": 1.2, CurrencyConverter.no_currency: 1}
converter = CurrencyConverter()
converter.get_rates = MagicMock(return_value=mock_data)
# Default currency is 'XXX', but we should start from a project with a currency # Default currency is 'XXX', but we should start from a project with a currency
self.post_project("raclette", default_currency="USD") self.post_project("raclette", default_currency="USD")
@ -1620,7 +1613,7 @@ class BudgetTestCase(IhatemoneyTestCase):
project = models.Project.query.get("raclette") project = models.Project.query.get("raclette")
bill = project.get_bills().first() bill = project.get_bills().first()
assert bill.converted_amount == converter.exchange_currency( assert bill.converted_amount == self.converter.exchange_currency(
bill.amount, "EUR", "USD" bill.amount, "EUR", "USD"
) )
@ -1631,10 +1624,6 @@ class BudgetTestCase(IhatemoneyTestCase):
def test_currency_switch_to_no_currency(self): def test_currency_switch_to_no_currency(self):
mock_data = {"USD": 1, "EUR": 0.8, "CAD": 1.2, CurrencyConverter.no_currency: 1}
converter = CurrencyConverter()
converter.get_rates = MagicMock(return_value=mock_data)
# Default currency is 'XXX', but we should start from a project with a currency # Default currency is 'XXX', but we should start from a project with a currency
self.post_project("raclette", default_currency="USD") self.post_project("raclette", default_currency="USD")
@ -1670,12 +1659,12 @@ class BudgetTestCase(IhatemoneyTestCase):
project = models.Project.query.get("raclette") project = models.Project.query.get("raclette")
for bill in project.get_bills_unordered(): for bill in project.get_bills_unordered():
assert bill.converted_amount == converter.exchange_currency( assert bill.converted_amount == self.converter.exchange_currency(
bill.amount, "EUR", "USD" bill.amount, "EUR", "USD"
) )
# And switch project to no currency: amount should be equal to what was submitted # And switch project to no currency: amount should be equal to what was submitted
project.switch_currency(converter.no_currency) project.switch_currency(CurrencyConverter.no_currency)
no_currency_bills = [ no_currency_bills = [
(bill.amount, bill.converted_amount) for bill in project.get_bills() (bill.amount, bill.converted_amount) for bill in project.get_bills()
] ]

View file

@ -1,9 +1,11 @@
import os import os
from unittest.mock import MagicMock
from flask_testing import TestCase from flask_testing import TestCase
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
from ihatemoney import models from ihatemoney import models
from ihatemoney.currency_convertor import CurrencyConverter
from ihatemoney.run import create_app, db from ihatemoney.run import create_app, db
@ -20,6 +22,12 @@ class BaseTestCase(TestCase):
def setUp(self): def setUp(self):
db.create_all() db.create_all()
# Add dummy data to CurrencyConverter for all tests (since it's a singleton)
mock_data = {"USD": 1, "EUR": 0.8, "CAD": 1.2, CurrencyConverter.no_currency: 1}
converter = CurrencyConverter()
converter.get_rates = MagicMock(return_value=mock_data)
# Also add it to an attribute to make tests clearer
self.converter = converter
def tearDown(self): def tearDown(self):
# clean after testing # clean after testing