diff --git a/src/flightplandb/user.py b/src/flightplandb/user.py index 718f7e2..04c94ab 100644 --- a/src/flightplandb/user.py +++ b/src/flightplandb/user.py @@ -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( @@ -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( @@ -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" + ) diff --git a/tests/test_user.py b/tests/test_user.py index 77b1d0f..162098a 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -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): @@ -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): @@ -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)