Skip to content

Commit

Permalink
parametrize test mock data
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Sep 28, 2023
1 parent e30f25d commit 8248629
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import json
import pytest
import typing


DATA_PATH = Path(__file__).parent / "data"
Expand Down Expand Up @@ -46,14 +47,16 @@ def mock_request_response(mock_session):
Prepares a mock response and status code for a given url and method.
"""

def _load_mock_request_response(name: str, url: str, method: str, code=200):
def _load_mock_request_response(
name: str, url: str, method: str, code=200
) -> dict[str, typing.Any]:
data = {}
for postfix in {"REQUEST", "RESPONSE"}:
with open(DATA_PATH / f"{name}.REQUEST.json") as f:
data[postfix.lower()] = json.load(f)
mock_session.register_uri(
method, url, json=data["response"]["body"], status_code=code
)
return data["request"]
return data

yield _load_mock_request_response
35 changes: 26 additions & 9 deletions tests/test_api_create.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
def test_api_create(mock_request_response):
from edutap.wallet_google.models.primitives.enums import State

import pytest


testdata = [
(
"Create",
"GenericObject",
{
"id": "3388000000022141777.obj53.test.ycc.edutap",
"classId": "3388000000022141777.test.ycc.edutap",
"state": State.ACTIVE,
},
),
]


@pytest.mark.parametrize("prefix,name,checkdata", testdata)
def test_api_create(mock_request_response, prefix, name, checkdata):
from edutap.wallet_google.api import create
from edutap.wallet_google.models.primitives.enums import State
from edutap.wallet_google.registry import lookup_model
from edutap.wallet_google.session import session_manager

request_data = mock_request_response(
"CreateGenericObject", session_manager.url("GenericObject"), "POST"
f"{prefix}{name}", session_manager.url(name), "POST"
)
result = create("GenericObject", request_data["body"])
result = create(name, request_data["request"]["body"])

GenericObject = lookup_model("GenericObject")
assert isinstance(result, GenericObject)
assert result.id == "3388000000022141777.obj53.test.ycc.edutap"
assert result.classId == "3388000000022141777.test.ycc.edutap"
assert result.state == State.ACTIVE
model = lookup_model(name)
assert isinstance(result, model)
for key, value in checkdata.items():
assert getattr(result, key) == value

0 comments on commit 8248629

Please sign in to comment.