"""
Pytest configuration file.

"""
from pathlib import Path

import yaml


def pytest_generate_tests(metafunc):
    """
    Generate tests for testing JSON schema validity.

    """
    if metafunc.function.__name__ == 'test_valid':
        case_dir = 'valid'
    elif metafunc.function.__name__ == 'test_invalid':
        case_dir = 'invalid'
    else:
        return
    root_dir = Path(__file__).parent.resolve()
    schemas_paths = root_dir.joinpath('appsemble/schemas').glob('*.yaml')
    params = []
    ids = []
    for schema_path in schemas_paths:
        name = schema_path.with_suffix('').name
        with schema_path.open() as f:
            schema = yaml.load(f)
        test_dir = root_dir.joinpath('test').joinpath(case_dir).joinpath(name)
        test_cases = [*test_dir.glob('*.yaml')]
        assert test_cases, 'Missing {} test data for {}'.format(case_dir, name)
        for case in test_cases:
            with case.open(encoding='utf8') as data:
                params.append((schema, yaml.load(data)))
            ids.append((name + ': ' + case.with_suffix('').name))
    metafunc.parametrize('schema,data', params, ids=ids)
