EDDN/src/tests/conftest.py
Athanasius 30bc05cf29
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.
2022-08-29 17:08:03 +01:00

142 lines
4.2 KiB
Python

"""General pytest configuration, including fixtures."""
import os
import pathlib
import sys
from typing import Callable, Optional
import pytest
"""A dictionary of test messages, all in string form."""
test_messages = {
######################################################################
# Examples of invalid messages.
######################################################################
'invalid_json': '{not real json',
'plain_outdated_schema': '''{
"$schemaRef": "http://schemas.elite-markets.net/eddn/journal/1",
"header": {
"uploaderID": "outdated schema",
"softwareName": "pytest:src/",
"softwareVersion": "v0.0.1"
},
"message": {
}
}''',
'plain_no_softwarename': '''{
"$schemaRef": "https://eddn.edcd.io/schemas/journal/1",
"header": {
"uploaderID": "no softwareName",
"softwareVersion": "v0.0.1"
},
"message": {
}
}''',
######################################################################
######################################################################
# journal/1 'scan' messages
######################################################################
# Example journal/1 'scan' valid message.
'plain_journal_scan_valid': '''{
"$schemaRef": "https://eddn.edcd.io/schemas/journal/1",
"header": {
"uploaderID": "valid journal message",
"softwareName": "pytest:src/",
"softwareVersion": "v0.0.1"
},
"message": {
"timestamp":"2021-11-05T15:46:28Z",
"event":"Scan",
"StarSystem":"Elphin",
"StarPos":[-30.12500,8.18750,-17.00000],
"SystemAddress":3932076118738
}
}''',
######################################################################
######################################################################
# commodity/3 messages
######################################################################
# Example commodity/3 valid message.
'plain_commodity_valid': '''{
"$schemaRef": "https://eddn.edcd.io/schemas/commodity/3",
"header": {
"uploaderID": "valid journal message",
"softwareName": "pytest:src/",
"softwareVersion": "v0.0.1"
},
"message": {
"timestamp":"2021-11-05T15:46:28Z",
"systemName":"LP 98-132",
"stationName":"Freeport",
"marketId":128008448,
"commodities":[
{
"name":"$platinum_name;",
"buyPrice":39557,
"sellPrice":39555,
"meanPrice":58263,
"stockBracket":0,
"demandBracket":0,
"stock":0,
"demand":0
}
]
}
}''',
######################################################################
}
@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."""
def _method(msg_type: str) -> Optional[str]:
return test_messages.get(msg_type)
return _method
@pytest.fixture
def fix_sys_path() -> None:
"""Set up an eddn.Gateway import."""
# Tests don't include the directory that `pytest` is run from on sys.path
sys.path.append(os.getcwd())
@pytest.fixture(scope="session")
def eddn_gateway():
"""Set up an eddn.Gateway import."""
import eddn.Gateway
class CLArgs:
config = False
cl_args = CLArgs()
eddn.Gateway.load_config(cl_args)
eddn.Gateway.configure()
return eddn.Gateway
@pytest.fixture
def bottle_response() -> object:
"""Mock a `bottle.response` enough for tests."""
class BottleResponseMock:
status: int = 200
return BottleResponseMock()