Skip to content

Commit c42f8cc

Browse files
committed
Reorder functions in test scripts.
The dependencies are now brought after the first main dependent function that uses them.
1 parent 506f122 commit c42f8cc

File tree

2 files changed

+65
-65
lines changed

2 files changed

+65
-65
lines changed

trolldb/tests/test_recorder.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ def config_file(tmp_path):
6262
return create_config_file(tmp_path)
6363

6464

65+
@pytest.mark.parametrize(("function", "args"), [
66+
(record_messages_from_config, lf("config_file")),
67+
(record_messages_from_command_line, [lf("config_file")])
68+
])
69+
async def test_record_from_cli_and_config(tmp_path, file_message, tmp_data_filename, function, args):
70+
"""Tests that message recording adds a message to the database either via configs from a file or the CLI."""
71+
msg = Message.decode(file_message)
72+
with running_prepared_database_context():
73+
with patched_subscriber_recv([file_message]):
74+
await function(args)
75+
assert await message_in_database_and_delete_count_is_one(msg)
76+
77+
6578
async def message_in_database_and_delete_count_is_one(msg: Message) -> bool:
6679
"""Checks if there is exactly one item in the database which matches the data of the message."""
6780
async with mongodb_context(test_app_config.database):
@@ -76,19 +89,6 @@ async def message_in_database_and_delete_count_is_one(msg: Message) -> bool:
7689
return result == msg.data and deletion_count == 1
7790

7891

79-
@pytest.mark.parametrize(("function", "args"), [
80-
(record_messages_from_config, lf("config_file")),
81-
(record_messages_from_command_line, [lf("config_file")])
82-
])
83-
async def test_record_from_cli_and_config(tmp_path, file_message, tmp_data_filename, function, args):
84-
"""Tests that message recording adds a message to the database either via configs from a file or the CLI."""
85-
msg = Message.decode(file_message)
86-
with running_prepared_database_context():
87-
with patched_subscriber_recv([file_message]):
88-
await function(args)
89-
assert await message_in_database_and_delete_count_is_one(msg)
90-
91-
9292
async def test_record_messages(config_file, tmp_path, file_message, tmp_data_filename):
9393
"""Tests that message recording adds a message to the database."""
9494
config = AppConfig(**make_test_app_config(tmp_path))

trolldb/tests/tests_api/test_api.py

+52-52
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,6 @@
2020
main_collection_name = test_app_config.database.main_collection_name
2121

2222

23-
def collections_exists(test_collection_names: list[str], expected_collection_name: list[str]) -> bool:
24-
"""Checks if the test and expected list of collection names match."""
25-
return Counter(test_collection_names) == Counter(expected_collection_name)
26-
27-
28-
def document_ids_are_correct(test_ids: list[str], expected_ids: list[str]) -> bool:
29-
"""Checks if the test (retrieved from the API) and expected list of (document) ids match."""
30-
return Counter(test_ids) == Counter(expected_ids)
31-
32-
33-
def single_query_is_correct(key: str, value: str | datetime) -> bool:
34-
"""Checks if the given single query, denoted by ``key`` matches correctly against the ``value``."""
35-
return (
36-
Counter(http_get(f"queries?{key}={value}").json()) ==
37-
Counter(TestDatabase.match_query(**{key: value}))
38-
)
39-
40-
41-
def make_query_string(keys: list[str], values_list: list[list[str] | datetime]) -> str:
42-
"""Makes a single query string for all the given queries."""
43-
query_buffer = []
44-
for key, value_list in zip(keys, values_list, strict=True):
45-
query_buffer += [f"{key}={value}" for value in value_list]
46-
return "&".join(query_buffer)
47-
48-
49-
def query_results_are_correct(keys: list[str], values_list: list[list[str] | datetime]) -> bool:
50-
"""Checks if the retrieved result from querying the database via the API matches the expected result.
51-
52-
There can be more than one query `key/value` pair.
53-
54-
Args:
55-
keys:
56-
A list of all query keys, e.g. ``keys=["platform", "sensor"]``
57-
58-
values_list:
59-
A list in which each element is a list of values itself. The `nth` element corresponds to the `nth` key in
60-
the ``keys``.
61-
62-
Returns:
63-
A boolean flag indicating whether the retrieved result matches the expected result.
64-
"""
65-
query_string = make_query_string(keys, values_list)
66-
67-
return (
68-
Counter(http_get(f"queries?{query_string}").json()) ==
69-
Counter(TestDatabase.match_query(
70-
**{label: value_list for label, value_list in zip(keys, values_list, strict=True)}
71-
))
72-
)
73-
74-
7523
@pytest.mark.usefixtures("_test_server_fixture")
7624
def test_root():
7725
"""Checks that the server is up and running, i.e. the root routes responds with 200."""
@@ -120,6 +68,16 @@ def test_collections():
12068
)
12169

12270

71+
def collections_exists(test_collection_names: list[str], expected_collection_name: list[str]) -> bool:
72+
"""Checks if the test and expected list of collection names match."""
73+
return Counter(test_collection_names) == Counter(expected_collection_name)
74+
75+
76+
def document_ids_are_correct(test_ids: list[str], expected_ids: list[str]) -> bool:
77+
"""Checks if the test (retrieved from the API) and expected list of (document) ids match."""
78+
return Counter(test_ids) == Counter(expected_ids)
79+
80+
12381
@pytest.mark.usefixtures("_test_server_fixture")
12482
def test_collections_negative():
12583
"""Checks that the non-existing collections cannot be found."""
@@ -159,6 +117,40 @@ def test_queries_platform_or_sensor(key: str, values: list[str]):
159117
)
160118

161119

120+
def make_query_string(keys: list[str], values_list: list[list[str] | datetime]) -> str:
121+
"""Makes a single query string for all the given queries."""
122+
query_buffer = []
123+
for key, value_list in zip(keys, values_list, strict=True):
124+
query_buffer += [f"{key}={value}" for value in value_list]
125+
return "&".join(query_buffer)
126+
127+
128+
def query_results_are_correct(keys: list[str], values_list: list[list[str] | datetime]) -> bool:
129+
"""Checks if the retrieved result from querying the database via the API matches the expected result.
130+
131+
There can be more than one query `key/value` pair.
132+
133+
Args:
134+
keys:
135+
A list of all query keys, e.g. ``keys=["platform", "sensor"]``
136+
137+
values_list:
138+
A list in which each element is a list of values itself. The `nth` element corresponds to the `nth` key in
139+
the ``keys``.
140+
141+
Returns:
142+
A boolean flag indicating whether the retrieved result matches the expected result.
143+
"""
144+
query_string = make_query_string(keys, values_list)
145+
146+
return (
147+
Counter(http_get(f"queries?{query_string}").json()) ==
148+
Counter(TestDatabase.match_query(
149+
**{label: value_list for label, value_list in zip(keys, values_list, strict=True)}
150+
))
151+
)
152+
153+
162154
@pytest.mark.usefixtures("_test_server_fixture")
163155
def test_queries_mix_platform_sensor():
164156
"""Tests a mix of platform and sensor queries."""
@@ -185,3 +177,11 @@ def test_queries_time():
185177
"time_max",
186178
time_max
187179
)
180+
181+
182+
def single_query_is_correct(key: str, value: str | datetime) -> bool:
183+
"""Checks if the given single query, denoted by ``key`` matches correctly against the ``value``."""
184+
return (
185+
Counter(http_get(f"queries?{key}={value}").json()) ==
186+
Counter(TestDatabase.match_query(**{key: value}))
187+
)

0 commit comments

Comments
 (0)