File: conftest.py

package info (click to toggle)
python-fastjsonschema 2.21.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,780 kB
  • sloc: python: 3,343; makefile: 88; sh: 18
file content (44 lines) | stat: -rw-r--r-- 1,646 bytes parent folder | download
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
import os
import sys

current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(current_dir, os.pardir))


from pprint import pprint

import pytest

from fastjsonschema import JsonSchemaValueException, compile
from fastjsonschema.draft07 import CodeGeneratorDraft07


def pytest_configure(config):
    config.addinivalue_line("markers", "none")
    config.addinivalue_line("markers", "benchmark")


@pytest.fixture
def asserter():
    def f(definition, value, expected, formats={}, use_formats=True):
        # When test fails, it will show up code.
        code_generator = CodeGeneratorDraft07(definition, formats=formats, use_formats=use_formats)
        print(code_generator.func_code)
        pprint(code_generator.global_state)

        # By default old tests are written for draft-04.
        definition.setdefault('$schema', 'http://json-schema.org/draft-04/schema')

        validator = compile(definition, formats=formats, use_formats=use_formats)
        if isinstance(expected, JsonSchemaValueException):
            with pytest.raises(JsonSchemaValueException) as exc:
                validator(value)
            if expected.message is not any:
                assert exc.value.message == expected.message
            assert exc.value.value == (value if expected.value == '{data}' else expected.value)
            assert exc.value.name == expected.name
            assert exc.value.definition == (definition if expected.definition == '{definition}' else expected.definition)
            assert exc.value.rule == expected.rule
        else:
            assert validator(value) == expected
    return f