mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
test utils cleanup
This commit is contained in:
@ -3,21 +3,21 @@ import unittest
|
||||
from tests import utils_for_test as test_utils
|
||||
|
||||
# Execute these steps for standard tests:
|
||||
# - Save this file as `text_{parser_name}.py` since the helper methods extract parser names from the filename.
|
||||
# - Organize fixtures in `text/fixtures` for optimal structure.
|
||||
# - Save this file as `test_{parser_name}.py` since the helper methods extract parser names from the filename.
|
||||
# - Organize fixtures in `test/fixtures` for optimal structure.
|
||||
# - Format fixtures as follows (using double dashes):
|
||||
# - `{parser_name}--{some_test_description}.out` for command output.
|
||||
# - `{parser_name}--{some_test_description}.json` for expected JSON after parsing.
|
||||
|
||||
class MyTests(unittest.TestCase):
|
||||
|
||||
def test_path_nodata(self):
|
||||
def test_foo_nodata(self):
|
||||
"""
|
||||
Test 'my_parser_name' with no data
|
||||
"""
|
||||
test_utils.run_no_data(self, __file__, {})
|
||||
|
||||
def test_all_fixtures(self):
|
||||
def test_foo_all_fixtures(self):
|
||||
"""
|
||||
Test 'my_parser_name' with various fixtures
|
||||
"""
|
||||
|
@ -11,7 +11,7 @@ class MyTests(unittest.TestCase):
|
||||
"""
|
||||
test_utils.run_no_data(self, __file__, {})
|
||||
|
||||
def test_all_fixtures(self):
|
||||
def test_path_all_fixtures(self):
|
||||
"""
|
||||
Test 'path' with various logs
|
||||
"""
|
||||
|
@ -5,18 +5,16 @@ from tests import utils_for_test as test_utils
|
||||
|
||||
class MyTests(unittest.TestCase):
|
||||
|
||||
def test_path_nodata(self):
|
||||
def test_path_list_nodata(self):
|
||||
"""
|
||||
Test 'path_list' with no data
|
||||
"""
|
||||
# self.assertEqual(parse('', quiet=True), [])
|
||||
test_utils.run_no_data(self, __file__, [])
|
||||
|
||||
def test_all_fixtures(self):
|
||||
def test_path_list_all_fixtures(self):
|
||||
"""
|
||||
Test 'path_list' with various logs
|
||||
"""
|
||||
|
||||
test_utils.run_all_fixtures(self, __file__)
|
||||
|
||||
|
||||
|
@ -1,60 +1,48 @@
|
||||
"""jc - JSON test utils"""
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import jc
|
||||
|
||||
|
||||
def open_file(file_path, ext):
|
||||
def _open_file(file_path, ext):
|
||||
return open(Path(file_path).with_suffix(ext), 'r', encoding='utf-8')
|
||||
|
||||
|
||||
def get_base_dir(file_path):
|
||||
def _get_base_dir(file_path):
|
||||
THIS_DIR = os.path.dirname(os.path.abspath(file_path))
|
||||
return THIS_DIR
|
||||
|
||||
|
||||
def get_parser_name():
|
||||
# Get the calling file name from the stack
|
||||
stack = inspect.stack()
|
||||
calling_frame = stack[1]
|
||||
calling_file_path = calling_frame[1]
|
||||
|
||||
return get_parser_name_from_path(calling_file_path)
|
||||
|
||||
|
||||
def get_parser_name_from_path(parser_path):
|
||||
def _get_parser_name_from_path(parser_path):
|
||||
return Path(parser_path).stem[len('test_'):]
|
||||
|
||||
|
||||
def get_fixtures(base_dir, parser_name):
|
||||
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):
|
||||
parser_name = get_parser_name_from_path(test_parser_path)
|
||||
|
||||
# expected = jc.get_parser(parser_name).info.default_no_data
|
||||
"""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):
|
||||
parser_name = get_parser_name_from_path(test_parser_path)
|
||||
base_dir = get_base_dir(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()
|
||||
print(f"'run all fixtures' for parser '{parser_name}':")
|
||||
for file, file_path in get_fixtures(base_dir, parser_name).items():
|
||||
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:
|
||||
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())
|
||||
|
||||
|
Reference in New Issue
Block a user