Skip to content

Commit

Permalink
Record API constants (#34)
Browse files Browse the repository at this point in the history
* Write a test for creating an instance

* Create a file for the constants to live in

* Remove mention of constant data from API

* Add a workflow to check the constants every week

* Format wrapper module

* Format constants checker

* Add documentation for logger parameter
  • Loading branch information
daffidwilde authored Oct 27, 2023
1 parent a7d79ab commit 7fc4583
Show file tree
Hide file tree
Showing 7 changed files with 584 additions and 203 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/constants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI

on:
schedule:
- cron: '0 0 * * 1'

jobs:
constants:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: "pip"

- name: Update pip and install test dependencies
run: |
python -m pip install --upgrade pip
python -m pip install .
- name: Run API constants checker
run: |
python check_api_constants.py
87 changes: 87 additions & 0 deletions check_api_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Script to check whether our constants for the API are up to date."""

import requests
from centhesus import (
API_ROOT,
AREA_TYPES_BY_POPULATION_TYPE,
DIMENSIONS_BY_POPULATION_TYPE,
POPULATION_TYPES,
)


def _check_population_types():
"""Check that we have all the population types."""

url = f"{API_ROOT}?limit=100"
response = requests.get(url, verify=True)
data = response.json()

available_pop_types = set(
item["name"] for item in data["items"] if item["type"] == "microdata"
)

assert available_pop_types == set(POPULATION_TYPES), "\n".join(
(
"Population types do not match.",
f"Available: {', '.join(available_pop_types)}",
f"Recorded: {', '.join(POPULATION_TYPES)}",
)
)

print("✅ Population types up to date.")


def _check_area_types_by_population_type():
"""Check that we have the area types for each population type."""

for pop_type in POPULATION_TYPES:
url = "/".join((API_ROOT, pop_type, "area-types?limit=100"))
response = requests.get(url, verify=True)
data = response.json()

available_area_types = set(item["id"] for item in data["items"])
recorded_area_types = AREA_TYPES_BY_POPULATION_TYPE[pop_type]

assert available_area_types == set(recorded_area_types), "\n".join(
(
f"Area types for population type {pop_type} do not match.",
f"Available: {', '.join(available_area_types)}",
f"Recorded: {', '.join(recorded_area_types)}",
)
)

print("✅ Area types by population type up to date.")


def _check_dimensions_by_population_type():
"""Check that we have the dimensions for each population type."""

for pop_type in POPULATION_TYPES:
url = "/".join((API_ROOT, pop_type, "dimensions?limit=500"))
response = requests.get(url, verify=True)
data = response.json()

available_dimensions = set(item["id"] for item in data["items"])
recorded_dimensions = DIMENSIONS_BY_POPULATION_TYPE[pop_type]

assert available_dimensions == set(recorded_dimensions), "\n".join(
(
f"Dimensions for population type {pop_type} do not match.",
f"Available: {', '.join(available_dimensions)}",
f"Recorded: {', '.join(recorded_dimensions)}",
)
)

print("✅ Dimensions by population type up to date.")


def main():
"""Check all the API constants."""

_check_population_types()
_check_area_types_by_population_type()
_check_dimensions_by_population_type()


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ readme = "README.md"
requires-python = ">=3.8"
version = "0.0.1"
dependencies = [
"pandas==2.0.3",
"pandas<2.1",
"requests",
"tqdm",
"typing",
]

[project.optional-dependencies]
test = [
"hypothesis",
"pytest",
"pytest-randomly",
]
lint = [
"black",
Expand Down
5 changes: 3 additions & 2 deletions src/census21api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""A Python wrapper for the England and Wales Census 2021 API."""

from . import constants
from .interface import Interface
from .wrapper import APIWrapper
from .wrapper import CensusAPI

__all__ = ["APIWrapper", "Interface"]
__all__ = ["CensusAPI", "Interface", "constants"]
Loading

0 comments on commit 7fc4583

Please sign in to comment.