Skip to content

Commit d3b875e

Browse files
committed
Refactor tests
1 parent abc4c5d commit d3b875e

6 files changed

Lines changed: 65 additions & 39 deletions

File tree

tests/__init__.py

Whitespace-only changes.

tests/helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import yaml
2+
3+
4+
def read_file(file_path):
5+
return yaml.safe_load(file_path.read_text(encoding='utf-8'))
6+
7+
8+
def read_instance(file_path):
9+
if file_path.is_dir():
10+
instance = []
11+
for group_path in file_path.iterdir():
12+
if group_path.suffix == '.yaml':
13+
instance += read_file(group_path)
14+
return instance
15+
elif file_path.suffix == '.yaml':
16+
return read_file(file_path)

tests/test_definitions.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
from pathlib import Path
22

33
import jsonschema
4-
import yaml
54

5+
from .helpers import read_file, read_instance
66

7-
def read_file(file_path):
8-
return yaml.safe_load(file_path.read_text(encoding='utf-8'))
9-
10-
11-
def read_instance(file_path):
12-
if file_path.is_dir():
13-
instance = []
14-
for group_path in file_path.iterdir():
15-
if group_path.suffix == '.yaml':
16-
instance += read_file(group_path)
17-
return instance
18-
elif file_path.suffix == '.yaml':
19-
return read_file(file_path)
7+
meta_path = Path(__file__).parent.joinpath('meta.yaml')
208

219

2210
def test_definitions():
23-
schema = read_file(Path(__file__).parent.joinpath('meta.yaml'))
11+
schema = read_file(meta_path)
2412

2513
for file_path in Path('definitions').iterdir():
2614
# read the instance

tests/test_pattern.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import json
2-
import os
31
import re
2+
from pathlib import Path
43

54
import pytest
65

6+
from .helpers import read_file
77

8-
def test_patterns():
9-
for root, _, files in os.walk('pattern'):
10-
for file_name in files:
11-
if file_name.endswith('.json'):
12-
file_path = os.path.join(root, file_name)
8+
pattern_keys = ['path', 'dataset', 'file']
9+
pattern_paths = list(Path('pattern').rglob('*.yaml'))
1310

14-
with open(file_path) as f:
15-
pattern_json = json.loads(f.read())
16-
pattern = os.linesep.join(pattern_json)
11+
@pytest.mark.parametrize('pattern_key', pattern_keys)
12+
@pytest.mark.parametrize('pattern_path', pattern_paths, ids=lambda x: str(x))
13+
def test_pattern(pattern_key, pattern_path):
14+
pattern = read_file(pattern_path).get(pattern_key)
15+
if pattern:
16+
pattern = re.sub(r'\s+', '', pattern)
1717

18-
try:
19-
re.compile(pattern)
20-
except re.error:
21-
pytest.fail(file_path)
18+
try:
19+
re.compile(pattern)
20+
except re.error as e:
21+
pytest.fail(f'Error with "{pattern_key}" in {pattern_path}: {e}')

tests/test_schema.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import json
2-
import os
1+
from pathlib import Path
32

3+
import pytest
44
from jsonschema import Draft7Validator
55

6+
from .helpers import read_file
67

7-
def test_patterns():
8-
for root, _, files in os.walk('schema'):
9-
for file_name in files:
10-
if file_name.endswith('.json'):
11-
file_path = os.path.join(root, file_name)
8+
schema_paths = sorted(Path('schema').rglob('*.yaml'))
129

13-
with open(file_path) as f:
14-
schema = json.loads(f.read())
10+
@pytest.mark.parametrize('schema_path', schema_paths, ids=lambda x: str(x))
11+
def test_schema(schema_path):
12+
schema = read_file(schema_path)
1513

16-
assert Draft7Validator.check_schema(schema) is None
14+
assert Draft7Validator.check_schema(schema) is None
15+
16+
assert schema['type'] == 'object'
17+
assert 'specifiers' in schema['required']
18+
assert 'specifiers' in schema['properties']
19+
20+
assert schema['properties']['specifiers']['type'] == 'object'
21+
assert 'required' in schema['properties']['specifiers']
22+
assert 'properties' in schema['properties']['specifiers']

tests/test_tree.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from .helpers import read_file
6+
7+
tree_paths = list(Path('tree').rglob('*.yaml'))
8+
9+
@pytest.mark.parametrize('tree_path', tree_paths, ids=lambda x: str(x))
10+
def test_tree(tree_path):
11+
tree = read_file(tree_path)
12+
13+
assert isinstance(tree, list)
14+
assert tree[0] == 'simulation_round'
15+
assert tree[1] == 'product'
16+
assert tree[2] in ['category', 'sector', 'publication']

0 commit comments

Comments
 (0)