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 `<schema>/<version>[/<event>]` 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.
This commit is contained in:
Athanasius 2022-08-29 17:08:03 +01:00
parent fa8952d0d5
commit 30bc05cf29
No known key found for this signature in database
GPG Key ID: 8C392035DD80FD62
2 changed files with 14 additions and 2 deletions

View File

@ -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."""

View File

@ -26,9 +26,9 @@ def test_fail_validation_no_softwarename(fix_sys_path, eddn_gateway, eddn_messag
assert res.startswith("FAIL: Schema Validation: [<ValidationError: \"'softwareName' is a required property\">]")
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"