Skip to content

Commit 131109d

Browse files
author
whipps
committed
Add tests for sorting and filtering params
1 parent 71562d3 commit 131109d

File tree

5 files changed

+94
-23
lines changed

5 files changed

+94
-23
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ based_on_style = pep8
2525
split_all_top_level_comma_separated_values=true
2626

2727
[flake8]
28-
ignore = E126,E501,W50
28+
ignore = E121,E126,E501,W50

tests/integration/test_orders_api.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ async def test_list_orders_basic(order_descriptions, session):
121121

122122
@respx.mock
123123
@pytest.mark.anyio
124-
async def test_list_orders_state_success(order_descriptions, session):
125-
list_url = TEST_ORDERS_URL + '?source_type=all&state=failed'
124+
async def test_list_orders_filtering_and_sorting(order_descriptions, session):
125+
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'
126126

127127
order1, order2, _ = order_descriptions
128128

@@ -136,10 +136,15 @@ async def test_list_orders_state_success(order_descriptions, session):
136136

137137
cl = OrdersClient(session, base_url=TEST_URL)
138138

139-
# if the value of state doesn't get sent as a url parameter,
139+
# if the value of each arg doesn't get sent as a url parameter,
140140
# the mock will fail and this test will fail
141-
assert [order1,
142-
order2] == [o async for o in cl.list_orders(state='failed')]
141+
assert [order1, order2] == [
142+
o async for o in cl.list_orders(
143+
state='failed', name='my_order_xyz', name__contains='xyz',
144+
created_on='2018-02-12T00:00:00Z/..',
145+
last_modified='../2018-03-18T12:31:12Z', hosting=True,
146+
sort_by='name DESC')
147+
]
143148

144149

145150
@pytest.mark.anyio

tests/integration/test_orders_cli.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def test_cli_orders_list_empty(invoke):
8484

8585

8686
@respx.mock
87-
def test_cli_orders_list_state(invoke, order_descriptions):
88-
list_url = TEST_ORDERS_URL + '?source_type=all&state=failed'
87+
def test_cli_orders_list_filtering_and_sorting(invoke, order_descriptions):
88+
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'
8989

9090
order1, order2, _ = order_descriptions
9191

@@ -97,9 +97,25 @@ def test_cli_orders_list_state(invoke, order_descriptions):
9797
mock_resp = httpx.Response(HTTPStatus.OK, json=page1_response)
9898
respx.get(list_url).return_value = mock_resp
9999

100-
# if the value of state doesn't get sent as a url parameter,
100+
# if the value of each arg doesn't get sent as a url parameter,
101101
# the mock will fail and this test will fail
102-
result = invoke(['list', '--state', 'failed'])
102+
result = invoke([
103+
'list',
104+
'--state',
105+
'failed',
106+
'--name',
107+
'my_order_xyz',
108+
'--name-contains',
109+
'xyz',
110+
'--created-on',
111+
'2018-02-12T00:00:00Z/..',
112+
'--last-modified',
113+
'../2018-03-18T12:31:12Z',
114+
'--hosting',
115+
'true',
116+
'--sort-by',
117+
'name DESC'
118+
])
103119
assert result.exit_code == 0
104120
sequence = '\n'.join([json.dumps(o) for o in [order1, order2]])
105121
assert result.output == sequence + '\n'

tests/integration/test_subscriptions_api.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}))
2929

3030

31-
def result_pages(status=None, size=40):
31+
def subscription_pages(status=None, size=40):
3232
"""Helper for creating fake subscriptions listing pages."""
3333
all_subs = [{'id': str(i), 'status': 'running'} for i in range(1, 101)]
3434
select_subs = (sub for sub in all_subs
@@ -54,7 +54,7 @@ def result_pages(status=None, size=40):
5454
api_mock.route(M(url=TEST_URL),
5555
M(params__contains={'status': 'running'})).mock(side_effect=[
5656
Response(200, json=page)
57-
for page in result_pages(status={'running'}, size=40)
57+
for page in subscription_pages(status={'running'}, size=40)
5858
])
5959

6060
# 2. Request for status: failed. Response has a single empty page.
@@ -71,17 +71,40 @@ def result_pages(status=None, size=40):
7171
M(url=TEST_URL),
7272
M(params__contains={'source_type': 'catalog'})).mock(side_effect=[
7373
Response(200, json=page)
74-
for page in result_pages(status={'running'}, size=40)
74+
for page in subscription_pages(status={'running'}, size=40)
7575
])
7676

