1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/tests/utils_for_test.py
Kelly Brazil cf77ddc396 cleanup
2024-02-06 01:54:31 +00:00

53 lines
1.8 KiB
Python

"""jc - JSON test utils"""
import json
import os
from pathlib import Path
import jc
def _open_file(file_path, ext):
return open(Path(file_path).with_suffix(ext), 'r', encoding='utf-8')
def _get_base_dir(file_path):
return os.path.dirname(os.path.abspath(file_path))
def _get_parser_name_from_path(parser_path):
return Path(parser_path).stem[len('test_'):]
def _get_fixtures(base_dir, parser_name):
fixtures = {x.stem: str(x.with_suffix('')) for x in
(list(Path(base_dir).glob(f"**/{parser_name}--*.*")))}
return fixtures
def run_no_data(self, test_parser_path, expected):
"""Call this function to run a test for no input data for a parser"""
parser_name = _get_parser_name_from_path(test_parser_path)
with self.subTest(f"'no data test' for parser '{parser_name}': "):
self.assertEqual(jc.parse(parser_name, '', quiet=True), expected)
def run_all_fixtures(self, test_parser_path):
"""Call this function to run tests for all fixtures for a parser"""
parser_name = _get_parser_name_from_path(test_parser_path)
base_dir = _get_base_dir(test_parser_path)
print(f"\n'run all fixtures' for parser '{parser_name}':")
for file, file_path in _get_fixtures(base_dir, parser_name).items():
print(f"- test '{parser_name}' parser with fixture: '{file}'")
with self.subTest(f"fixture: '{file}'"):
with _open_file(file_path, '.out') as in_file, \
_open_file(file_path, '.json') as json_file:
f_in = in_file.read()
f_json = json.loads(json_file.read())
self.assertEqual(
jc.parse(parser_name, f_in, quiet=True),
f_json,
f"Should be equal for test files: '{file}.*'"
)