File: test_monkeypatch.py

package info (click to toggle)
python-frozendict 2.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,704 kB
  • sloc: ansic: 33,974; python: 2,314; sh: 11; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 989 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
import json

import frozendict as cool
import pytest
from frozendict import frozendict
from frozendict.monkeypatch import MonkeypatchWarning


class A:
    pass


@pytest.fixture
def object_to_serialize():
    return frozendict()


@pytest.fixture
def object_to_serialize_2():
    return A()


@pytest.fixture
def serialized_object():
    return "{}"


def test_get_json_encoder(
    object_to_serialize,
    object_to_serialize_2,
    serialized_object,
):
    if cool.c_ext:
        cool.monkeypatch.patchOrUnpatchJson(patch=True)
    else:
        with pytest.warns(MonkeypatchWarning):
            cool.monkeypatch.patchOrUnpatchJson(patch=True, warn=True)
    
    assert json.dumps(object_to_serialize) == serialized_object
    
    with pytest.raises(TypeError):
        json.dumps(object_to_serialize_2)
    
    cool.monkeypatch.patchOrUnpatchJson(patch=False, warn=False)
    
    if cool.c_ext:
        with pytest.raises(TypeError):
            json.dumps(object_to_serialize)