Skip to content

Commit

Permalink
chore: migrate to pytest
Browse files Browse the repository at this point in the history
- replace setUp/tearDown with pytest fixtures
- rename test classes to use the pytest convention
- use pytest assertions

Co-authored-by: Glandos <[email protected]>
  • Loading branch information
azmeuk and Glandos committed Aug 12, 2023
1 parent 0187932 commit 03d9c55
Show file tree
Hide file tree
Showing 8 changed files with 751 additions and 766 deletions.
212 changes: 100 additions & 112 deletions ihatemoney/tests/api_test.py

Large diffs are not rendered by default.

470 changes: 223 additions & 247 deletions ihatemoney/tests/budget_test.py

Large diffs are not rendered by default.

42 changes: 7 additions & 35 deletions ihatemoney/tests/common/ihatemoney_testcase.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
import os
from unittest.mock import MagicMock

from flask_testing import TestCase
import pytest
from werkzeug.security import generate_password_hash

from ihatemoney import models
from ihatemoney.currency_convertor import CurrencyConverter
from ihatemoney.run import create_app, db


class BaseTestCase(TestCase):
@pytest.mark.usefixtures("client", "converter")
class BaseTestCase:
SECRET_KEY = "TEST SESSION"
SQLALCHEMY_DATABASE_URI = os.environ.get(
"TESTING_SQLALCHEMY_DATABASE_URI", "sqlite://"
)
ENABLE_CAPTCHA = False

def create_app(self):
# Pass the test object as a configuration.
return create_app(self)

def setUp(self):
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,
"PLN": 4,
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):
# clean after testing
db.session.remove()
db.drop_all()

def login(self, project, password=None, test_client=None):
password = password or project

Expand Down Expand Up @@ -81,7 +55,7 @@ def import_project(self, id, data, success=True):
data=data,
# follow_redirects=True,
)
self.assertEqual("/{id}/edit" in str(resp.response), not success)
assert ("/{id}/edit" in str(resp.response)) == (not success)

def create_project(self, id, default_currency="XXX", name=None, password=None):
name = name or str(id)
Expand All @@ -107,11 +81,9 @@ class IhatemoneyTestCase(BaseTestCase):
def assertStatus(self, expected, resp, url=None):
if url is None:
url = resp.request.path
return self.assertEqual(
expected,
resp.status_code,
f"{url} expected {expected}, got {resp.status_code}",
)
assert (
expected == resp.status_code
), f"{url} expected {expected}, got {resp.status_code}"

def enable_admin(self, password="adminpass"):
self.app.config["ACTIVATE_ADMIN_DASHBOARD"] = True
Expand Down
60 changes: 60 additions & 0 deletions ihatemoney/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from unittest.mock import MagicMock

from flask import Flask
import pytest

from ihatemoney.currency_convertor import CurrencyConverter
from ihatemoney.run import create_app, db


@pytest.fixture
def app(request: pytest.FixtureRequest):
"""Create the Flask app with database"""
app = create_app(request.cls)

with app.app_context():
db.create_all()
request.cls.app = app

yield app

# clean after testing
db.session.remove()
db.drop_all()


@pytest.fixture
def client(app: Flask, request: pytest.FixtureRequest):
client = app.test_client()
request.cls.client = client

yield client


@pytest.fixture
def app_ctx(app):
"""
This fixture add both app_context AND request context for ease of use.
If you only need an app_context locally, use `with self.app.app_context():`
in your code.
"""
with app.app_context(), app.test_request_context():
yield


@pytest.fixture
def converter(request: pytest.FixtureRequest):
# Add dummy data to CurrencyConverter for all tests (since it's a singleton)
mock_data = {
"USD": 1,
"EUR": 0.8,
"CAD": 1.2,
"PLN": 4,
CurrencyConverter.no_currency: 1,
}
converter = CurrencyConverter()
converter.get_rates = MagicMock(return_value=mock_data)
# Also add it to an attribute to make tests clearer
request.cls.converter = converter

yield converter
Loading

0 comments on commit 03d9c55

Please sign in to comment.