Skip to content

Commit

Permalink
Reorder functions in test scripts.
Browse files Browse the repository at this point in the history
The dependencies are now brought after the first main dependent function that uses them.
  • Loading branch information
pkhalaj committed May 31, 2024
1 parent 506f122 commit c42f8cc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
26 changes: 13 additions & 13 deletions trolldb/tests/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def config_file(tmp_path):
return create_config_file(tmp_path)


@pytest.mark.parametrize(("function", "args"), [
(record_messages_from_config, lf("config_file")),
(record_messages_from_command_line, [lf("config_file")])
])
async def test_record_from_cli_and_config(tmp_path, file_message, tmp_data_filename, function, args):
"""Tests that message recording adds a message to the database either via configs from a file or the CLI."""
msg = Message.decode(file_message)
with running_prepared_database_context():
with patched_subscriber_recv([file_message]):
await function(args)
assert await message_in_database_and_delete_count_is_one(msg)


async def message_in_database_and_delete_count_is_one(msg: Message) -> bool:
"""Checks if there is exactly one item in the database which matches the data of the message."""
async with mongodb_context(test_app_config.database):
Expand All @@ -76,19 +89,6 @@ async def message_in_database_and_delete_count_is_one(msg: Message) -> bool:
return result == msg.data and deletion_count == 1


@pytest.mark.parametrize(("function", "args"), [
(record_messages_from_config, lf("config_file")),
(record_messages_from_command_line, [lf("config_file")])
])
async def test_record_from_cli_and_config(tmp_path, file_message, tmp_data_filename, function, args):
"""Tests that message recording adds a message to the database either via configs from a file or the CLI."""
msg = Message.decode(file_message)
with running_prepared_database_context():
with patched_subscriber_recv([file_message]):
await function(args)
assert await message_in_database_and_delete_count_is_one(msg)


async def test_record_messages(config_file, tmp_path, file_message, tmp_data_filename):
"""Tests that message recording adds a message to the database."""
config = AppConfig(**make_test_app_config(tmp_path))
Expand Down
104 changes: 52 additions & 52 deletions trolldb/tests/tests_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,6 @@
main_collection_name = test_app_config.database.main_collection_name


def collections_exists(test_collection_names: list[str], expected_collection_name: list[str]) -> bool:
"""Checks if the test and expected list of collection names match."""
return Counter(test_collection_names) == Counter(expected_collection_name)


def document_ids_are_correct(test_ids: list[str], expected_ids: list[str]) -> bool:
"""Checks if the test (retrieved from the API) and expected list of (document) ids match."""
return Counter(test_ids) == Counter(expected_ids)


def single_query_is_correct(key: str, value: str | datetime) -> bool:
"""Checks if the given single query, denoted by ``key`` matches correctly against the ``value``."""
return (
Counter(http_get(f"queries?{key}={value}").json()) ==
Counter(TestDatabase.match_query(**{key: value}))
)


def make_query_string(keys: list[str], values_list: list[list[str] | datetime]) -> str:
"""Makes a single query string for all the given queries."""
query_buffer = []
for key, value_list in zip(keys, values_list, strict=True):
query_buffer += [f"{key}={value}" for value in value_list]
return "&".join(query_buffer)


def query_results_are_correct(keys: list[str], values_list: list[list[str] | datetime]) -> bool:
"""Checks if the retrieved result from querying the database via the API matches the expected result.
There can be more than one query `key/value` pair.
Args:
keys:
A list of all query keys, e.g. ``keys=["platform", "sensor"]``
values_list:
A list in which each element is a list of values itself. The `nth` element corresponds to the `nth` key in
the ``keys``.
Returns:
A boolean flag indicating whether the retrieved result matches the expected result.
"""
query_string = make_query_string(keys, values_list)

return (
Counter(http_get(f"queries?{query_string}").json()) ==
Counter(TestDatabase.match_query(
**{label: value_list for label, value_list in zip(keys, values_list, strict=True)}
))
)


@pytest.mark.usefixtures("_test_server_fixture")
def test_root():
"""Checks that the server is up and running, i.e. the root routes responds with 200."""
Expand Down Expand Up @@ -120,6 +68,16 @@ def test_collections():
)


def collections_exists(test_collection_names: list[str], expected_collection_name: list[str]) -> bool:
"""Checks if the test and expected list of collection names match."""
return Counter(test_collection_names) == Counter(expected_collection_name)


def document_ids_are_correct(test_ids: list[str], expected_ids: list[str]) -> bool:
"""Checks if the test (retrieved from the API) and expected list of (document) ids match."""
return Counter(test_ids) == Counter(expected_ids)


@pytest.mark.usefixtures("_test_server_fixture")
def test_collections_negative():
"""Checks that the non-existing collections cannot be found."""
Expand Down Expand Up @@ -159,6 +117,40 @@ def test_queries_platform_or_sensor(key: str, values: list[str]):
)


def make_query_string(keys: list[str], values_list: list[list[str] | datetime]) -> str:
"""Makes a single query string for all the given queries."""
query_buffer = []
for key, value_list in zip(keys, values_list, strict=True):
query_buffer += [f"{key}={value}" for value in value_list]
return "&".join(query_buffer)


def query_results_are_correct(keys: list[str], values_list: list[list[str] | datetime]) -> bool:
"""Checks if the retrieved result from querying the database via the API matches the expected result.
There can be more than one query `key/value` pair.
Args:
keys:
A list of all query keys, e.g. ``keys=["platform", "sensor"]``
values_list:
A list in which each element is a list of values itself. The `nth` element corresponds to the `nth` key in
the ``keys``.
Returns:
A boolean flag indicating whether the retrieved result matches the expected result.
"""
query_string = make_query_string(keys, values_list)

return (
Counter(http_get(f"queries?{query_string}").json()) ==
Counter(TestDatabase.match_query(
**{label: value_list for label, value_list in zip(keys, values_list, strict=True)}
))
)


@pytest.mark.usefixtures("_test_server_fixture")
def test_queries_mix_platform_sensor():
"""Tests a mix of platform and sensor queries."""
Expand All @@ -185,3 +177,11 @@ def test_queries_time():
"time_max",
time_max
)


def single_query_is_correct(key: str, value: str | datetime) -> bool:
"""Checks if the given single query, denoted by ``key`` matches correctly against the ``value``."""
return (
Counter(http_get(f"queries?{key}={value}").json()) ==
Counter(TestDatabase.match_query(**{key: value}))
)

0 comments on commit c42f8cc

Please sign in to comment.