Skip to content

Commit

Permalink
Update pytests for other_uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Nov 21, 2024
1 parent c716715 commit d78f715
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
27 changes: 24 additions & 3 deletions backend/lcfs/tests/other_uses/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def create_mock_entity(overrides: dict):
mock_entity = MagicMock(spec=OtherUses)
mock_entity.other_uses_id = 1
mock_entity.compliance_report_id = 1
mock_entity.fuel_type.fuel_type = "Gasoline"
mock_entity.fuel_category.category = "Petroleum-based"
mock_entity.expected_use.name = "Transportation"
mock_entity.ci_of_fuel = 20.0
mock_entity.units = "L"
mock_entity.rationale = "Test rationale"
mock_entity.group_uuid = "test-group-uuid"
Expand All @@ -20,6 +18,22 @@ def create_mock_entity(overrides: dict):
mock_entity.action_type = ActionTypeEnum.CREATE
mock_entity.user_type = UserTypeEnum.SUPPLIER

# Mock relationships
mock_entity.fuel_type = MagicMock()
mock_entity.fuel_type.fuel_type = "Gasoline"

mock_entity.fuel_category = MagicMock()
mock_entity.fuel_category.category = "Petroleum-based"

mock_entity.expected_use = MagicMock()
mock_entity.expected_use.name = "Transportation"

mock_entity.provision_of_the_act = MagicMock()
mock_entity.provision_of_the_act.name = "Provision A"

mock_entity.fuel_code = MagicMock()
mock_entity.fuel_code.fuel_code = "Code123"

# Apply overrides
if overrides:
for key, value in overrides.items():
Expand All @@ -30,15 +44,22 @@ def create_mock_entity(overrides: dict):

def create_mock_schema(overrides: dict):
mock_schema = OtherUsesCreateSchema(
other_uses_id=1,
compliance_report_id=1,
quantity_supplied=1000,
fuel_type="Gasoline",
fuel_category="Petroleum-based",
expected_use="Transportation",
provision_of_the_act="Provision A",
fuel_code="Code123",
units="L",
ci_of_fuel=20.0,
rationale="Test rationale",
deleted=False,
group_uuid="test-group-uuid",
version=1,
user_type="Supplier",
action_type="Create",
)

# Apply overrides
Expand Down
25 changes: 23 additions & 2 deletions backend/lcfs/tests/other_uses/test_other_uses_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,35 @@ async def test_get_table_options(other_uses_repo):
@pytest.mark.anyio
async def test_get_other_uses(other_uses_repo, mock_db_session):
compliance_report_id = 1
mock_other_use = create_mock_entity({})
mock_result_other_uses = [mock_other_use]
mock_compliance_report_uuid = "mock_group_uuid"

# Mock the first db.execute call for fetching compliance report group UUID
mock_first_execute = MagicMock()
mock_first_execute.scalar.return_value = mock_compliance_report_uuid

# Create a realistic mock other_use object
mock_other_use = MagicMock()
mock_other_use.other_uses_id = 1
mock_other_use.compliance_report_id = compliance_report_id
mock_other_use.quantity_supplied = 1000
mock_other_use.fuel_type = MagicMock(fuel_type="Gasoline")
mock_other_use.fuel_category = MagicMock(category="Petroleum-based")
mock_other_use.ci_of_fuel = 20.0
mock_other_use.provision_of_the_act = MagicMock(name="Provision A")
mock_other_use.provision_of_the_act.name = "Provision A"
mock_other_use.fuel_code = MagicMock(fuel_code="Code123")
mock_other_use.fuel_code.fuel_code = "Code123"
mock_other_use.expected_use = MagicMock(name="Transportation")
mock_other_use.expected_use.name = "Transportation"
mock_other_use.units = "L"
mock_other_use.rationale = "For testing purposes"
mock_other_use.group_uuid = mock_compliance_report_uuid
mock_other_use.version = 1
mock_other_use.user_type = "Supplier"
mock_other_use.action_type = "Create"

mock_result_other_uses = [mock_other_use]

