Skip to content

Commit

Permalink
58 verification (#59)
Browse files Browse the repository at this point in the history
* test verify parameter

* added verify parameter to wrapper

* Update example test

See doc-string of test for details

* Format code base

* Add original #39 test back in (+ assure it fails)

* Add a tip block about verification

* Remove extra type hint from json assignment

---------

Co-authored-by: Henry Wilde <[email protected]>
  • Loading branch information
EdCussONS and daffidwilde authored Feb 16, 2024
1 parent d0c9c30 commit 47411c1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ Here's a basic example of how to use the `CensusAPI` class to retrieve a table:

```

> [!TIP]
> If you encounter SSL verification issues, you can bypass this step by
> setting the `verify` parameter when creating an instance of `CensusAPI`:
> ```python
> api_unverified = CensusAPI(verify=False)
> ```
> In general, this is <ins>not recommended</ins> as SSL verification helps
> ensure the security of your machine and its connection to the internet.
> Please use this at your own discretion.
## Limitations
The `CensusAPI` class includes a variety of methods to interact with the API in
Expand All @@ -99,6 +110,7 @@ query tables containing age data despite being able to create them through the
web interface. Again, this is a deliberate choice by the developers and may be
subject to change.
## Contributing
This project is open-source, and contributions from the community are welcome.
Expand Down
13 changes: 11 additions & 2 deletions src/census21api/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@


class CensusAPI:
"""A wrapper for the 2021 England and Wales Census API."""
"""A wrapper for the 2021 England and Wales Census API.
Parameters
----------
verify : bool
Whether to use SSL verification. Defaults to True.
"""

def __init__(self, verify: bool = True) -> None:
self.verify: bool = verify

def _process_response(self, response: Response) -> JSONLike:
"""
Expand Down Expand Up @@ -74,7 +83,7 @@ def get(self, url: str) -> JSONLike:
successful, and `None` otherwise.
"""

response = requests.get(url, verify=True)
response = requests.get(url, verify=self.verify)

return self._process_response(response)

Expand Down
35 changes: 33 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,46 @@


def test_issue_39_blocked_pairs():
"""https://github.com/datasciencecampus/census21api/issues/39"""
"""
https://github.com/datasciencecampus/census21api/issues/39
This test no longer works as is. See
`test_issue_39_blocked_pairs_gets_400_error` for an update to this
case. However, we keep this test for posterity, and test that it
**doesn't** work.
"""

population_type = "UR_HH"
area_type = "nat"
dimensions = ("economic_activity_status_12a", "industry_current_88a")

api = CensusAPI()

with pytest.warns(UserWarning) as w:
assert "blocked pair" not in w
table = api.query_table(population_type, area_type, dimensions)

assert table is None


def test_issue_39_blocked_pairs_gets_400_error():
"""
Originally, this was about catching blocked pairs.
See https://github.com/datasciencecampus/census21api/issues/39 for
details on the original issue.
As of 2024-02-16, it seems that blocked pairs now give a 400 error.
This test now checks for that.
"""

population_type = "UR_HH"
area_type = "nat"
dimensions = ("economic_activity_status_12a", "industry_current_88a")

api = CensusAPI()

with pytest.warns(UserWarning, match="blocked pair"):
with pytest.warns(UserWarning, match="Status code: 400"):
table = api.query_table(population_type, area_type, dimensions)

assert table is None
15 changes: 8 additions & 7 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
MOCK_URL = "mock://test.com/"


def test_init():
@given(st.booleans())
def test_init(verify: bool):
"""Test that the `CensusAPI` class can be instantiated correctly."""

api = CensusAPI()
api = CensusAPI(verify)

assert isinstance(api, CensusAPI)
assert vars(api) == {}
assert vars(api) == {"verify": verify}


@given(st.dictionaries(st.text(), st.text()))
Expand Down Expand Up @@ -91,11 +92,11 @@ def test_process_response_invalid_json():
assert data is None


@given(st.dictionaries(st.text(), st.text()))
def test_get(json):
@given(st.dictionaries(st.text(), st.text()), st.booleans())
def test_get(json, verify: bool):
"""Test that the API only gives data from successful responses."""

api = CensusAPI()
api = CensusAPI(verify)

with mock.patch("census21api.wrapper.requests.get") as get, mock.patch(
"census21api.wrapper.CensusAPI._process_response"
Expand All @@ -107,7 +108,7 @@ def test_get(json):

assert data == json

get.assert_called_once_with(MOCK_URL, verify=True)
get.assert_called_once_with(MOCK_URL, verify=verify)
process.assert_called_once_with(response)


Expand Down

0 comments on commit 47411c1

Please sign in to comment.