From 0e6c0a395df0775d959870d90b9b5e81952bb49b Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Sun, 11 Aug 2024 13:50:03 +0200 Subject: [PATCH] Add missing tests --- tests/execution/test_stream.py | 185 +++++++++++++++++++++++++++++++-- 1 file changed, 177 insertions(+), 8 deletions(-) diff --git a/tests/execution/test_stream.py b/tests/execution/test_stream.py index d4dea0b0..1790350d 100644 --- a/tests/execution/test_stream.py +++ b/tests/execution/test_stream.py @@ -1093,7 +1093,7 @@ async def get_friend(i): return {"nonNullName": throw() if i < 0 else friends[i].name} def get_friends(_info): - return [get_friend(0), get_friend(-1), get_friend(1)] + return [get_friend(i) for i in (0, -1, 1)] result = await complete( document, @@ -1136,7 +1136,68 @@ def get_friends(_info): ] @pytest.mark.asyncio() - async def handles_async_error_in_complete_value_for_non_nullable_list(): + async def handles_nested_async_error_in_complete_value_after_initial_count(): + document = parse( + """ + query { + friendList @stream(initialCount: 1) { + nonNullName + } + } + """ + ) + + async def get_friend_name(i): + await sleep(0) + if i < 0: + raise RuntimeError("Oops") + return friends[i].name + + def get_friends(_info): + return [{"nonNullName": get_friend_name(i)} for i in (0, -1, 1)] + + result = await complete( + document, + { + "friendList": get_friends, + }, + ) + assert result == [ + { + "data": { + "friendList": [{"nonNullName": "Luke"}], + }, + "hasNext": True, + }, + { + "incremental": [ + { + "items": [None], + "path": ["friendList", 1], + "errors": [ + { + "message": "Oops", + "locations": [{"line": 4, "column": 17}], + "path": ["friendList", 1, "nonNullName"], + }, + ], + }, + ], + "hasNext": True, + }, + { + "incremental": [ + { + "items": [{"nonNullName": "Han"}], + "path": ["friendList", 2], + } + ], + "hasNext": False, + }, + ] + + @pytest.mark.asyncio() + async def handles_async_error_in_complete_value_after_initial_count_non_null(): document = parse( """ query { @@ -1155,7 +1216,7 @@ async def get_friend(i): return {"nonNullName": throw() if i < 0 else friends[i].name} def get_friends(_info): - return [get_friend(0), get_friend(-1), get_friend(1)] + return [get_friend(i) for i in (0, -1, 1)] result = await complete( document, @@ -1173,7 +1234,7 @@ def get_friends(_info): { "incremental": [ { - "items": None, + "items": [None], "path": ["nonNullFriendList", 1], "errors": [ { @@ -1189,7 +1250,59 @@ def get_friends(_info): ] @pytest.mark.asyncio() - async def handles_async_error_after_initial_count_reached_from_async_iterable(): + async def handles_nested_async_error_in_complete_value_after_initial_non_null(): + document = parse( + """ + query { + nonNullFriendList @stream(initialCount: 1) { + nonNullName + } + } + """ + ) + + async def get_friend_name(i): + await sleep(0) + if i < 0: + raise RuntimeError("Oops") + return friends[i].name + + def get_friends(_info): + return [{"nonNullName": get_friend_name(i)} for i in (0, -1, 1)] + + result = await complete( + document, + { + "nonNullFriendList": get_friends, + }, + ) + assert result == [ + { + "data": { + "nonNullFriendList": [{"nonNullName": "Luke"}], + }, + "hasNext": True, + }, + { + "incremental": [ + { + "items": None, + "path": ["nonNullFriendList", 1], + "errors": [ + { + "message": "Oops", + "locations": [{"line": 4, "column": 17}], + "path": ["friendList", 1, "nonNullName"], + }, + ], + }, + ], + "hasNext": False, + }, + ] + + @pytest.mark.asyncio() + async def handles_async_error_in_complete_value_after_initial_from_async_iterable(): document = parse( """ query { @@ -1208,9 +1321,8 @@ async def get_friend(i): return {"nonNullName": throw() if i < 0 else friends[i].name} async def get_friends(_info): - yield await get_friend(0) - yield await get_friend(-1) - yield await get_friend(1) + for i in 0, -1, 1: + yield await get_friend(i) result = await complete( document, @@ -1248,6 +1360,63 @@ async def get_friends(_info): "path": ["friendList", 2], }, ], + "hasNext": True, + }, + { + "hasNext": False, + }, + ] + + @pytest.mark.asyncio() + async def handles_async_error_in_complete_value_from_async_iterable_non_null(): + document = parse( + """ + query { + nonNullFriendList @stream(initialCount: 1) { + nonNullName + } + } + """ + ) + + async def throw(): + raise RuntimeError("Oops") + + async def get_friend(i): + await sleep(0) + return {"nonNullName": throw() if i < 0 else friends[i].name} + + async def get_friends(_info): + for i in 0, -1, 1: + yield await get_friend(i) + + result = await complete( + document, + { + "nonNullFriendList": get_friends, + }, + ) + assert result == [ + { + "data": { + "nonNullFriendList": [{"nonNullName": "Luke"}], + }, + "hasNext": True, + }, + { + "incremental": [ + { + "items": [None], + "path": ["nonNullFriendList", 1], + "errors": [ + { + "message": "Oops", + "locations": [{"line": 4, "column": 17}], + "path": ["friendList", 1, "nonNullName"], + }, + ], + }, + ], "hasNext": False, }, ]