Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change TaskRegisty Client endpoints #347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion amt/clients/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def get_instrument_list(self) -> RepositoryContent:
return RepositoryContent.model_validate(response.json()["entries"])

def get_instrument(self, urn: str, version: str = "latest") -> Instrument:
response = self.client.get(f"{TaskRegistryAPIClient.base_url}/urns/", params={"version": version, "urn": urn})
response = self.client.get(
f"{TaskRegistryAPIClient.base_url}/instruments/urn/{urn}", params={"version": version}
)

if response.status_code != 200:
raise AMTNotFound()
Expand Down
75 changes: 69 additions & 6 deletions tests/api/routes/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
from amt.api.routes.project import set_path
from amt.models import Project
from httpx import AsyncClient
from pytest_httpx import HTTPXMock
from pytest_mock import MockFixture

from tests.constants import default_project, default_project_with_system_card, default_task
from tests.constants import (
TASK_REGISTRY_AIIA_CONTENT_PAYLOAD,
TASK_REGISTRY_CONTENT_PAYLOAD,
TASK_REGISTRY_LIST_PAYLOAD,
default_project,
default_project_with_system_card,
default_task,
)
from tests.database_test_utils import DatabaseTestUtils


Expand Down Expand Up @@ -67,7 +75,18 @@ async def test_get_system_card_unknown_project(client: AsyncClient) -> None:


@pytest.mark.asyncio
async def test_get_assessment_card(client: AsyncClient, db: DatabaseTestUtils) -> None:
async def test_get_assessment_card(client: AsyncClient, httpx_mock: HTTPXMock, db: DatabaseTestUtils) -> None:
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
# given
await db.given([default_project_with_system_card("testproject1")])

