start writing test

This commit is contained in:
Daniel Roth 2026-03-06 10:55:40 +00:00
parent d33ec39aea
commit af01f7e5b0
3 changed files with 362 additions and 138 deletions

View file

@ -0,0 +1,61 @@
from datetime import datetime
import uuid
from sqlmodel import Session
from backend.app.db.models.tasks import SourceEnum, Task
def test_get_task_by_source(db_session: Session):
# arrange
current_categorisation_task = Task(
id=uuid.uuid4(),
task_source="",
job_started=datetime(2026, 1, 1, 9, 0, 0),
job_completed=None,
status="in progress",
service="plan_categorisation",
updated_at=datetime(2026, 1, 1, 9, 0, 0),
source=SourceEnum.PORTFOLIO,
source_id="100",
)
previous_categorisation_task = Task(
id=uuid.uuid4(),
task_source="",
job_started=datetime(2025, 12, 31, 9, 0, 0),
job_completed=datetime(2025, 12, 31, 9, 10, 0),
status="complete",
service="plan_categorisation",
updated_at=datetime(2025, 12, 31, 9, 10, 0),
source=SourceEnum.PORTFOLIO,
source_id="100",
)
other_portfolio_categorisation_task = Task(
id=uuid.uuid4(),
task_source="",
job_started=datetime(2026, 1, 1, 9, 0, 0),
job_completed=None,
status="in progress",
service="plan_categorisation",
updated_at=datetime(2026, 1, 1, 9, 0, 0),
source=SourceEnum.PORTFOLIO,
source_id="101",
)
engine_task = Task(
id=uuid.uuid4(),
task_source="",
job_started=datetime(2026, 1, 1, 9, 0, 0),
job_completed=None,
status="in progress",
service="plan_engine",
updated_at=datetime(2026, 1, 1, 9, 0, 0),
source=SourceEnum.PORTFOLIO,
source_id="100",
)
# act
# assert

View file

@ -3,12 +3,25 @@ import numpy as np
from pathlib import Path
import time
from sqlmodel import Session
from backend.export.property_scenarios.main import process_export
from backend.export.property_scenarios.input_schema import ExportRequest
from backend.app.db.models.portfolio import PropertyModel, Epc, Portfolio, PortfolioStatus, PortfolioGoal, \
PropertyCreationStatus, PropertyDetailsEpcModel
from backend.app.db.models.recommendations import PlanModel, Recommendation, PlanRecommendations, \
RecommendationMaterials
from backend.app.db.models.portfolio import (
PropertyModel,
Epc,
Portfolio,
PortfolioStatus,
PortfolioGoal,
PropertyCreationStatus,
PropertyDetailsEpcModel,
)
from backend.app.db.models.recommendations import (
PlanModel,
Recommendation,
PlanRecommendations,
RecommendationMaterials,
)
from backend.app.db.models.materials import Material
from utils.logger import setup_logger
@ -22,7 +35,7 @@ def load_csv(name: str) -> pd.DataFrame:
return df
def test_default_export_integration(db_session):
def test_default_export_integration(db_session: Session):
# ----------------------------------------
# 1) Load csvs
# ----------------------------------------
@ -78,11 +91,13 @@ def test_default_export_integration(db_session):
else None
)
prop = PropertyModel(**{
col: row_dict[col]
for col in PropertyModel.__table__.columns.keys()
if col in row_dict
})
prop = PropertyModel(
**{
col: row_dict[col]
for col in PropertyModel.__table__.columns.keys()
if col in row_dict
}
)
prop.creation_status = PropertyCreationStatus[
row_dict["creation_status"].split(".")[-1]
@ -90,9 +105,7 @@ def test_default_export_integration(db_session):
prop.status = PortfolioStatus[row_dict["status"].split(".")[-1]]
if row_dict.get("current_epc_rating"):
prop.current_epc_rating = Epc[
row_dict["current_epc_rating"].split(".")[-1]
]
prop.current_epc_rating = Epc[row_dict["current_epc_rating"].split(".")[-1]]
properties.append(prop)
@ -112,7 +125,8 @@ def test_default_export_integration(db_session):
epc_data = {
col.name: row_dict[col.name]
for col in PropertyDetailsEpcModel.__table__.columns.values()
if col.name in row_dict and col.name not in ["id", "property_id", "portfolio_id"]
if col.name in row_dict
and col.name not in ["id", "property_id", "portfolio_id"]
}
epc = PropertyDetailsEpcModel(
@ -142,11 +156,13 @@ def test_default_export_integration(db_session):
row_dict["scenario_id"] = None
plan = PlanModel(**{
col: row_dict[col]
for col in PlanModel.__table__.columns.keys()
if col in row_dict
})
plan = PlanModel(
**{
col: row_dict[col]
for col in PlanModel.__table__.columns.keys()
if col in row_dict
}
)
plans.append(plan)
@ -158,11 +174,13 @@ def test_default_export_integration(db_session):
# ----------------------------------------
recs = [
Recommendation(**{
col: row[col]
for col in Recommendation.__table__.columns.keys()
if col in row
})
Recommendation(
**{
col: row[col]
for col in Recommendation.__table__.columns.keys()
if col in row
}
)
for _, row in recommendations_df.iterrows()
]
@ -203,28 +221,19 @@ def test_default_export_integration(db_session):
# ----------------------------------------
logger.info(
"Recommendation count in DB: %s",
db_session.query(Recommendation).count()
"Recommendation count in DB: %s", db_session.query(Recommendation).count()
)
logger.info(
"Property count in DB: %s",
db_session.query(PropertyModel).count()
)
logger.info("Property count in DB: %s", db_session.query(PropertyModel).count())
logger.info(
"Property EPC in DB: %s",
db_session.query(PropertyDetailsEpcModel).count()
"Property EPC in DB: %s", db_session.query(PropertyDetailsEpcModel).count()
)
logger.info(
"Plan count in DB: %s",
db_session.query(PlanModel).count()
)
logger.info("Plan count in DB: %s", db_session.query(PlanModel).count())
logger.info(
"PlanRecommendatons count in DB: %s",
db_session.query(PlanModel).count()
"PlanRecommendatons count in DB: %s", db_session.query(PlanModel).count()
)
logger.info("Starting process_export")
@ -232,17 +241,23 @@ def test_default_export_integration(db_session):
result = process_export(payload, session=db_session)
logger.info("process_export finished in %.2f seconds", time.perf_counter() - process_t0)
logger.info(
"process_export finished in %.2f seconds", time.perf_counter() - process_t0
)
# ----------------------------------------
# 8) Assertions
# ----------------------------------------
assert "default_plans" in result, "Expected 'default_plans' in export result, got {}".format(result.keys())
assert (
"default_plans" in result
), "Expected 'default_plans' in export result, got {}".format(result.keys())
df = result["default_plans"]
assert df.shape[0] == 10, "Expected 10 properties in the export, got {}".format(df.shape[0])
assert df.shape[0] == 10, "Expected 10 properties in the export, got {}".format(
df.shape[0]
)
failed = df[df["predicted_post_works_sap"] < 69]
failed_property_types = failed["property_type"].value_counts().to_dict()
@ -251,19 +266,28 @@ def test_default_export_integration(db_session):
assert failed.shape[0]
assert df["total_retrofit_cost"].sum() == 41706.585999999996, (
"Expected total retrofit cost to be 10000, got {}".format(df["total_retrofit_cost"].sum())
assert (
df["total_retrofit_cost"].sum() == 41706.585999999996
), "Expected total retrofit cost to be 10000, got {}".format(
df["total_retrofit_cost"].sum()
)
assert df["predicted_post_works_sap"].sum() == 698.1, (
"Expected total predicted post works SAP to be 698.1, got {}".format(df["predicted_post_works_sap"].sum())
assert (
df["predicted_post_works_sap"].sum() == 698.1
), "Expected total predicted post works SAP to be 698.1, got {}".format(
df["predicted_post_works_sap"].sum()
)
assert df["sap_points"].sum() == 100.10000000000001, (
"Expected total SAP points increase to be 100.10000000000001, got {}".format(df["sap_points"].sum())
assert (
df["sap_points"].sum() == 100.10000000000001
), "Expected total SAP points increase to be 100.10000000000001, got {}".format(
df["sap_points"].sum()
)
assert df.shape == (10, 95), "Expected dataframe shape to be (10, 11), got {}".format(df.shape)
assert df.shape == (
10,
95,
), "Expected dataframe shape to be (10, 11), got {}".format(df.shape)
def test_solar_with_battery_example(db_session):
@ -271,116 +295,251 @@ def test_solar_with_battery_example(db_session):
test_property_id = 1
portfolio_df = pd.DataFrame(
[{'id': test_portfolio_id, 'name': 'Example', 'budget': None,
'status': 'PortfolioStatus.SCOPING', 'goal': 'PortfolioGoal.NONE', 'cost': None, 'number_of_properties': None,
'co2_equivalent_savings': None, 'energy_savings': None, 'energy_cost_savings': None,
'property_valuation_increase': None, 'rental_yield_increase': None, 'total_work_hours': None,
'labour_days': None, 'created_at': '2026-02-12 21:23:37.862000+00:00',
'updated_at': '2026-02-12 21:23:37.862000+00:00', 'epc_breakdown_pre_retrofit': None,
'epc_breakdown_post_retrofit': None, 'n_units_to_retrofit': None, 'co2_per_unit_pre_retrofit': None,
'co2_per_unit_post_retrofit': None, 'energy_bill_per_unit_pre_retrofit': None,
'energy_bill_per_unit_post_retrofit': None, 'energy_consumption_per_unit_pre_retrofit': None,
'energy_consumption_per_unit_post_retrofit': None, 'valuation_improvement_per_unit': None,
'cost_per_unit': None, 'cost_per_co2_saved': None, 'cost_per_sap_point': None,
'valuation_return_on_investment': None}]
[
{
"id": test_portfolio_id,
"name": "Example",
"budget": None,
"status": "PortfolioStatus.SCOPING",
"goal": "PortfolioGoal.NONE",
"cost": None,
"number_of_properties": None,
"co2_equivalent_savings": None,
"energy_savings": None,
"energy_cost_savings": None,
"property_valuation_increase": None,
"rental_yield_increase": None,
"total_work_hours": None,
"labour_days": None,
"created_at": "2026-02-12 21:23:37.862000+00:00",
"updated_at": "2026-02-12 21:23:37.862000+00:00",
"epc_breakdown_pre_retrofit": None,
"epc_breakdown_post_retrofit": None,
"n_units_to_retrofit": None,
"co2_per_unit_pre_retrofit": None,
"co2_per_unit_post_retrofit": None,
"energy_bill_per_unit_pre_retrofit": None,
"energy_bill_per_unit_post_retrofit": None,
"energy_consumption_per_unit_pre_retrofit": None,
"energy_consumption_per_unit_post_retrofit": None,
"valuation_improvement_per_unit": None,
"cost_per_unit": None,
"cost_per_co2_saved": None,
"cost_per_sap_point": None,
"valuation_return_on_investment": None,
}
]
)
properties_df = pd.DataFrame(
[{'id': test_property_id, 'portfolio_id': test_portfolio_id, 'creation_status': 'PropertyCreationStatus.READY',
'uprn': 100090438731, 'landlord_property_id': 'BARR052', 'building_reference_number': 3460742868.0,
'status': 'PortfolioStatus.ASSESSMENT', 'address': '52, Barrack Street', 'postcode': 'CO1 2LR',
'has_pre_condition_report': True, 'has_recommendations': True, 'created_at': '2026-02-12 21:59:02.744427',
'updated_at': '2026-02-19 16:18:57.941443', 'property_type': 'House', 'built_form': 'End-Terrace',
'local_authority': 'Colchester', 'constituency': 'Colchester', 'number_of_rooms': 4.0, 'year_built': 1900.0,
'tenure': 'rental (private)', 'current_epc_rating': 'Epc.E', 'current_sap_points': 53.0,
'current_valuation': 0.0, 'installed_measures_sap_point_adjustment': 0.0,
'is_sap_points_adjusted_for_installed_measures': False, 'original_sap_points': 53.0}]
[
{
"id": test_property_id,
"portfolio_id": test_portfolio_id,
"creation_status": "PropertyCreationStatus.READY",
"uprn": 100090438731,
"landlord_property_id": "BARR052",
"building_reference_number": 3460742868.0,
"status": "PortfolioStatus.ASSESSMENT",
"address": "52, Barrack Street",
"postcode": "CO1 2LR",
"has_pre_condition_report": True,
"has_recommendations": True,
"created_at": "2026-02-12 21:59:02.744427",
"updated_at": "2026-02-19 16:18:57.941443",
"property_type": "House",
"built_form": "End-Terrace",
"local_authority": "Colchester",
"constituency": "Colchester",
"number_of_rooms": 4.0,
"year_built": 1900.0,
"tenure": "rental (private)",
"current_epc_rating": "Epc.E",
"current_sap_points": 53.0,
"current_valuation": 0.0,
"installed_measures_sap_point_adjustment": 0.0,
"is_sap_points_adjusted_for_installed_measures": False,
"original_sap_points": 53.0,
}
]
)
property_details_epc_df = pd.DataFrame(
[
{'id': 1534934, 'property_id': test_property_id, 'portfolio_id': test_portfolio_id,
'full_address': '48, Medcalf Road', 'lodgement_date': '2018-09-05', 'is_expired': False,
'total_floor_area': 68.0, 'walls': 'Solid brick, as built, no insulation', 'walls_rating': 1,
'roof': 'Pitched, no insulation', 'roof_rating': 1.0, 'floor': 'Solid, no insulation',
'floor_rating': None,
'windows': 'Fully double glazed', 'windows_rating': 4, 'heating': 'Boiler and radiators, mains gas',
'heating_rating': 4, 'heating_controls': 'Programmer, room thermostat and trvs',
'heating_controls_rating': 4,
'hot_water': 'From main system', 'hot_water_rating': 4,
'lighting': 'Low energy lighting in all fixed outlets', 'lighting_rating': 5,
'mainfuel': 'Mains gas not community', 'ventilation': 'natural', 'solar_pv': 0.0, 'solar_hot_water': False,
'wind_turbine': 0.0, 'floor_height': 2.55, 'number_heated_rooms': None, 'heat_loss_corridor': False,
'unheated_corridor_length': None, 'number_of_open_fireplaces': 0, 'number_of_extensions': 0,
'number_of_storeys': None, 'mains_gas': True, 'energy_tariff': 'Single',
'primary_energy_consumption': 278.0,
'co2_emissions': 3.81, 'current_energy_demand': 14643.366,
'current_energy_demand_heating_hotwater': 12185.6,
'estimated': False, 'sap_05_overwritten': False, 'sap_05_score': None, 'sap_05_epc_rating': None,
'heating_cost_current': 711.0628, 'hot_water_cost_current': 139.06198, 'lighting_cost_current': 70.770935,
'appliances_cost_current': 609.7844, 'gas_standing_charge': 128.0785,
'electricity_standing_charge': 199.8375,
'original_co2_emissions': 3.81, 'original_primary_energy_consumption': 278.0,
'original_current_energy_demand': 14643.366, 'original_current_energy_demand_heating_hotwater': 12185.6,
'installed_measures_co2_adjustment': 0.0, 'installed_measures_energy_demand_adjustment': 0.0,
'installed_measures_total_energy_bill_adjustment': 0.0, 'installed_measures_heat_demand_adjustment': 0.0,
'is_epc_adjusted_for_installed_measures': False}
{
"id": 1534934,
"property_id": test_property_id,
"portfolio_id": test_portfolio_id,
"full_address": "48, Medcalf Road",
"lodgement_date": "2018-09-05",
"is_expired": False,
"total_floor_area": 68.0,
"walls": "Solid brick, as built, no insulation",
"walls_rating": 1,
"roof": "Pitched, no insulation",
"roof_rating": 1.0,
"floor": "Solid, no insulation",
"floor_rating": None,
"windows": "Fully double glazed",
"windows_rating": 4,
"heating": "Boiler and radiators, mains gas",
"heating_rating": 4,
"heating_controls": "Programmer, room thermostat and trvs",
"heating_controls_rating": 4,
"hot_water": "From main system",
"hot_water_rating": 4,
"lighting": "Low energy lighting in all fixed outlets",
"lighting_rating": 5,
"mainfuel": "Mains gas not community",
"ventilation": "natural",
"solar_pv": 0.0,
"solar_hot_water": False,
"wind_turbine": 0.0,
"floor_height": 2.55,
"number_heated_rooms": None,
"heat_loss_corridor": False,
"unheated_corridor_length": None,
"number_of_open_fireplaces": 0,
"number_of_extensions": 0,
"number_of_storeys": None,
"mains_gas": True,
"energy_tariff": "Single",
"primary_energy_consumption": 278.0,
"co2_emissions": 3.81,
"current_energy_demand": 14643.366,
"current_energy_demand_heating_hotwater": 12185.6,
"estimated": False,
"sap_05_overwritten": False,
"sap_05_score": None,
"sap_05_epc_rating": None,
"heating_cost_current": 711.0628,
"hot_water_cost_current": 139.06198,
"lighting_cost_current": 70.770935,
"appliances_cost_current": 609.7844,
"gas_standing_charge": 128.0785,
"electricity_standing_charge": 199.8375,
"original_co2_emissions": 3.81,
"original_primary_energy_consumption": 278.0,
"original_current_energy_demand": 14643.366,
"original_current_energy_demand_heating_hotwater": 12185.6,
"installed_measures_co2_adjustment": 0.0,
"installed_measures_energy_demand_adjustment": 0.0,
"installed_measures_total_energy_bill_adjustment": 0.0,
"installed_measures_heat_demand_adjustment": 0.0,
"is_epc_adjusted_for_installed_measures": False,
}
]
)
plans_df = pd.DataFrame(
[
{'id': 0, 'name': None, 'portfolio_id': test_portfolio_id, 'property_id': test_property_id,
'scenario_id': 1060, 'created_at': '2026-02-19 16:14:45.560816', 'is_default': True,
'valuation_increase_lower_bound': 0.0302,
'valuation_increase_upper_bound': 0.07, 'valuation_increase_average': 0.048226666, 'plan_type': None,
'post_sap_points': 71.5, 'post_epc_rating': 'Epc.C', 'post_co2_emissions': 4.1813498,
'co2_savings': 0.71865046, 'post_energy_bill': 1447.5204, 'energy_bill_savings': 691.6662,
'post_energy_consumption': 15303.688, 'energy_consumption_savings': 3276.7622,
'valuation_post_retrofit': None, 'valuation_increase': None, 'cost_of_works': 6984.568,
'contingency_cost': 1003.9568}
{
"id": 0,
"name": None,
"portfolio_id": test_portfolio_id,
"property_id": test_property_id,
"scenario_id": 1060,
"created_at": "2026-02-19 16:14:45.560816",
"is_default": True,
"valuation_increase_lower_bound": 0.0302,
"valuation_increase_upper_bound": 0.07,
"valuation_increase_average": 0.048226666,
"plan_type": None,
"post_sap_points": 71.5,
"post_epc_rating": "Epc.C",
"post_co2_emissions": 4.1813498,
"co2_savings": 0.71865046,
"post_energy_bill": 1447.5204,
"energy_bill_savings": 691.6662,
"post_energy_consumption": 15303.688,
"energy_consumption_savings": 3276.7622,
"valuation_post_retrofit": None,
"valuation_increase": None,
"cost_of_works": 6984.568,
"contingency_cost": 1003.9568,
}
]
)
plan_recs_df = pd.DataFrame(
[{'id': 0, 'plan_id': 0, 'recommendation_id': 0}]
)
plan_recs_df = pd.DataFrame([{"id": 0, "plan_id": 0, "recommendation_id": 0}])
recommendations_df = pd.DataFrame(
[{'id': 0, 'property_id': test_property_id, 'created_at': '2026-02-19 16:14:45.560816',
'type': 'solar_pv', 'measure_type': 'solar_pv',
'description': 'Fit solar',
'estimated_cost': 10000, 'default': True, 'starting_u_value': None, 'new_u_value': None, 'sap_points': 1.5,
'heat_demand': 14.9, 'kwh_savings': 1041.2, 'co2_equivalent_savings': 0.2, 'energy_savings': 14.9,
'energy_cost_savings': 72.639015, 'property_valuation_increase': None, 'rental_yield_increase': None,
'total_work_hours': 4.16, 'labour_days': 1.0, 'already_installed': False, 'plan_name': 'whatever'}
]
[
{
"id": 0,
"property_id": test_property_id,
"created_at": "2026-02-19 16:14:45.560816",
"type": "solar_pv",
"measure_type": "solar_pv",
"description": "Fit solar",
"estimated_cost": 10000,
"default": True,
"starting_u_value": None,
"new_u_value": None,
"sap_points": 1.5,
"heat_demand": 14.9,
"kwh_savings": 1041.2,
"co2_equivalent_savings": 0.2,
"energy_savings": 14.9,
"energy_cost_savings": 72.639015,
"property_valuation_increase": None,
"rental_yield_increase": None,
"total_work_hours": 4.16,
"labour_days": 1.0,
"already_installed": False,
"plan_name": "whatever",
}
]
)
recommendations_materials_df = pd.DataFrame(
[
{
"id": 0, "recommendation_id": 0, "material_id": 0, "depth": None, "quantity": 1.0,
"id": 0,
"recommendation_id": 0,
"material_id": 0,
"depth": None,
"quantity": 1.0,
"quantity_unit": "part",
"estimated_cost": 10000, "created_at": '2026-02-19 16:14:45.560816',
"updated_at": '2026-02-19 16:14:45.560816',
"estimated_cost": 10000,
"created_at": "2026-02-19 16:14:45.560816",
"updated_at": "2026-02-19 16:14:45.560816",
}
]
)
materials_df = pd.DataFrame(
[
{'id': 0, 'type': 'solar_pv', 'description': 'Some solar product',
'depth': 75.0,
'depth_unit': 'mm', 'cost': None, 'cost_unit': 'gbp_per_m2', 'r_value_per_mm': 0.030303031,
'r_value_unit': 'square_meter_kelvin_per_watt', 'thermal_conductivity': 0.033,
'thermal_conductivity_unit': 'watt_per_meter_kelvin', 'link': 'Test',
'created_at': "'2026-02-19 16:14:45.560816", 'is_active': True,
'prime_material_cost': None,
'material_cost': 0.0, 'labour_cost': 0.0, 'labour_hours_per_unit': 0.0, 'plant_cost': 0.0,
'total_cost': 10000,
'notes': None, 'is_installer_quote': True, 'innovation_rate': 0.25, 'size': None, 'size_unit': None,
'includes_scaffolding': True, 'includes_battery': True, 'battery_size': 5.8}
{
"id": 0,
"type": "solar_pv",
"description": "Some solar product",
"depth": 75.0,
"depth_unit": "mm",
"cost": None,
"cost_unit": "gbp_per_m2",
"r_value_per_mm": 0.030303031,
"r_value_unit": "square_meter_kelvin_per_watt",
"thermal_conductivity": 0.033,
"thermal_conductivity_unit": "watt_per_meter_kelvin",
"link": "Test",
"created_at": "'2026-02-19 16:14:45.560816",
"is_active": True,
"prime_material_cost": None,
"material_cost": 0.0,
"labour_cost": 0.0,
"labour_hours_per_unit": 0.0,
"plant_cost": 0.0,
"total_cost": 10000,
"notes": None,
"is_installer_quote": True,
"innovation_rate": 0.25,
"size": None,
"size_unit": None,
"includes_scaffolding": True,
"includes_battery": True,
"battery_size": 5.8,
}
]
)
@ -463,7 +622,7 @@ def test_solar_with_battery_example(db_session):
already_installed=row.already_installed,
sap_points=row.sap_points,
type=row.type,
description=row.description
description=row.description,
)
db_session.add(rec)
db_session.flush()
@ -515,13 +674,15 @@ def test_solar_with_battery_example(db_session):
db_session.commit()
payload = ExportRequest.model_validate({
"task_id": "test",
"subtask_id": "test",
"portfolio_id": test_portfolio_id,
"scenario_ids": [],
"default_plans_only": True,
})
payload = ExportRequest.model_validate(
{
"task_id": "test",
"subtask_id": "test",
"portfolio_id": test_portfolio_id,
"scenario_ids": [],
"default_plans_only": True,
}
)
result = process_export(payload, session=db_session)
@ -534,7 +695,9 @@ def test_solar_with_battery_example(db_session):
# solar_pv should NOT exist
assert "solar_pv" not in df.columns
assert df.shape[0] == 1, "Expected 1 property in the export, got {}".format(df.shape[0])
assert df.shape[0] == 1, "Expected 1 property in the export, got {}".format(
df.shape[0]
)
# Cost should land in correct column
assert df["solar_pv_with_battery"].iloc[0] == 10000