Skip to content

Commit

Permalink
further type checking on user functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PH-KDX committed Jul 25, 2024
1 parent a1272bf commit 0d9c0b3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/flightplandb/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ async def plans(
async for i in internal.getiter(
path=f"/user/{username}/plans", sort=sort, limit=limit, key=key
):
yield Plan(**i)
if isinstance(i, dict):
yield Plan(**i)
else:
raise ValueError(
f"could not convert {i} to a Plan datatype; it is not a valid mapping"
)


async def likes(
Expand Down Expand Up @@ -121,7 +126,12 @@ async def likes(
async for i in internal.getiter(
path=f"/user/{username}/likes", sort=sort, limit=limit, key=key
):
yield Plan(**i)
if isinstance(i, dict):
yield Plan(**i)
else:
raise ValueError(
f"could not convert {i} to a Plan datatype; it is not a valid mapping"
)


async def search(
Expand Down Expand Up @@ -150,4 +160,9 @@ async def search(
async for i in internal.getiter(
path="/search/users", limit=limit, params={"q": username}, key=key
):
yield UserSmall(**i)
if isinstance(i, dict):
yield UserSmall(**i)
else:
raise ValueError(
f"could not convert {i} to a Plan datatype; it is not a valid mapping"
)
33 changes: 33 additions & 0 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ async def test_user_plans(patched_internal_getiter):
patched_internal_getiter.assert_has_calls(correct_calls)


@pytest.mark.allow_hosts(["127.0.0.1", "::1"])
@mock.patch("flightplandb.internal.getiter")
async def test_user_invalid_plan(patched_internal_getiter):
patched_internal_getiter.return_value = AsyncIter(["foobar"])

with pytest.raises(ValueError):
[x async for x in flightplandb.user.plans("lemon")]
# check that UserAPI method made correct request of FlightPlanDB
patched_internal_getiter.assert_called_once_with(path="/user/lemon/plans", sort="created", limit=100, key=None)


@pytest.mark.allow_hosts(["127.0.0.1", "::1"])
@mock.patch("flightplandb.internal.getiter")
async def test_user_likes(patched_internal_getiter):
Expand Down Expand Up @@ -362,6 +373,17 @@ async def test_user_likes(patched_internal_getiter):
patched_internal_getiter.assert_has_calls(correct_calls)


@pytest.mark.allow_hosts(["127.0.0.1", "::1"])
@mock.patch("flightplandb.internal.getiter")
async def test_user_invalid_liked_plan(patched_internal_getiter):
patched_internal_getiter.return_value = AsyncIter(["foobar"])

with pytest.raises(ValueError):
[x async for x in flightplandb.user.likes("lemon")]
# check that UserAPI method made correct request of FlightPlanDB
patched_internal_getiter.assert_called_once_with(path="/user/lemon/likes", sort="created", limit=100, key=None)


@pytest.mark.allow_hosts(["127.0.0.1", "::1"])
@mock.patch("flightplandb.internal.getiter")
async def test_user_search(patched_internal_getiter):
Expand Down Expand Up @@ -421,3 +443,14 @@ async def test_user_search(patched_internal_getiter):
assert response_list == correct_response_list
# check that UserAPI method made correct request of FlightPlanDB
patched_internal_getiter.assert_has_calls(correct_calls)


@pytest.mark.allow_hosts(["127.0.0.1", "::1"])
@mock.patch("flightplandb.internal.getiter")
async def test_user_invalid_searched_plan(patched_internal_getiter):
patched_internal_getiter.return_value = AsyncIter(["foobar"])

with pytest.raises(ValueError):
[x async for x in flightplandb.user.search("lemon")]
# check that UserAPI method made correct request of FlightPlanDB
patched_internal_getiter.assert_called_once_with(path="/search/users", limit=100, params={"q": "lemon"}, key=None)

0 comments on commit 0d9c0b3

Please sign in to comment.