Expand Down Expand Up @@ -109,7 +128,18 @@ async def test_get_assessment_card_unknown_assessment(client: AsyncClient, db: D


@pytest.mark.asyncio
async def test_get_model_card(client: AsyncClient, db: DatabaseTestUtils) -> None:
async def test_get_model_card(client: AsyncClient, httpx_mock: HTTPXMock, db: DatabaseTestUtils) -> None:
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
# given
await db.given([default_project_with_system_card("testproject1")])

Expand Down Expand Up @@ -161,7 +191,18 @@ async def test_get_project_details(client: AsyncClient, db: DatabaseTestUtils) -


@pytest.mark.asyncio
async def test_get_system_card_requirements(client: AsyncClient, db: DatabaseTestUtils) -> None:
async def test_get_system_card_requirements(client: AsyncClient, httpx_mock: HTTPXMock, db: DatabaseTestUtils) -> None:
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
# given
await db.given([default_project_with_system_card("testproject1"), default_task(project_id=1, status_id=1)])

Expand All @@ -175,7 +216,18 @@ async def test_get_system_card_requirements(client: AsyncClient, db: DatabaseTes


@pytest.mark.asyncio
async def test_get_system_card_data_page(client: AsyncClient, db: DatabaseTestUtils) -> None:
async def test_get_system_card_data_page(client: AsyncClient, httpx_mock: HTTPXMock, db: DatabaseTestUtils) -> None:
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
# given
await db.given([default_project_with_system_card("testproject1"), default_task(project_id=1, status_id=1)])

Expand All @@ -189,7 +241,18 @@ async def test_get_system_card_data_page(client: AsyncClient, db: DatabaseTestUt


@pytest.mark.asyncio
async def test_get_system_card_instruments(client: AsyncClient, db: DatabaseTestUtils) -> None:
async def test_get_system_card_instruments(client: AsyncClient, httpx_mock: HTTPXMock, db: DatabaseTestUtils) -> None:
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
# given
await db.given([default_project_with_system_card("testproject1"), default_task(project_id=1, status_id=1)])

Expand Down
4 changes: 2 additions & 2 deletions tests/clients/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_task_registry_api_client_get_instrument(httpx_mock: HTTPXMock):
# given
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)

Expand All @@ -48,7 +48,7 @@ def test_task_registry_api_client_get_instrument_not_succesfull(httpx_mock: HTTP
task_registry_api_client = TaskRegistryAPIClient()
httpx_mock.add_response(
status_code=408,
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
)

urn = "urn:nl:aivt:tr:iama:1.0"
Expand Down
34 changes: 34 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ def default_task(
"_links": {
"self": "https://minbzk.github.io/task-registry/instruments/iama.yaml"
}
},
{
"type": "file",
"size": 32897,
"name": "aiia.yaml",
"path": "instruments/aiia.yaml",
"urn": "urn:nl:aivt:tr:aiia:1.0",
"download_url": "https://minbzk.github.io/task-registry/instruments/aiia.yaml",
"_links": {
"self": "https://minbzk.github.io/task-registry/instruments/aiia.yaml"
}
}
]
}
Expand Down Expand Up @@ -118,3 +129,26 @@ def default_task(
"tasks": []
}
"""

TASK_REGISTRY_AIIA_CONTENT_PAYLOAD = """
{
"systemcard_path": ".assessments[]",
"schema_version": "1.1.0",
"name": "AI Impact Assessment (AIIA)",
"description": "Het IAMA helpt om de risico's voor mensenrechten bij het gebruik van algoritmen \
in kaart te brengen en maatregelen te nemen om deze aan te pakken.",
"urn": "urn:nl:aivt:tr:aiia:1.0",
"language": "nl",
"owners": [
{
"organization": "",
"name": "",
"email": "",
"role": ""
}
],
"date": "",
"url": "https://www.rijksoverheid.nl/documenten/rapporten/2021/02/25/impact-assessment-mensenrechten-en-algoritmes",
"tasks": []
}
"""
18 changes: 12 additions & 6 deletions tests/services/test_instruments_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from amt.services.instruments import InstrumentsService
from pytest_httpx import HTTPXMock
from tests.constants import (
TASK_REGISTRY_AIIA_CONTENT_PAYLOAD,
TASK_REGISTRY_CONTENT_PAYLOAD,
TASK_REGISTRY_LIST_PAYLOAD,
)
Expand All @@ -21,8 +22,9 @@ def test_fetch_urns(httpx_mock: HTTPXMock):
result = instruments_service.fetch_urns()

# then
assert len(result) == 1
assert len(result) == 2
assert result[0] == "urn:nl:aivt:tr:iama:1.0"
assert result[1] == "urn:nl:aivt:tr:aiia:1.0"


def test_fetch_instruments(httpx_mock: HTTPXMock):
Expand All @@ -33,15 +35,19 @@ def test_fetch_instruments(httpx_mock: HTTPXMock):
)

httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)

# when
result = instruments_service.fetch_instruments()

# then
assert len(result) == 1
assert len(result) == 2


def test_fetch_instrument_with_urn(httpx_mock: HTTPXMock):
Expand All @@ -51,7 +57,7 @@ def test_fetch_instrument_with_urn(httpx_mock: HTTPXMock):
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)

Expand All @@ -70,7 +76,7 @@ def test_fetch_instruments_with_urns(httpx_mock: HTTPXMock):
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)

Expand Down Expand Up @@ -105,7 +111,7 @@ def test_fetch_instruments_invalid(httpx_mock: HTTPXMock):
)

httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/urns/?version=latest&urn=urn%3Anl%3Aaivt%3Atr%3Aiama%3A1.0",
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=b'{"test": 1}',
)

Expand Down
60 changes: 55 additions & 5 deletions tests/services/test_instruments_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
get_next_tasks_per_instrument,
get_task_timestamp_from_assessment_card,
)
from tests.constants import default_instrument
from pytest_httpx import HTTPXMock
from tests.constants import (
TASK_REGISTRY_AIIA_CONTENT_PAYLOAD,
TASK_REGISTRY_CONTENT_PAYLOAD,
TASK_REGISTRY_LIST_PAYLOAD,
default_instrument,
)

# TODO: Add more cases of the system_card for coverage

Expand Down Expand Up @@ -171,8 +177,19 @@ def test_find_next_tasks_for_instrument_correct_lifecycle(system_card: SystemCar
assert tasks["tasks_per_lifecycle"][2] == test_tasks


def test_get_state_per_instrument(system_card: SystemCard):
def test_get_state_per_instrument(system_card: SystemCard, httpx_mock: HTTPXMock):
instrument_state_service = InstrumentStateService(system_card)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
res = instrument_state_service.get_state_per_instrument()
assert {"urn": "urn:nl:aivt:tr:aiia:1.0", "in_progress": 1, "name": "AI Impact Assessment (AIIA)"} in res
assert {
Expand All @@ -184,22 +201,55 @@ def test_get_state_per_instrument(system_card: SystemCard):
assert {"in_progress": 0, "name": "URN not found in Task Registry.", "urn": "urn:instrument:assessment"} in res


def test_get_amount_completed_instruments(system_card: SystemCard):
def test_get_amount_completed_instruments(system_card: SystemCard, httpx_mock: HTTPXMock):
instrument_state_service = InstrumentStateService(system_card)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
_ = instrument_state_service.get_state_per_instrument()
res = instrument_state_service.get_amount_completed_instruments()
assert res == 1


def test_get_amount_total_instruments(system_card: SystemCard):
def test_get_amount_total_instruments(system_card: SystemCard, httpx_mock: HTTPXMock):
instrument_state_service = InstrumentStateService(system_card)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
_ = instrument_state_service.get_state_per_instrument()
res = instrument_state_service.get_amount_total_instruments()
assert res == 3


def test_get_amount_completed_instruments_one_completed(system_card: SystemCard):
def test_get_amount_completed_instruments_one_completed(system_card: SystemCard, httpx_mock: HTTPXMock):
instrument_state_service = InstrumentStateService(system_card)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/", content=TASK_REGISTRY_LIST_PAYLOAD.encode()
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest",
content=TASK_REGISTRY_CONTENT_PAYLOAD.encode(),
)
httpx_mock.add_response(
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:aiia:1.0?version=latest",
content=TASK_REGISTRY_AIIA_CONTENT_PAYLOAD.encode(),
)
_ = instrument_state_service.get_state_per_instrument()
instrument_state_service.instrument_states = [{"in_progress": 0}, {"in_progress": 1}]
res = instrument_state_service.get_amount_completed_instruments()
Expand Down
Loading