# Mock the second db.execute call for fetching other uses
mock_second_execute = MagicMock()
mock_second_execute.unique.return_value.scalars.return_value.all.return_value = (
Expand Down
16 changes: 15 additions & 1 deletion backend/lcfs/tests/other_uses/test_other_uses_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ async def test_get_table_options(other_uses_service):
"fuel_types": [],
"units_of_measure": [],
"expected_uses": [],
"provisions_of_the_act":[],
"fuel_codes": [],
}
)

Expand All @@ -43,7 +45,12 @@ async def test_get_table_options(other_uses_service):
@pytest.mark.anyio
async def test_create_other_use(other_uses_service):
service, mock_repo, mock_fuel_repo = other_uses_service
other_use_data = create_mock_schema({})

# Mock the schema data
other_use_data = create_mock_schema({
"fuel_code": "Code123" # Add a valid fuel_code
})

mock_fuel_repo.get_fuel_category_by_name = AsyncMock(
return_value=MagicMock(fuel_category_id=1)
)
Expand All @@ -53,6 +60,12 @@ async def test_create_other_use(other_uses_service):
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(
return_value=MagicMock(expected_use_type_id=1)
)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(
return_value=MagicMock(provision_of_the_act_id=1)
)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(
return_value=MagicMock(fuel_code_id=1)
)

mock_created_use = create_mock_entity({})
mock_repo.create_other_use = AsyncMock(return_value=mock_created_use)
Expand All @@ -63,6 +76,7 @@ async def test_create_other_use(other_uses_service):
assert response.fuel_type == "Gasoline"
assert response.fuel_category == "Petroleum-based"
assert response.expected_use == "Transportation"
assert response.fuel_code == "Code123"

mock_repo.create_other_use.assert_awaited_once()

Expand Down
2 changes: 2 additions & 0 deletions backend/lcfs/tests/other_uses/test_other_uses_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def test_validate_compliance_report_id_success(other_uses_validation):
expected_use="Transportation",
units="L",
rationale="Test rationale",
provision_of_the_act="Provision A",
)
]

Expand All @@ -42,6 +43,7 @@ async def test_validate_compliance_report_id_failure(other_uses_validation):
expected_use="Transportation",
units="L",
rationale="Test rationale",
provision_of_the_act="Provision A",
)
]

Expand Down
23 changes: 21 additions & 2 deletions backend/lcfs/tests/other_uses/test_other_uses_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from lcfs.db.models.user.Role import RoleEnum
from lcfs.web.api.base import ComplianceReportRequestSchema
from lcfs.web.api.other_uses.schema import PaginatedOtherUsesRequestSchema
from lcfs.web.api.other_uses.schemas import OtherUsesSchema
from lcfs.web.api.other_uses.services import OtherUsesServices
from lcfs.web.api.other_uses.validation import OtherUsesValidation
from lcfs.tests.other_uses.conftest import create_mock_schema, create_mock_entity
Expand Down Expand Up @@ -136,9 +137,27 @@ async def test_save_other_uses_row_create(
) as mock_validate_organization_access:
set_mock_user(fastapi_app, [RoleEnum.SUPPLIER])
url = fastapi_app.url_path_for("save_other_uses_row")
payload = create_mock_schema({}).model_dump()
payload = {
"compliance_report_id": 1,
"quantity_supplied": 1000,
"fuel_type": "Gasoline",
"fuel_category": "Petroleum-based",
"expected_use": "Transportation",
"units": "L",
"rationale": "Test rationale",
"provision_of_the_act": "Provision A",
"fuel_code": "FuelCode123",
"group_uuid": "test-group-uuid",
"version": 1,
"user_type": "Supplier",
"action_type": "Create",
}

# Mock the service method to return a valid schema object
mock_other_uses_service.create_other_use.return_value = OtherUsesSchema(
**payload
)

mock_other_uses_service.create_other_use.return_value = payload
mock_validate_organization_access.return_value = True

fastapi_app.dependency_overrides[OtherUsesServices] = (
Expand Down

0 comments on commit d78f715

Please sign in to comment.