From 30bc05cf29b71ae554874cf23cea2717685fc110 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 29 Aug 2022 17:08:03 +0100 Subject: [PATCH] tests: Move to loading test messages from files * Implement a new fixture `eddn_message_from_file()` for testing this. Eventually this will get renamed to `eddn_message()`. * Files containing one message each go under `tests/eddn_message`, with sub-directories for `/[/]` and then the filename is the nature of that file in that context. Completely invalid files, i.e. not even valid JSON, can be placed in the top level of `tests/eddn_message`. * Use this for the `parse_and_error_handle()` "valid journal/1 scan" test. --- src/tests/conftest.py | 12 ++++++++++++ src/tests/gateway/test_parse_and_error_handle.py | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tests/conftest.py b/src/tests/conftest.py index b606acf..7f8d0e5 100644 --- a/src/tests/conftest.py +++ b/src/tests/conftest.py @@ -1,5 +1,6 @@ """General pytest configuration, including fixtures.""" import os +import pathlib import sys from typing import Callable, Optional @@ -89,6 +90,17 @@ test_messages = { } +@pytest.fixture() +def eddn_message_from_file() -> Callable: + """Load and supply a test message from the on-disk collection.""" + def _method(msg_type: str) -> Optional[str]: + path = pathlib.Path('tests/eddn_message/' + msg_type) + with open(path, 'r') as eddn_message: + return eddn_message.read() + + return _method + + @pytest.fixture def eddn_message() -> Callable: """Supply the requested test message.""" diff --git a/src/tests/gateway/test_parse_and_error_handle.py b/src/tests/gateway/test_parse_and_error_handle.py index 90c86ce..85d622b 100644 --- a/src/tests/gateway/test_parse_and_error_handle.py +++ b/src/tests/gateway/test_parse_and_error_handle.py @@ -26,9 +26,9 @@ def test_fail_validation_no_softwarename(fix_sys_path, eddn_gateway, eddn_messag assert res.startswith("FAIL: Schema Validation: []") -def test_valid_journal_scan(fix_sys_path, eddn_gateway, eddn_message: Callable) -> None: +def test_valid_journal_scan(fix_sys_path, eddn_gateway, eddn_message_from_file: Callable) -> None: """Test a valid journal/1, `event == 'Scan'` message.""" - msg = eddn_message('plain_journal_scan_valid') + msg = eddn_message_from_file('journal/1/scan/valid.json') res = eddn_gateway.parse_and_error_handle(msg.encode(encoding="utf-8")) assert res == "OK"