From 131109d6b6a3b30c225eb349b17534a288972253 Mon Sep 17 00:00:00 2001 From: whipps Date: Thu, 5 Dec 2024 23:36:40 -0700 Subject: [PATCH] Add tests for sorting and filtering params --- setup.cfg | 2 +- tests/integration/test_orders_api.py | 15 ++++-- tests/integration/test_orders_cli.py | 24 ++++++++-- tests/integration/test_subscriptions_api.py | 51 ++++++++++++++++++--- tests/integration/test_subscriptions_cli.py | 25 +++++++--- 5 files changed, 94 insertions(+), 23 deletions(-) diff --git a/setup.cfg b/setup.cfg index 896717a9..49dc643d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,4 +25,4 @@ based_on_style = pep8 split_all_top_level_comma_separated_values=true [flake8] -ignore = E126,E501,W50 \ No newline at end of file +ignore = E121,E126,E501,W50 \ No newline at end of file diff --git a/tests/integration/test_orders_api.py b/tests/integration/test_orders_api.py index 3470436f..0257bf5f 100644 --- a/tests/integration/test_orders_api.py +++ b/tests/integration/test_orders_api.py @@ -121,8 +121,8 @@ async def test_list_orders_basic(order_descriptions, session): @respx.mock @pytest.mark.anyio -async def test_list_orders_state_success(order_descriptions, session): - list_url = TEST_ORDERS_URL + '?source_type=all&state=failed' +async def test_list_orders_filtering_and_sorting(order_descriptions, session): + list_url = TEST_ORDERS_URL + '?source_type=all&state=failed&name=my_order_xyz&name__contains=xyz&created_on=2018-02-12T00:00:00Z/..&last_modified=../2018-03-18T12:31:12Z&hosting=true&sort_by=name DESC' order1, order2, _ = order_descriptions @@ -136,10 +136,15 @@ async def test_list_orders_state_success(order_descriptions, session): cl = OrdersClient(session, base_url=TEST_URL) - # if the value of state doesn't get sent as a url parameter, + # if the value of each arg doesn't get sent as a url parameter, # the mock will fail and this test will fail - assert [order1, - order2] == [o async for o in cl.list_orders(state='failed')] + assert [order1, order2] == [ + o async for o in cl.list_orders( + state='failed', name='my_order_xyz', name__contains='xyz', + created_on='2018-02-12T00:00:00Z/..', + last_modified='../2018-03-18T12:31:12Z', hosting=True, + sort_by='name DESC') + ] @pytest.mark.anyio diff --git a/tests/integration/test_orders_cli.py b/tests/integration/test_orders_cli.py index c873877a..7b759dfb 100644 --- a/tests/integration/test_orders_cli.py +++ b/tests/integration/test_orders_cli.py @@ -84,8 +84,8 @@ def test_cli_orders_list_empty(invoke): @respx.mock -def test_cli_orders_list_state(invoke, order_descriptions): - list_url = TEST_ORDERS_URL + '?source_type=all&state=failed' +def test_cli_orders_list_filtering_and_sorting(invoke, order_descriptions): + list_url = TEST_ORDERS_URL + '?source_type=all&state=failed&name=my_order_xyz&name__contains=xyz&created_on=2018-02-12T00:00:00Z/..&last_modified=../2018-03-18T12:31:12Z&hosting=true&sort_by=name DESC' order1, order2, _ = order_descriptions @@ -97,9 +97,25 @@ def test_cli_orders_list_state(invoke, order_descriptions): mock_resp = httpx.Response(HTTPStatus.OK, json=page1_response) respx.get(list_url).return_value = mock_resp - # if the value of state doesn't get sent as a url parameter, + # if the value of each arg doesn't get sent as a url parameter, # the mock will fail and this test will fail - result = invoke(['list', '--state', 'failed']) + result = invoke([ + 'list', + '--state', + 'failed', + '--name', + 'my_order_xyz', + '--name-contains', + 'xyz', + '--created-on', + '2018-02-12T00:00:00Z/..', + '--last-modified', + '../2018-03-18T12:31:12Z', + '--hosting', + 'true', + '--sort-by', + 'name DESC' + ]) assert result.exit_code == 0 sequence = '\n'.join([json.dumps(o) for o in [order1, order2]]) assert result.output == sequence + '\n' diff --git a/tests/integration/test_subscriptions_api.py b/tests/integration/test_subscriptions_api.py index d237b63b..2b1c2f96 100644 --- a/tests/integration/test_subscriptions_api.py +++ b/tests/integration/test_subscriptions_api.py @@ -28,7 +28,7 @@ })) -def result_pages(status=None, size=40): +def subscription_pages(status=None, size=40): """Helper for creating fake subscriptions listing pages.""" all_subs = [{'id': str(i), 'status': 'running'} for i in range(1, 101)] select_subs = (sub for sub in all_subs @@ -54,7 +54,7 @@ def result_pages(status=None, size=40): api_mock.route(M(url=TEST_URL), M(params__contains={'status': 'running'})).mock(side_effect=[ Response(200, json=page) - for page in result_pages(status={'running'}, size=40) + for page in subscription_pages(status={'running'}, size=40) ]) # 2. Request for status: failed. Response has a single empty page. @@ -71,7 +71,7 @@ def result_pages(status=None, size=40): M(url=TEST_URL), M(params__contains={'source_type': 'catalog'})).mock(side_effect=[ Response(200, json=page) - for page in result_pages(status={'running'}, size=40) + for page in subscription_pages(status={'running'}, size=40) ]) # 5. source_type: soil_water_content requested. Response has a single empty page. @@ -79,9 +79,32 @@ def result_pages(status=None, size=40): M(params__contains={'source_type': 'soil_water_content'})).mock( side_effect=[Response(200, json={'subscriptions': []})]) -# 6. No status or source_type requested. Response is the same as for 1. -api_mock.route(M(url=TEST_URL)).mock( - side_effect=[Response(200, json=page) for page in result_pages(size=40)]) +# 6. All other parameters are used. Response has 2 subscriptions. +# The response is unrealistic here, but we are just testing the query parameter handling. +api_mock.route( + M(url=TEST_URL), + M( + params__contains={ + 'name': 'test xyz', + 'name__contains': 'xyz', + 'created': '2018-02-12T00:00:00Z/..', + 'updated': '../2018-03-18T12:31:12Z', + 'start_time': '2018-01-01T00:00:00Z', + 'end_time': '2022-01-01T00:00:00Z/2024-01-01T00:00:00Z', + 'hosting': 'true', + 'sort_by': 'name DESC', + })).mock(side_effect=[ + Response(200, json={'subscriptions': [{ + 'id': 1 + }, { + 'id': 2 + }]}) + ]) + +# 7. No status or source_type requested. Response is the same as for 1. +api_mock.route(M(url=TEST_URL)).mock(side_effect=[ + Response(200, json=page) for page in subscription_pages(size=40) +]) # The "creation", "update", and "cancel" mock APIs return submitted @@ -213,6 +236,22 @@ async def test_list_subscriptions_source_type_success( ]) == count +@pytest.mark.anyio +@api_mock +async def test_list_subscriptions_filtering_and_sorting(): + async with Session() as session: + client = SubscriptionsClient(session, base_url=TEST_URL) + assert len([ + sub async for sub in client.list_subscriptions( + name='test xyz', name__contains='xyz', + created='2018-02-12T00:00:00Z/..', + updated='../2018-03-18T12:31:12Z', + start_time='2018-01-01T00:00:00Z', + end_time='2022-01-01T00:00:00Z/2024-01-01T00:00:00Z', + hosting=True, sort_by='name DESC') + ]) == 2 + + @pytest.mark.anyio @failing_api_mock async def test_create_subscription_failure(): diff --git a/tests/integration/test_subscriptions_cli.py b/tests/integration/test_subscriptions_cli.py index 50bd8226..c10f8547 100644 --- a/tests/integration/test_subscriptions_cli.py +++ b/tests/integration/test_subscriptions_cli.py @@ -51,13 +51,24 @@ def _invoke(extra_args, runner=None, **kwargs): return _invoke -@pytest.mark.parametrize('options,expected_count', - [(['--status=running'], 100), ([], 100), - (['--source-type=catalog'], 100), - (['--source-type=soil_water_content'], 0), - (['--limit=1', '--status=running'], 1), - (['--limit=2', '--pretty', '--status=running'], 2), - (['--limit=1', '--status=preparing'], 0)]) +@pytest.mark.parametrize( + 'options,expected_count', + [(['--status=running'], 100), ([], 100), (['--source-type=catalog'], 100), + (['--source-type=soil_water_content'], 0), + (['--limit=1', '--status=running'], 1), + (['--limit=2', '--pretty', '--status=running'], 2), + (['--limit=1', '--status=preparing'], 0), + ([ + '--name=test xyz', + '--name-contains=xyz', + '--created=2018-02-12T00:00:00Z/..', + '--updated=../2018-03-18T12:31:12Z', + '--start-time=2018-01-01T00:00:00Z', + '--end-time=2022-01-01T00:00:00Z/2024-01-01T00:00:00Z', + '--hosting=true', + '--sort-by=name DESC' + ], + 2)]) @api_mock # Remember, parameters come before fixtures in the function definition. def test_subscriptions_list_options(invoke, options, expected_count):