๐Ÿ“ฆ encode / apistar

๐Ÿ“„ test_json_schema.py ยท 71 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71import json
import os

import pytest

from apistar.codecs import JSONSchemaCodec

filenames = [
    'additionalItems.json',
    'additionalProperties.json',
    # 'allOf.json',
    # 'anyOf.json',
    'default.json',
    # 'definitions.json',
    # 'dependencies.json',
    # 'enum.json',
    'items.json',
    'maxItems.json',
    'maxLength.json',
    'maxProperties.json',
    'maximum.json',
    'minItems.json',
    'minLength.json',
    'minProperties.json',
    'minimum.json',
    'multipleOf.json',
    # 'not.json',
    # 'oneOf.json',
    'pattern.json',
    'patternProperties.json',
    'properties.json',
    # 'ref.json',
    # 'refRemote.json',
    'required.json',
    'type.json',
    'uniqueItems.json'
]


def load_test_cases():
    loaded = []

    for filename in filenames:
        path = os.path.join('tests', 'json_schema', filename)
        content = open(path, 'rb').read()
        test_suite = json.loads(content.decode('utf-8'))
        for test_cases in test_suite:
            description = test_cases['description']
            schema = test_cases['schema']
            for test in test_cases['tests']:
                test_description = test['description']
                test_data = test['data']
                test_valid = test['valid']
                full_description = '%s : %s - %s' % (
                    filename, description, test_description
                )
                case = (schema, test_data, test_valid, full_description)
                loaded.append(case)

    return loaded


test_cases = load_test_cases()


@pytest.mark.parametrize("schema,value,is_valid,description", test_cases)
def test_json_schema(schema, value, is_valid, description):
    validator = JSONSchemaCodec().decode_from_data_structure(schema)
    was_valid = validator.is_valid(value)
    assert was_valid == is_valid, description