File: test_default.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 (48 lines) | stat: -rw-r--r-- 1,586 bytes parent folder | download | duplicates (2)
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
import pytest

from fastjsonschema import JsonSchemaValueException, validate


@pytest.mark.parametrize('value, expected', [
    (None, JsonSchemaValueException('data must be object', value='{data}', name='data', definition='{definition}', rule='type')),
    ({}, {'a': '', 'b': 42, 'c': {}, 'd': []}),
    ({'a': 'abc'}, {'a': 'abc', 'b': 42, 'c': {}, 'd': []}),
    ({'b': 123}, {'a': '', 'b': 123, 'c': {}, 'd': []}),
    ({'a': 'abc', 'b': 123}, {'a': 'abc', 'b': 123, 'c': {}, 'd': []}),
])
def test_default_in_object(asserter, value, expected):
    asserter({
        'type': 'object',
        'properties': {
            'a': {'type': 'string', 'default': ''},
            'b': {'type': 'number', 'default': 42},
            'c': {'type': 'object', 'default': {}},
            'd': {'type': 'array', 'default': []},
        },
    }, value, expected)


@pytest.mark.parametrize('value, expected', [
    (None, JsonSchemaValueException('data must be array', value='{data}', name='data', definition='{definition}', rule='type')),
    ([], ['', 42]),
    (['abc'], ['abc', 42]),
    (['abc', 123], ['abc', 123]),
])
def test_default_in_array(asserter, value, expected):
    asserter({
        'type': 'array',
        'items': [
            {'type': 'string', 'default': ''},
            {'type': 'number', 'default': 42},
        ],
    }, value, expected)


def test_default_turned_off():
    output = validate({
        'type': 'object',
        'properties': {
            'a': {'type': 'string', 'default': ''},
        },
    }, {}, use_default=False)
    assert output == {}