7777
# 5. source_type: soil_water_content requested. Response has a single empty page.
7878
api_mock.route(M(url=TEST_URL),
7979
M(params__contains={'source_type': 'soil_water_content'})).mock(
8080
side_effect=[Response(200, json={'subscriptions': []})])
8181

82-
# 6. No status or source_type requested. Response is the same as for 1.
83-
api_mock.route(M(url=TEST_URL)).mock(
84-
side_effect=[Response(200, json=page) for page in result_pages(size=40)])
82+
# 6. All other parameters are used. Response has 2 subscriptions.
83+
# The response is unrealistic here, but we are just testing the query parameter handling.
84+
api_mock.route(
85+
M(url=TEST_URL),
86+
M(
87+
params__contains={
88+
'name': 'test xyz',
89+
'name__contains': 'xyz',
90+
'created': '2018-02-12T00:00:00Z/..',
91+
'updated': '../2018-03-18T12:31:12Z',
92+
'start_time': '2018-01-01T00:00:00Z',
93+
'end_time': '2022-01-01T00:00:00Z/2024-01-01T00:00:00Z',
94+
'hosting': 'true',
95+
'sort_by': 'name DESC',
96+
})).mock(side_effect=[
97+
Response(200, json={'subscriptions': [{
98+
'id': 1
99+
}, {
100+
'id': 2
101+
}]})
102+
])
103+
104+
# 7. No status or source_type requested. Response is the same as for 1.
105+
api_mock.route(M(url=TEST_URL)).mock(side_effect=[
106+
Response(200, json=page) for page in subscription_pages(size=40)
107+
])
85108

86109

87110
# The "creation", "update", and "cancel" mock APIs return submitted
@@ -213,6 +236,22 @@ async def test_list_subscriptions_source_type_success(
213236
]) == count
214237

215238

239+
@pytest.mark.anyio
240+
@api_mock
241+
async def test_list_subscriptions_filtering_and_sorting():
242+
async with Session() as session:
243+
client = SubscriptionsClient(session, base_url=TEST_URL)
244+
assert len([
245+
sub async for sub in client.list_subscriptions(
246+
name='test xyz', name__contains='xyz',
247+
created='2018-02-12T00:00:00Z/..',
248+
updated='../2018-03-18T12:31:12Z',
249+
start_time='2018-01-01T00:00:00Z',
250+
end_time='2022-01-01T00:00:00Z/2024-01-01T00:00:00Z',
251+
hosting=True, sort_by='name DESC')
252+
]) == 2
253+
254+
216255
@pytest.mark.anyio
217256
@failing_api_mock
218257
async def test_create_subscription_failure():

tests/integration/test_subscriptions_cli.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,24 @@ def _invoke(extra_args, runner=None, **kwargs):
5151
return _invoke
5252

5353

54-
@pytest.mark.parametrize('options,expected_count',
55-
[(['--status=running'], 100), ([], 100),
56-
(['--source-type=catalog'], 100),
57-
(['--source-type=soil_water_content'], 0),
58-
(['--limit=1', '--status=running'], 1),
59-
(['--limit=2', '--pretty', '--status=running'], 2),
60-
(['--limit=1', '--status=preparing'], 0)])
54+
@pytest.mark.parametrize(
55+
'options,expected_count',
56+
[(['--status=running'], 100), ([], 100), (['--source-type=catalog'], 100),
57+
(['--source-type=soil_water_content'], 0),
58+
(['--limit=1', '--status=running'], 1),
59+
(['--limit=2', '--pretty', '--status=running'], 2),
60+
(['--limit=1', '--status=preparing'], 0),
61+
([
62+
'--name=test xyz',
63+
'--name-contains=xyz',
64+
'--created=2018-02-12T00:00:00Z/..',
65+
'--updated=../2018-03-18T12:31:12Z',
66+
'--start-time=2018-01-01T00:00:00Z',
67+
'--end-time=2022-01-01T00:00:00Z/2024-01-01T00:00:00Z',
68+
'--hosting=true',
69+
'--sort-by=name DESC'
70+
],
71+
2)])
6172
@api_mock
6273
# Remember, parameters come before fixtures in the function definition.
6374
def test_subscriptions_list_options(invoke, options, expected_count):

0 commit comments

Comments
 (0)