generated from MinBZK/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67a26fd
commit be429e2
Showing
8 changed files
with
8,313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/fixtures/vcr_cassettes/test_fetch_task_with_invalid_urn.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
interactions: | ||
- request: | ||
body: "" | ||
headers: | ||
accept: | ||
- "*/*" | ||
accept-encoding: | ||
- gzip, deflate | ||
connection: | ||
- keep-alive | ||
host: | ||
- task-registry.apps.digilab.network | ||
user-agent: | ||
- python-httpx/0.27.2 | ||
method: GET | ||
uri: https://task-registry.apps.digilab.network/instruments/urn/invalid?version=latest | ||
response: | ||
body: | ||
string: '{"detail":"invalid urn: invalid"}' | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- "33" | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Fri, 15 Nov 2024 14:26:47 GMT | ||
Strict-Transport-Security: | ||
- max-age=31536000; includeSubDomains | ||
status: | ||
code: 400 | ||
message: Bad Request | ||
version: 1 |
291 changes: 291 additions & 0 deletions
291
tests/fixtures/vcr_cassettes/test_fetch_task_with_urn.yml
Large diffs are not rendered by default.
Oops, something went wrong.
724 changes: 724 additions & 0 deletions
724
tests/fixtures/vcr_cassettes/test_fetch_task_with_urns.yml
Large diffs are not rendered by default.
Oops, something went wrong.
323 changes: 323 additions & 0 deletions
323
tests/fixtures/vcr_cassettes/test_fetch_task_with_valid_and_invalid_urn.yml
Large diffs are not rendered by default.
Oops, something went wrong.
6,747 changes: 6,747 additions & 0 deletions
6,747
tests/fixtures/vcr_cassettes/test_fetch_tasks_all.yml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import pytest | ||
from pytest_httpx import HTTPXMock | ||
import vcr # type: ignore | ||
from amt.clients.clients import TaskRegistryAPIClient, TaskType | ||
from amt.core.exceptions import AMTInstrumentError | ||
from amt.models.task import Task | ||
from amt.repositories.task_registry import TaskRegistryRepository | ||
from tests.constants import TASK_REGISTRY_AIIA_CONTENT_PAYLOAD, TASK_REGISTRY_LIST_PAYLOAD | ||
|
||
# TODO(berry): made payloads to a better location | ||
|
||
|
||
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_fetch_tasks_all.yml") # type: ignore | ||
@pytest.mark.asyncio | ||
async def test_fetch_tasks_all(): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
|
||
# when | ||
instrument_result = await repository.fetch_tasks(TaskType.INSTRUMENTS) | ||
requirement_result = await repository.fetch_tasks(TaskType.REQUIREMENTS) | ||
measure_result = await repository.fetch_tasks(TaskType.MEASURES) | ||
|
||
# then | ||
assert len(instrument_result) == 4 | ||
assert len(requirement_result) == 60 | ||
assert len(measure_result) == 70 | ||
|
||
|
||
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_fetch_task_with_urn.yml") # type: ignore | ||
@pytest.mark.asyncio | ||
async def test_fetch_task_with_urn(): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
urn = "urn:nl:aivt:tr:iama:1.0" | ||
|
||
# when | ||
result = await repository.fetch_tasks(TaskType.INSTRUMENTS, urns=urn) | ||
|
||
# then | ||
assert len(result) == 1 | ||
assert "urn" in result[0] | ||
assert result[0]["urn"] == urn | ||
|
||
|
||
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_fetch_task_with_urns.yml") # type: ignore | ||
@pytest.mark.asyncio | ||
async def test_fetch_task_with_urns(): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
urns = ["urn:nl:aivt:tr:iama:1.0", "urn:nl:aivt:tr:aiia:1.0"] | ||
|
||
# when | ||
result = await repository.fetch_tasks(TaskType.INSTRUMENTS, urns=urns) | ||
|
||
# then | ||
assert len(result) == 2 | ||
assert "urn" in result[0] | ||
assert result[0]["urn"] == urns[0] | ||
assert "urn" in result[1] | ||
assert result[1]["urn"] == urns[1] | ||
|
||
|
||
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_fetch_task_with_invalid_urn.yml") # type: ignore | ||
@pytest.mark.asyncio | ||
async def test_fetch_task_with_invalid_urn(): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
urn = "invalid" | ||
|
||
# when | ||
result = await repository.fetch_tasks(TaskType.INSTRUMENTS, urns=urn) | ||
|
||
# then | ||
assert len(result) == 0 | ||
|
||
|
||
@vcr.use_cassette("tests/fixtures/vcr_cassettes/test_fetch_task_with_valid_and_invalid_urn.yml") # type: ignore | ||
@pytest.mark.asyncio | ||
async def test_fetch_task_with_valid_and_invalid_urn(): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
urns = ["urn:nl:aivt:tr:iama:1.0", "invalid"] | ||
|
||
# when | ||
result = await repository.fetch_tasks(TaskType.INSTRUMENTS, urns=urns) | ||
|
||
# then | ||
assert len(result) == 1 | ||
assert "urn" in result[0] | ||
assert result[0]["urn"] == urns[0] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_fetch_tasks_invalid_response(httpx_mock: HTTPXMock): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
urn = "urn:nl:aivt:tr:iama:1.0" | ||
|
||
httpx_mock.add_response( | ||
url="https://task-registry.apps.digilab.network/instruments/urn/urn:nl:aivt:tr:iama:1.0?version=latest", | ||
content=b'{"test": 1}', | ||
) | ||
|
||
# then | ||
with pytest.raises(AMTInstrumentError): | ||
await repository.fetch_tasks(TaskType.INSTRUMENTS, urn) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_fetch_tasks_valid_and_invalid_response(httpx_mock: HTTPXMock): | ||
# given | ||
client = TaskRegistryAPIClient() | ||
repository = TaskRegistryRepository(client) | ||
|
||
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:aiia:1.0?version=latest", | ||
content=TASK_REGISTRY_AIIA_CONTENT_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=b'{"test": 1}', | ||
) | ||
|
||
# then | ||
with pytest.raises(AMTInstrumentError): | ||
await repository.fetch_tasks(TaskType.INSTRUMENTS) |