Skip to content

Commit

Permalink
Add tests for sorting and filtering params
Browse files Browse the repository at this point in the history
  • Loading branch information
whipps committed Dec 6, 2024
1 parent 71562d3 commit 131109d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ based_on_style = pep8
split_all_top_level_comma_separated_values=true

[flake8]
ignore = E126,E501,W50
ignore = E121,E126,E501,W50
15 changes: 10 additions & 5 deletions tests/integration/test_orders_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
24 changes: 20 additions & 4 deletions tests/integration/test_orders_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand Down
51 changes: 45 additions & 6 deletions tests/integration/test_subscriptions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -71,17 +71,40 @@ 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.
api_mock.route(M(url=TEST_URL),
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
Expand Down Expand Up @@ -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():
Expand Down
25 changes: 18 additions & 7 deletions tests/integration/test_subscriptions_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 131109d

Please sign in to comment.