Пример #1
0
import datetime
import re

import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException

exc = JsonSchemaValidationException('must be date-time',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='format')


@pytest.mark.parametrize('value, expected', [
    ('', exc),
    ('bla', exc),
    ('2018-02-05T14:17:10.00', exc),
    ('2018-02-05T14:17:10.00Z\n', exc),
    ('2018-02-05T14:17:10.00Z', '2018-02-05T14:17:10.00Z'),
    ('2018-02-05T14:17:10Z', '2018-02-05T14:17:10Z'),
])
def test_datetime(asserter, value, expected):
    asserter({'type': 'string', 'format': 'date-time'}, value, expected)


exc = JsonSchemaValidationException('must be hostname',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='format')
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException

exc = JsonSchemaValidationException(
    'must be one of [1, 2, \'a\', "b\'c"] but is: 12',
    value='{data}',
    _rendered_path='data',
    definition='{definition}',
    rule='enum')
exc2 = JsonSchemaValidationException(
    'must be one of [1, 2, \'a\', "b\'c"] but is: aa',
    value='{data}',
    _rendered_path='data',
    definition='{definition}',
    rule='enum')


@pytest.mark.parametrize('value, expected', [
    (1, 1),
    (2, 2),
    (12, exc),
    ('a', 'a'),
    ('aa', exc2),
])
def test_enum(asserter, value, expected):
    asserter({'enum': [1, 2, 'a', "b'c"]}, value, expected)


exc = JsonSchemaValidationException(
    'must be string or number, but is a: {value_type}',
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException

exc = JsonSchemaValidationException('must be boolean, but is a: {value_type}',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, True),
    (False, False),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_boolean(asserter, value, expected):
    asserter({'type': 'boolean'},
             value,
             expected,
             value_type=type(value).__name__)
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException

exc = JsonSchemaValidationException('must be array, but is a: {value_type}',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], []),
    ([1, 'a', True], [1, 'a', True]),
    ((1, 'a', True), (1, 'a', True)),
    (range(5), range(5)),
    ({}, exc),
])
def test_array(asserter, value, expected):
    asserter({'type': 'array'},
             value,
             expected,
             value_type=type(value).__name__)


exc = JsonSchemaValidationException(
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException


@pytest.mark.parametrize('value, expected', [
    (None,
     JsonSchemaValidationException('must be object, but is a: NoneType',
                                   value='{data}',
                                   _rendered_path='data',
                                   definition='{definition}',
                                   rule='type')),
    ({}, {
        'a': '',
        'b': 42,
        'c': {},
        'd': []
    }),
    ({
        'a': 'abc'
    }, {
        'a': 'abc',
        'b': 42,
        'c': {},
        'd': []
    }),
    ({
        'b': 123
    }, {
        'a': '',
        'b': 123,
Пример #6
0
@pytest.mark.parametrize('value, expected', [
    ({
        'namedTypeArray_string': ['str', 'str'],
        'namedTypeArray<string[]>': [1, 2]
    }, {
        'namedTypeArray_string': ['str', 'str'],
        'namedTypeArray<string[]>': [1, 2]
    }),
    ({
        'namedTypeArray_string': ['str', 'str'],
        'namedTypeArray<string[]>': ['str', 'str']
    },
     JsonSchemaValidationException(
         'must be number, but is a: str',
         value=None,
         _rendered_path='data.namedTypeArray<string[]>[0]',
         definition=None,
         rule='type')),
    ({
        'namedTypeArray_string': [1, 2],
        'namedTypeArray<string[]>': [1, 2]
    },
     JsonSchemaValidationException(
         'must be string, but is a: int',
         value=None,
         _rendered_path='data.namedTypeArray_string[0]',
         definition=None,
         rule='type')),
])
def test_unique_name_generator(asserter, value, expected):
    asserter(validationTestTypesSchema,
Пример #7
0
def test_exception_rule_definition(definition, rule, expected_rule_definition):
    exc = JsonSchemaValidationException('msg', None, definition=definition, rule=rule)
    assert exc.rule_definition == expected_rule_definition
import pytest

import precisionlife_fastjsonschema as fastjsonschema
from precisionlife_fastjsonschema import JsonSchemaDefinitionException, JsonSchemaValidationException

exc = JsonSchemaValidationException('must be object, but is a: {value_type}',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    (False, exc),
    ('abc', exc),
    ([], exc),
    ({}, {}),
    ({
        'x': 1,
        'y': True
    }, {
        'x': 1,
        'y': True
    }),
])
def test_object(asserter, value, expected):
    asserter({'type': 'object'},
             value,
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 42, 3],
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
 ),
 (
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5, 'any'],
     [9, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5, 'any'],
 ),
 (
     [10, 'world', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaValidationException('must be smaller than 10', value=10, _rendered_path='data[0]', definition=definition['items'][0], rule='maximum'),
 ),
 (
     [9, 'xxx', [1], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaValidationException('must be one of [\'hello\', \'world\'] but is: xxx', value='xxx', _rendered_path='data[1]', definition=definition['items'][1], rule='enum'),
 ),
 (
     [9, 'hello', [], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaValidationException('must contain at least 1 items', value=[], _rendered_path='data[2]', definition=definition['items'][2], rule='minItems'),
 ),
 (
     [9, 'hello', [1, 2, 3], {'a': 'a', 'b': 'b', 'c': 'xy'}, 'str', 5],
     JsonSchemaValidationException('must be string, but is a: int', value=2, _rendered_path='data[2][1]', definition={'type': 'string'}, rule='type'),
 ),
 (
     [9, 'hello', [1], {'a': 'a', 'x': 'x', 'y': 'y'}, 'str', 5],
Пример #10
0
def test_integer_is_not_number(asserter, value):
    asserter({
        'type': 'integer',
    }, value, JsonSchemaValidationException('must be integer, but is a: float', value='{data}', _rendered_path='data', definition='{definition}', rule='type'))
Пример #11
0
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException


@pytest.fixture(params=['number', 'integer'])
def number_type(request):
    return request.param


exc = JsonSchemaValidationException('must be {number_type}, but is a: {value_type}', value='{data}', _rendered_path='data', definition='{definition}', rule='type')
@pytest.mark.parametrize('value, expected', [
    (-5, -5),
    (0, 0),
    (5, 5),
    (None, exc),
    (True, exc),
    ('abc', exc),
    ([], exc),
    ({}, exc),
])
def test_number(asserter, number_type, value, expected):
    asserter({'type': number_type}, value, expected, number_type=number_type, value_type=type(value).__name__)


exc = JsonSchemaValidationException('must be smaller than or equal to 10', value='{data}', _rendered_path='data', definition='{definition}', rule='maximum')
@pytest.mark.parametrize('value, expected', [
    (-5, -5),
    (5, 5),
    (9, 9),
    (10, 10),
import pytest

from precisionlife_fastjsonschema import JsonSchemaValidationException

exc = JsonSchemaValidationException('must be string, but is a: {value_type}',
                                    value='{data}',
                                    _rendered_path='data',
                                    definition='{definition}',
                                    rule='type')


@pytest.mark.parametrize('value, expected', [
    (0, exc),
    (None, exc),
    (True, exc),
    ('', ''),
    ('abc', 'abc'),
    ([], exc),
    ({}, exc),
])
def test_string(asserter, value, expected):
    asserter({'type': 'string'},
             value,
             expected,
             value_type=type(value).__name__)


exc = JsonSchemaValidationException(
    'must be shorter than or equal to 5 characters',
    value='{data}',
    _rendered_path='data',