Added unit tests for property

This commit is contained in:
Khalim Conn-Kowlessar 2023-06-19 20:16:15 +01:00
parent 35a9679220
commit 148bd5e857

View file

@ -30,44 +30,83 @@ mock_epc_response = {
]
}
# Create a mock EPC client
mock_client = Mock(spec=EpcClient())
mock_client.domestic.search.return_value = mock_epc_response.copy()
mock_client.auth_token = "mocked_auth_token"
# Create a mock OpenUprnClient instance
mock_open_uprn_client = Mock(spec=OpenUprnClient(path=None, uprns=[12345]))
mock_open_uprn_client.data = pd.DataFrame(
[
{"UPRN": 12345, "longitude": 1.2345, "latitude": 2.3456},
{"UPRN": 12346, "longitude": 3.4567, "latitude": 4.5678}
mock_epc_response_dupe = {
'rows': [
{
'inspection-date': '2023-06-01', 'some-other-key': 'some-value', 'roof-description': 'Roof Description',
'walls-description': 'Walls Description', 'windows-description': 'Windows Description',
'mainheat-description': 'Main Heating Description', 'hotwater-description': 'Hot Water Description'
},
{
'inspection-date': '2023-05-01', 'some-other-key': 'some-other-value',
'roof-description': 'Roof Description', 'walls-description': 'Walls Description',
'windows-description': 'Windows Description', 'mainheat-description': 'Main Heating Description',
'hotwater-description': 'Hot Water Description'
},
{
'inspection-date': '2023-06-01', 'some-other-key': 'duplicate-date',
'roof-description': 'Roof Description',
'walls-description': 'Walls Description', 'windows-description': 'Windows Description',
'mainheat-description': 'Main Heating Description', 'hotwater-description': 'Hot Water Description'
}
]
)
# Create a mock EpcClean instance
mock_cleaner = Mock(spec=EpcClean(data=[
{"roof-description": "Roof Description"},
{"walls-description": "Walls Description"},
{"windows-description": "Windows Description"},
{"mainheat-description": "Main Heating Description"},
{"hotwater-description": "Hot Water Description"}
]))
mock_cleaner.cleaned = {
"roof-description": [{"original_description": "Roof Description"}],
"walls-description": [{"original_description": "Walls Description"}],
"windows-description": [{"original_description": "Windows Description"}],
"mainheat-description": [{"original_description": "Main Heating Description"}],
"hotwater-description": [{"original_description": "Hot Water Description"}]
}
class TestProperty:
@pytest.fixture
def property_instance(self):
return Property("AB12CD", "Test Address", epc_client=mock_client)
@pytest.fixture(autouse=True)
def property_instance(self, mock_epc_client, mock_open_uprn_client, mock_cleaner):
return Property("AB12CD", "Test Address", epc_client=mock_epc_client)
def test_init(self):
inst1 = Property("AB12CD", "Test Address", epc_client=mock_client)
@pytest.fixture(autouse=True)
def property_instance_dupe_data(self, mock_epc_client_dupe_data):
return Property("AB12CD", "Test Address", epc_client=mock_epc_client_dupe_data)
@pytest.fixture
def mock_epc_client(self):
mock_epc_client = Mock(spec=EpcClient())
mock_epc_client.domestic.search.return_value = mock_epc_response.copy()
mock_epc_client.auth_token = "mocked_auth_token"
return mock_epc_client
@pytest.fixture
def mock_epc_client_dupe_data(self):
mock_epc_client_dupe_data = Mock(spec=EpcClient())
mock_epc_client_dupe_data.domestic.search.return_value = mock_epc_response_dupe.copy()
mock_epc_client_dupe_data.auth_token = "mocked_auth_token"
return mock_epc_client_dupe_data
@pytest.fixture
def mock_open_uprn_client(self):
mock_open_uprn_client = Mock(spec=OpenUprnClient(path=None, uprns=[12345]))
mock_open_uprn_client.data = pd.DataFrame(
[
{"UPRN": 12345, "longitude": 1.2345, "latitude": 2.3456},
{"UPRN": 12346, "longitude": 3.4567, "latitude": 4.5678}
]
)
return mock_open_uprn_client
@pytest.fixture
def mock_cleaner(self):
mock_cleaner = Mock(spec=EpcClean(data=[
{"roof-description": "Roof Description"},
{"walls-description": "Walls Description"},
{"windows-description": "Windows Description"},
{"mainheat-description": "Main Heating Description"},
{"hotwater-description": "Hot Water Description"}
]))
mock_cleaner.cleaned = {
"roof-description": [{"original_description": "Roof Description"}],
"walls-description": [{"original_description": "Walls Description"}],
"windows-description": [{"original_description": "Windows Description"}],
"mainheat-description": [{"original_description": "Main Heating Description"}],
"hotwater-description": [{"original_description": "Hot Water Description"}]
}
return mock_cleaner
def test_init(self, mock_epc_client):
inst1 = Property("AB12CD", "Test Address", epc_client=mock_epc_client)
# Should be mocked auth token
assert inst1.epc_client.auth_token == "mocked_auth_token"
@ -87,21 +126,11 @@ class TestProperty:
# Verify that the correct data is being returned
assert property_instance.data == mock_epc_response["rows"][0]
def test_search_address_epc_multiple_results(self, property_instance):
# Modify the mock response to return two results with the same date
mock_client.domestic.search.return_value["rows"].append({
"inspection-date": "2023-06-01",
"some-other-key": "duplicate-date"
})
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.search_address_epc()
property_instance_dupe_data.search_address_epc()
# Reset the change
mock_client.domestic.search.return_value["rows"].pop(-1)
assert len(mock_client.domestic.search.return_value["rows"]) == 1
def test_get_coordinates(self, property_instance):
def test_get_coordinates(self, property_instance, mock_open_uprn_client):
# Set up the mock OpenUprnClient
property_instance.data = {"uprn": 12345}
property_instance.get_coordinates(mock_open_uprn_client)
@ -113,7 +142,7 @@ class TestProperty:
"latitude": 2.3456
}
def test_get_coordinates_without_open_uprn_data(self, property_instance):
def test_get_coordinates_without_open_uprn_data(self, property_instance, mock_open_uprn_client):
# Modify the mock OpenUprnClient to not have read any data
mock_open_uprn_client.data = None
@ -121,7 +150,7 @@ class TestProperty:
with pytest.raises(ValueError, match="OpenUprnClient has not read data"):
property_instance.get_coordinates(mock_open_uprn_client)
def test_get_components(self, property_instance):
def test_get_components(self, property_instance, mock_cleaner, mock_epc_client):
property_instance.search_address_epc()
property_instance.get_components(mock_cleaner)
@ -132,7 +161,7 @@ class TestProperty:
assert property_instance.main_heating == {"original_description": "Main Heating Description"}
assert property_instance.hotwater == {"original_description": "Hot Water Description"}
def test_get_components_without_cleaned_data(self, property_instance):
def test_get_components_without_cleaned_data(self, property_instance, mock_cleaner):
# Modify the mock EpcClean to not have cleaned data
mock_cleaner.cleaned = {}
@ -140,7 +169,7 @@ class TestProperty:
with pytest.raises(ValueError, match="Cleaner does not contain cleaned data"):
property_instance.get_components(mock_cleaner)
def test_get_components_no_attributes(self, property_instance):
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": []
@ -150,7 +179,7 @@ class TestProperty:
with pytest.raises(ValueError, match="Either No attributes or multiple found for roof-description"):
property_instance.get_components(mock_cleaner)
def test_get_components_multiple_attributes(self, property_instance):
def test_get_components_multiple_attributes(self, property_instance, mock_cleaner):
# This shouldn't happen - it would mean a cleaning error
property_instance.search_address_epc()
mock_cleaner.cleaned = {