From 9d1cdfcad1bbc877ad628224f277ac7659016754 Mon Sep 17 00:00:00 2001 From: Samantha Iacob Date: Tue, 24 Oct 2023 16:01:24 +0100 Subject: [PATCH 1/2] Second commit --- test_Census21_CACD_Wrapper.py | 42 ++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/test_Census21_CACD_Wrapper.py b/test_Census21_CACD_Wrapper.py index 8b21316..185d956 100644 --- a/test_Census21_CACD_Wrapper.py +++ b/test_Census21_CACD_Wrapper.py @@ -6,8 +6,44 @@ """ from Census21_CACD_Wrapper import APIWrapper +import pytest + +api = APIWrapper() + +# Test checks that the test can accurately complete a simple request def test_can_retrieve_usual_residents_sex_EW(): - api = APIWrapper(logger=False) - res = api.query_api('UR',f'sex',f'nat').iloc[0]['count'] - assert res == 30420202 # The number of female respondents in England & Wales \ No newline at end of file + res = api.query_api('UR','sex','nat').iloc[0]['count'] + assert res == 30420202 # The number of female respondents in England & Wales + +#Testing a batch of possible https response codes. +#This should ensure that the API wrapper only processes sucessful responses (200-299) +class Response: + def __init__(self, status_code): + self.status_code = status_code + +test_response1 = Response(120) +test_response2 = Response(200) +test_response3 = Response(298) +test_response4 = Response(301) +test_response5 = Response(445) +test_response6 = Response(586) + +response_value_test_cases = [ + (test_response1, False), + (test_response2, True), + (test_response3, True), + (test_response4, False), + (test_response5, False), + (test_response6, False) + ] + +@pytest.mark.parametrize("response_value, expected_outcome", response_value_test_cases) +def test_valid_status_code(response_value, expected_outcome): + assert api._valid_status_code(response_value) == expected_outcome + +# Trying to ensure that no errors are sneaking in the pop codes list. +def test_no_errors_in_pop_codes(): + api.set_population_types_available() + assert 'errors' not in api._valid_pop_types + From 8728fe3fdf50efef79cf5497de317117b158b22a Mon Sep 17 00:00:00 2001 From: Iacob Date: Wed, 25 Oct 2023 11:05:28 +0100 Subject: [PATCH 2/2] More tests --- test_Census21_CACD_Wrapper.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test_Census21_CACD_Wrapper.py b/test_Census21_CACD_Wrapper.py index 185d956..10ca14a 100644 --- a/test_Census21_CACD_Wrapper.py +++ b/test_Census21_CACD_Wrapper.py @@ -42,8 +42,13 @@ def __init__(self, status_code): def test_valid_status_code(response_value, expected_outcome): assert api._valid_status_code(response_value) == expected_outcome -# Trying to ensure that no errors are sneaking in the pop codes list. +# Trying to ensure that no errors are sneaking into the pop codes list undetected. + def test_no_errors_in_pop_codes(): api.set_population_types_available() assert 'errors' not in api._valid_pop_types +def test_get_responds_to_error_correctly(): + api.get('https://api.beta.ons.gov.uk/') + assert api._current_data != None +