Skip to content

Commit

Permalink
Modifiled the files with the changes requested
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeelSatasiya committed Feb 24, 2025
1 parent 9171339 commit 9dc027a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
41 changes: 21 additions & 20 deletions tests/fixtures/policy_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
import json
from unittest.mock import patch

valid_json_value = {
"data": {"gov.irs.income.bracket.rates.2": {"2024-01-01.2024-12-31": 0.2433}},
}

valid_hash_value = "NgJhpeuRVnIAwgYWuJsd2fI/N88rIE6Kcj8q4TPD/i4="

# Sample policy data
sample_policy_data = {
# Sample valid policy data
valid_policy_data = {
"id": 11,
"country_id": "us",
"label": None,
"api_version": "1.180.1",
"policy_json": json.dumps(
{"gov.irs.income.bracket.rates.2": {"2024-01-01.2024-12-31": 0.2433}}
),
"policy_hash": "NgJhpeuRVnIAwgYWuJsd2fI/N88rIE6Kcj8q4TPD/i4=",
"policy_json": json.dumps(valid_json_value["data"]),
"policy_hash": valid_hash_value,
}

valid_hash_value = "NgJhpeuRVnIAwgYWuJsd2fI/N88rIE6Kcj8q4TPD/i4="


@pytest.fixture
def mock_hash_object():
Expand All @@ -26,11 +27,11 @@ def mock_hash_object():
yield mock


@pytest.fixture
def mock_database():
"""Mock the database module."""
with patch("policyengine_api.services.policy_service.database") as mock_db:
yield mock_db
# @pytest.fixture
# def mock_database():
# """Mock the database module."""
# with patch("policyengine_api.services.policy_service.database") as mock_db:
# yield mock_db


@pytest.fixture
Expand All @@ -39,17 +40,17 @@ def existing_policy_record(test_db):
test_db.query(
"INSERT INTO policy (id, country_id, policy_json, policy_hash, label, api_version) VALUES (?, ?, ?, ?, ?, ?)",
(
sample_policy_data["id"],
sample_policy_data["country_id"],
sample_policy_data["policy_json"],
sample_policy_data["policy_hash"],
sample_policy_data["label"],
sample_policy_data["api_version"],
valid_policy_data["id"],
valid_policy_data["country_id"],
valid_policy_data["policy_json"],
valid_policy_data["policy_hash"],
valid_policy_data["label"],
valid_policy_data["api_version"],
),
)

inserted_row = test_db.query(
"SELECT * FROM policy WHERE id = ?", (sample_policy_data["id"],)
"SELECT * FROM policy WHERE id = ?", (valid_policy_data["id"],)
).fetchone()

return inserted_row
22 changes: 17 additions & 5 deletions tests/unit/services/test_get_policy_service.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import pytest
import json

from policyengine_api.services.policy_service import PolicyService

from tests.fixtures.policy_fixtures import (
sample_policy_data,
mock_database,
valid_json_value,
valid_hash_value,
valid_policy_data,
existing_policy_record,
)

Expand All @@ -19,13 +22,21 @@ def test_get_policy_given_existing_record(

# WHEN we call get_policy for this record...
result = service.get_policy(
existing_policy_record["country_id"], existing_policy_record["id"]
valid_policy_data["country_id"], valid_policy_data["id"]
)

valid_policy_json = json.loads(existing_policy_record["policy_json"])
expected_result = {
"id": valid_policy_data["id"],
"country_id": valid_policy_data["country_id"],
"label": valid_policy_data["label"],
"api_version": valid_policy_data["api_version"],
"policy_json": json.loads(valid_policy_data["policy_json"]),
"policy_hash": valid_policy_data["policy_hash"],
}

# THEN the result should contain the expected policy data
assert result["policy_json"] == valid_policy_json
assert result == expected_result


def test_get_policy_given_nonexistent_record(self, test_db):
# GIVEN an empty database (this is created by default)
Expand All @@ -38,6 +49,7 @@ def test_get_policy_given_nonexistent_record(self, test_db):
assert result is None

def test_get_policy_given_str_id(self):

# GIVEN an invalid ID
INVALID_RECORD_ID = "invalid"

Expand Down

0 comments on commit 9dc027a

Please sign in to comment.