File: test_models.py

package info (click to toggle)
dataclass-wizard 0.39.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,112 kB
  • sloc: python: 19,560; makefile: 126; javascript: 23
file content (68 lines) | stat: -rw-r--r-- 1,851 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
from pytest_mock import MockerFixture

from dataclass_wizard import fromlist
from dataclass_wizard.models import Container, json_field
from .conftest import SampleClass


@pytest.fixture
def mock_open(mocker: MockerFixture):
    return mocker.patch('dataclass_wizard.models.open')


def test_json_field_does_not_allow_both_default_and_default_factory():
    """
    Confirm we can't specify both `default` and `default_factory` when
    calling the :func:`json_field` helper function.
    """
    with pytest.raises(ValueError):
        _ = json_field((), default=None, default_factory=None)


def test_container_with_incorrect_usage():
    """Confirm an error is raised when wrongly instantiating a Container."""
    c = Container()

    with pytest.raises(TypeError) as exc_info:
        _ = c.to_json()

    err_msg = exc_info.exconly()
    assert 'A Container object needs to be instantiated ' \
           'with a generic type T' in err_msg


def test_container_methods(mocker: MockerFixture, mock_open):
    list_of_dict = [{'f1': 'hello', 'f2': 1},
                    {'f1': 'world', 'f2': 2}]

    list_of_a = fromlist(SampleClass, list_of_dict)

    c = Container[SampleClass](list_of_a)

    # The repr() is very short, so it would be expected to fit in one line,
    # which thus aligns with the output of `pprint.pformat`.
    assert str(c) == repr(c)

    assert c.prettify() == """\
[
  {
    "f1": "hello",
    "f2": 1
  },
  {
    "f1": "world",
    "f2": 2
  }
]"""

    assert c.to_json() == '[{"f1": "hello", "f2": 1}, {"f1": "world", "f2": 2}]'

    mock_open.assert_not_called()
    mock_encoder = mocker.Mock()

    filename = 'my_file.json'
    c.to_json_file(filename, encoder=mock_encoder)

    mock_open.assert_called_once_with(filename, 'w')
    mock_encoder.assert_called_once_with(list_of_dict, mocker.ANY)