From 3e2a8a8a6850f4b6d5a7b7e8e16761d875a05306 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Wed, 4 Dec 2024 01:13:39 +0100 Subject: [PATCH] fix: Refactor fixtures --- tests/fixtures/household_fixtures.py | 39 ++++++++++++++++++++++++ tests/python/test_household.py | 45 +++++----------------------- 2 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 tests/fixtures/household_fixtures.py diff --git a/tests/fixtures/household_fixtures.py b/tests/fixtures/household_fixtures.py new file mode 100644 index 00000000..733b6857 --- /dev/null +++ b/tests/fixtures/household_fixtures.py @@ -0,0 +1,39 @@ +import pytest +import json +from unittest.mock import patch + + +SAMPLE_HOUSEHOLD_DATA = { + "data": {"people": {"person1": {"age": 30, "income": 50000}}}, + "label": "Test Household", +} + +SAMPLE_DB_ROW = { + "id": 1, + "country_id": "us", + "household_json": json.dumps(SAMPLE_HOUSEHOLD_DATA["data"]), + "household_hash": "some-hash", + "label": "Test Household", + "api_version": "3.0.0", +} + + +# These will be moved to the correct location once +# testing PR that creates folder structure is merged +@pytest.fixture +def mock_database(): + """Mock the database module.""" + with patch( + "policyengine_api.services.household_service.database" + ) as mock_db: + yield mock_db + + +@pytest.fixture +def mock_hash_object(): + """Mock the hash_object function.""" + with patch( + "policyengine_api.services.household_service.hash_object" + ) as mock: + mock.return_value = "some-hash" + yield mock diff --git a/tests/python/test_household.py b/tests/python/test_household.py index 6731864b..8741dc2c 100644 --- a/tests/python/test_household.py +++ b/tests/python/test_household.py @@ -1,48 +1,17 @@ import pytest -from flask import Flask import json from unittest.mock import MagicMock, patch from sqlalchemy.engine.row import LegacyRow from policyengine_api.routes.household_routes import household_bp from policyengine_api.services.household_service import HouseholdService -from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS - -# TODO: Check if this format is correct -SAMPLE_HOUSEHOLD_DATA = { - "data": {"people": {"person1": {"age": 30, "income": 50000}}}, - "label": "Test Household", -} - -SAMPLE_DB_ROW = { - "id": 1, - "country_id": "us", - "household_json": json.dumps(SAMPLE_HOUSEHOLD_DATA["data"]), - "household_hash": "some-hash", - "label": "Test Household", - "api_version": "3.0.0", -} - - -# These will be moved to the correct location once -# testing PR that creates folder structure is merged -@pytest.fixture -def mock_database(): - """Mock the database module.""" - with patch( - "policyengine_api.services.household_service.database" - ) as mock_db: - yield mock_db - - -@pytest.fixture -def mock_hash_object(): - """Mock the hash_object function.""" - with patch( - "policyengine_api.services.household_service.hash_object" - ) as mock: - mock.return_value = "some-hash" - yield mock + +from tests.fixtures.household_fixtures import ( + SAMPLE_HOUSEHOLD_DATA, + SAMPLE_DB_ROW, + mock_database, + mock_hash_object, +) class TestGetHousehold: