mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
updated property unit tests
This commit is contained in:
parent
ac556d5507
commit
756dca12ba
1 changed files with 88 additions and 69 deletions
|
|
@ -9,6 +9,7 @@ from etl.epc_clean.EpcClean import EpcClean
|
||||||
mock_epc_response = {
|
mock_epc_response = {
|
||||||
"rows": [
|
"rows": [
|
||||||
{
|
{
|
||||||
|
"tenure": "rental (social)",
|
||||||
"lmk-key": 1,
|
"lmk-key": 1,
|
||||||
"uprn": 1,
|
"uprn": 1,
|
||||||
"number-habitable-rooms": 5,
|
"number-habitable-rooms": 5,
|
||||||
|
|
@ -17,7 +18,7 @@ mock_epc_response = {
|
||||||
"inspection-date": "2023-06-01",
|
"inspection-date": "2023-06-01",
|
||||||
'lodgement-datetime': '2023-06-01 20:29:01',
|
'lodgement-datetime': '2023-06-01 20:29:01',
|
||||||
"some-other-key": "some-value",
|
"some-other-key": "some-value",
|
||||||
"roof-description": "Roof Description",
|
"roof-description": "pitched, no insulation",
|
||||||
"walls-description": "Walls Description",
|
"walls-description": "Walls Description",
|
||||||
"windows-description": "Windows Description",
|
"windows-description": "Windows Description",
|
||||||
"mainheat-description": "Main Heating Description",
|
"mainheat-description": "Main Heating Description",
|
||||||
|
|
@ -168,29 +169,54 @@ mock_epc_response_dupe = {
|
||||||
|
|
||||||
|
|
||||||
class TestProperty:
|
class TestProperty:
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def property_instance(self, mock_epc_client, mock_cleaner):
|
def mock_photo_supply_lookup(self):
|
||||||
property_instance = Property(1, "AB12CD", "Test Address", epc_client=mock_epc_client)
|
return pd.DataFrame(
|
||||||
|
[
|
||||||
|
dict(
|
||||||
|
tenure="rental (social)",
|
||||||
|
built_form="Detached",
|
||||||
|
property_type="House",
|
||||||
|
construction_age_band="England and Wales: 1967-1975",
|
||||||
|
is_flat=False,
|
||||||
|
is_pitched=True,
|
||||||
|
is_roof_room=False,
|
||||||
|
floor_area_decile=2,
|
||||||
|
photo_supply_median=40
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def mock_floor_area_decile_thresholds(self):
|
||||||
|
return pd.DataFrame(
|
||||||
|
{"floor_area_decile_thresholds": [0, 10, 30, 50]}
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def property_instance(self, mock_cleaner):
|
||||||
|
property_instance = Property(id=1, postcode="AB12CD", address="Test Address", data=mock_epc_response["rows"][0])
|
||||||
return property_instance
|
return property_instance
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def property_instance_dupe_data(self, mock_epc_client_dupe_data):
|
def property_instance_dupe_data(self):
|
||||||
property_instance_dupe_data = Property(2, "AB12CD", "Test Address", epc_client=mock_epc_client_dupe_data)
|
property_instance_dupe_data = Property(id=2, postcode="AB12CD", address="Test Address")
|
||||||
return property_instance_dupe_data
|
return property_instance_dupe_data
|
||||||
|
|
||||||
@pytest.fixture
|
# @pytest.fixture
|
||||||
def mock_epc_client(self):
|
# def mock_epc_client(self):
|
||||||
mock_epc_client = Mock(spec=EpcClient(auth_token="mocked_auth_token"))
|
# mock_epc_client = Mock(spec=EpcClient(auth_token="mocked_auth_token"))
|
||||||
mock_epc_client.domestic.search.return_value = mock_epc_response.copy()
|
# mock_epc_client.domestic.search.return_value = mock_epc_response.copy()
|
||||||
mock_epc_client.auth_token = "mocked_auth_token"
|
# mock_epc_client.auth_token = "mocked_auth_token"
|
||||||
return mock_epc_client
|
# return mock_epc_client
|
||||||
|
#
|
||||||
@pytest.fixture
|
# @pytest.fixture
|
||||||
def mock_epc_client_dupe_data(self):
|
# def mock_epc_client_dupe_data(self):
|
||||||
mock_epc_client_dupe_data = Mock(spec=EpcClient(auth_token="mocked_auth_token"))
|
# mock_epc_client_dupe_data = Mock(spec=EpcClient(auth_token="mocked_auth_token"))
|
||||||
mock_epc_client_dupe_data.domestic.search.return_value = mock_epc_response_dupe.copy()
|
# mock_epc_client_dupe_data.domestic.search.return_value = mock_epc_response_dupe.copy()
|
||||||
mock_epc_client_dupe_data.auth_token = "mocked_auth_token"
|
# mock_epc_client_dupe_data.auth_token = "mocked_auth_token"
|
||||||
return mock_epc_client_dupe_data
|
# return mock_epc_client_dupe_data
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_cleaner(self):
|
def mock_cleaner(self):
|
||||||
|
|
@ -229,7 +255,11 @@ class TestProperty:
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_cleaner.cleaned = {
|
mock_cleaner.cleaned = {
|
||||||
"roof-description": [{"original_description": "Roof Description"}],
|
"roof-description": [
|
||||||
|
{"original_description": "Roof Description"},
|
||||||
|
{"original_description": "pitched, no insulation", "is_pitched": True, "is_flat": False,
|
||||||
|
"is_roof_room": False}
|
||||||
|
],
|
||||||
"walls-description": [walls_data],
|
"walls-description": [walls_data],
|
||||||
"windows-description": [{"original_description": "Windows Description"}],
|
"windows-description": [{"original_description": "Windows Description"}],
|
||||||
"mainheat-description": [{"original_description": "Main Heating Description"}],
|
"mainheat-description": [{"original_description": "Main Heating Description"}],
|
||||||
|
|
@ -240,37 +270,32 @@ class TestProperty:
|
||||||
}
|
}
|
||||||
return mock_cleaner
|
return mock_cleaner
|
||||||
|
|
||||||
def test_init(self, mock_epc_client):
|
def test_init(self):
|
||||||
inst1 = Property(0, "AB12CD", "Test Address", epc_client=mock_epc_client)
|
inst1 = Property(0, postcode="AB12CD", address="Test Address")
|
||||||
# Should be mocked auth token
|
|
||||||
assert inst1.epc_client.auth_token == "mocked_auth_token"
|
|
||||||
|
|
||||||
inst2 = Property(3, "AB12CD", "Test Address", epc_client=mock_epc_client)
|
assert inst1.data is None
|
||||||
assert inst2.epc_client.auth_token
|
|
||||||
|
|
||||||
inst3 = Property(4, "AB12CD", "Test Address", data={"some": "data"}, epc_client=mock_epc_client)
|
inst2 = Property(3, "AB12CD", "Test Address")
|
||||||
assert inst3.data == {"some": "data"}
|
assert inst2.id == 3
|
||||||
|
|
||||||
data = inst3.search_address_epc()
|
inst3 = Property(4, "AB12CD", "Test Address", data={"some": "data", "uprn": 123})
|
||||||
assert data is None
|
assert inst3.data == {"some": "data", "uprn": 123}
|
||||||
|
|
||||||
def test_search_address_epc(self, property_instance):
|
def test_get_components(
|
||||||
# Call the method to test
|
self, property_instance, mock_cleaner, mock_photo_supply_lookup, mock_floor_area_decile_thresholds
|
||||||
property_instance.search_address_epc()
|
):
|
||||||
|
property_instance.get_components(
|
||||||
# Verify that the correct data is being returned
|
mock_cleaner.cleaned,
|
||||||
assert property_instance.data == mock_epc_response["rows"][0]
|
photo_supply_lookup=mock_photo_supply_lookup,
|
||||||
|
floor_area_decile_thresholds=mock_floor_area_decile_thresholds
|
||||||
def test_search_address_epc_multiple_results(self, property_instance_dupe_data, mock_epc_client_dupe_data):
|
)
|
||||||
with pytest.raises(Exception, match="More than one result found for this address - investigate me"):
|
|
||||||
property_instance_dupe_data.search_address_epc()
|
|
||||||
|
|
||||||
def test_get_components(self, property_instance, mock_cleaner, mock_epc_client):
|
|
||||||
property_instance.search_address_epc()
|
|
||||||
property_instance.get_components(mock_cleaner.cleaned)
|
|
||||||
|
|
||||||
# Verify that the components are set correctly
|
# Verify that the components are set correctly
|
||||||
assert property_instance.roof == {"original_description": "Roof Description"}
|
assert property_instance.roof == {
|
||||||
|
'original_description': 'pitched, no insulation', 'is_pitched': True,
|
||||||
|
'is_flat': False, 'is_roof_room': False
|
||||||
|
}
|
||||||
|
|
||||||
assert property_instance.walls == {
|
assert property_instance.walls == {
|
||||||
"original_description": "Walls Description",
|
"original_description": "Walls Description",
|
||||||
"is_cavity_wall": True,
|
"is_cavity_wall": True,
|
||||||
|
|
@ -294,24 +319,15 @@ class TestProperty:
|
||||||
|
|
||||||
# Verify that ValueError is raised when EpcClean doesn't contain cleaned data
|
# Verify that ValueError is raised when EpcClean doesn't contain cleaned data
|
||||||
with pytest.raises(ValueError, match="Cleaner does not contain cleaned data"):
|
with pytest.raises(ValueError, match="Cleaner does not contain cleaned data"):
|
||||||
property_instance.get_components(mock_cleaner.cleaned)
|
property_instance.get_components(mock_cleaner.cleaned, pd.DataFrame(), pd.DataFrame())
|
||||||
|
|
||||||
def test_get_components_no_data(self, property_instance, mock_cleaner):
|
def test_get_components_no_attributes(
|
||||||
|
self, property_instance, mock_cleaner, mock_photo_supply_lookup, mock_floor_area_decile_thresholds
|
||||||
|
):
|
||||||
# Modify the mock cleaner to have no attributes for a specific description
|
# Modify the mock cleaner to have no attributes for a specific description
|
||||||
mock_cleaner.cleaned = {
|
mock_cleaner.cleaned = {
|
||||||
"roof-description": []
|
"roof-description": []
|
||||||
}
|
}
|
||||||
|
|
||||||
# Verify that ValueError is raised when no attributes are found
|
|
||||||
with pytest.raises(ValueError, match="Property does not contain data"):
|
|
||||||
property_instance.get_components(mock_cleaner.cleaned)
|
|
||||||
|
|
||||||
def test_get_components_no_attributes(self, property_instance, mock_cleaner):
|
|
||||||
# Modify the mock cleaner to have no attributes for a specific description
|
|
||||||
mock_cleaner.cleaned = {
|
|
||||||
"roof-description": []
|
|
||||||
}
|
|
||||||
property_instance.search_address_epc()
|
|
||||||
property_instance.data["roof-description"] = "Pitched, no insulation"
|
property_instance.data["roof-description"] = "Pitched, no insulation"
|
||||||
property_instance.walls = {
|
property_instance.walls = {
|
||||||
"original_description": "Walls Description",
|
"original_description": "Walls Description",
|
||||||
|
|
@ -332,14 +348,17 @@ class TestProperty:
|
||||||
}
|
}
|
||||||
|
|
||||||
# Assert backup cleaning has been applied
|
# Assert backup cleaning has been applied
|
||||||
property_instance.get_components(mock_cleaner.cleaned)
|
property_instance.get_components(
|
||||||
|
mock_cleaner.cleaned, mock_photo_supply_lookup, mock_floor_area_decile_thresholds
|
||||||
|
)
|
||||||
|
|
||||||
assert property_instance.roof["clean_description"] == "Pitched, no insulation"
|
assert property_instance.roof["clean_description"] == "Pitched, no insulation"
|
||||||
assert property_instance.roof["is_pitched"]
|
assert property_instance.roof["is_pitched"]
|
||||||
|
|
||||||
def test_get_components_multiple_attributes(self, property_instance, mock_cleaner):
|
def test_get_components_multiple_attributes(
|
||||||
|
self, property_instance, mock_cleaner, mock_photo_supply_lookup, mock_floor_area_decile_thresholds
|
||||||
|
):
|
||||||
# This shouldn't happen - it would mean a cleaning error
|
# This shouldn't happen - it would mean a cleaning error
|
||||||
property_instance.search_address_epc()
|
|
||||||
property_instance.data["roof-description"] = "Roof Description"
|
property_instance.data["roof-description"] = "Roof Description"
|
||||||
cleaned = {
|
cleaned = {
|
||||||
"roof-description": [
|
"roof-description": [
|
||||||
|
|
@ -350,10 +369,10 @@ class TestProperty:
|
||||||
|
|
||||||
# Verify that ValueError is raised when multiple attributes are found
|
# Verify that ValueError is raised when multiple attributes are found
|
||||||
with pytest.raises(ValueError, match="Either No attributes or multiple found for roof-description"):
|
with pytest.raises(ValueError, match="Either No attributes or multiple found for roof-description"):
|
||||||
property_instance.get_components(cleaned)
|
property_instance.get_components(cleaned, mock_photo_supply_lookup, mock_floor_area_decile_thresholds)
|
||||||
|
|
||||||
def test_set_spatial(self, mock_epc_client):
|
def test_set_spatial(self):
|
||||||
prop = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop = Property(1, postcode="AB12CD", address="Test Address")
|
||||||
|
|
||||||
spatial1 = pd.DataFrame([{
|
spatial1 = pd.DataFrame([{
|
||||||
'X_COORDINATE': 411143.0, 'Y_COORDINATE': 281701.0, 'LATITUDE': 52.4331896, 'LONGITUDE': -1.8375238,
|
'X_COORDINATE': 411143.0, 'Y_COORDINATE': 281701.0, 'LATITUDE': 52.4331896, 'LONGITUDE': -1.8375238,
|
||||||
|
|
@ -367,7 +386,7 @@ class TestProperty:
|
||||||
assert prop.is_heritage
|
assert prop.is_heritage
|
||||||
assert prop.restricted_measures
|
assert prop.restricted_measures
|
||||||
|
|
||||||
prop2 = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop2 = Property(1, "AB12CD", "Test Address")
|
||||||
|
|
||||||
spatial2 = pd.DataFrame([{
|
spatial2 = pd.DataFrame([{
|
||||||
'X_COORDINATE': 411143.0, 'Y_COORDINATE': 281701.0, 'LATITUDE': 52.4331896, 'LONGITUDE': -1.8375238,
|
'X_COORDINATE': 411143.0, 'Y_COORDINATE': 281701.0, 'LATITUDE': 52.4331896, 'LONGITUDE': -1.8375238,
|
||||||
|
|
@ -381,10 +400,10 @@ class TestProperty:
|
||||||
assert not prop2.is_heritage
|
assert not prop2.is_heritage
|
||||||
assert not prop2.restricted_measures
|
assert not prop2.restricted_measures
|
||||||
|
|
||||||
def test_set_floor_level(self, mock_epc_client):
|
def test_set_floor_level(self):
|
||||||
# In this case, we have a flat which looks looks it's on the first floor, but it's actually on the ground
|
# In this case, we have a flat which looks looks it's on the first floor, but it's actually on the ground
|
||||||
# floor, so we should set floor_level to 0
|
# floor, so we should set floor_level to 0
|
||||||
prop = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop = Property(1, postcode="AB12CD", address="Test Address")
|
||||||
prop.data = {'floor-level': '01', 'property-type': 'Flat'}
|
prop.data = {'floor-level': '01', 'property-type': 'Flat'}
|
||||||
prop.floor = {
|
prop.floor = {
|
||||||
'original_description': 'Solid, no insulation (assumed)', 'clean_description': 'Solid, no insulation',
|
'original_description': 'Solid, no insulation (assumed)', 'clean_description': 'Solid, no insulation',
|
||||||
|
|
@ -400,7 +419,7 @@ class TestProperty:
|
||||||
|
|
||||||
# This property is labelled as being on the ground floor but actually has another property below
|
# This property is labelled as being on the ground floor but actually has another property below
|
||||||
# so we set floor level to 1
|
# so we set floor level to 1
|
||||||
prop2 = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop2 = Property(1, postcode="AB12CD", address="Test Address")
|
||||||
prop2.data = {'floor-level': 'Ground', 'property-type': 'Flat'}
|
prop2.data = {'floor-level': 'Ground', 'property-type': 'Flat'}
|
||||||
prop2.floor = {
|
prop2.floor = {
|
||||||
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
||||||
|
|
@ -415,7 +434,7 @@ class TestProperty:
|
||||||
assert prop2.floor_level == 1
|
assert prop2.floor_level == 1
|
||||||
|
|
||||||
# this property is correctly labelled as being on the 2nd floor
|
# this property is correctly labelled as being on the 2nd floor
|
||||||
prop3 = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop3 = Property(1, postcode="AB12CD", address="Test Address")
|
||||||
prop3.data = {'floor-level': '02', 'property-type': 'Flat'}
|
prop3.data = {'floor-level': '02', 'property-type': 'Flat'}
|
||||||
prop3.floor = {
|
prop3.floor = {
|
||||||
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
||||||
|
|
@ -430,7 +449,7 @@ class TestProperty:
|
||||||
assert prop3.floor_level == 2
|
assert prop3.floor_level == 2
|
||||||
|
|
||||||
# Example of a house
|
# Example of a house
|
||||||
prop4 = Property(1, "AB12CD", "Test Address", mock_epc_client)
|
prop4 = Property(1, postcode="AB12CD", address="Test Address")
|
||||||
prop4.data = {'floor-level': '', 'property-type': 'House'}
|
prop4.data = {'floor-level': '', 'property-type': 'House'}
|
||||||
prop4.floor = {
|
prop4.floor = {
|
||||||
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
'original_description': '(Another dwelling below)', 'clean_description': 'Solid, no insulation',
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue