File: test_implementations.py

package info (click to toggle)
python-anyjson 0.3.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 184 kB
  • sloc: python: 330; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,654 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
import pytest
import anyjson

modnames = [e[0] for e in anyjson._modules]

def test_default_serialization():
    assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
    assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]"


def test_default_deserialization():
    assert anyjson.loads("[1,2,3]") == [1,2,3]
    assert anyjson.deserialize("[1,2,3]") == [1,2,3]


def test_forced_serialization():
    for name in modnames:
        try:
            anyjson.force_implementation(name)
        except ImportError:
            continue # module can't be tested, try next

        assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
        assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]"


def test_forced_deserialization():
    for name in modnames:
        try:
            anyjson.force_implementation(name)
        except ImportError:
            continue # module can't be tested, try next

        assert anyjson.loads("[1,2,3]") == [1,2,3]
        assert anyjson.deserialize("[1,2,3]") == [1,2,3]


def test_exceptions():
    for name in modnames:
        try:
            anyjson.force_implementation(name)
        except ImportError:
            continue # module can't be tested, try next

        pytest.raises(TypeError, anyjson.dumps, [object()])
        pytest.raises(TypeError, anyjson.serialize, [object()])
        pytest.raises(ValueError, anyjson.loads, "[")
        pytest.raises(ValueError, anyjson.deserialize, "[")


def test_json_loads_unicode():
    try:
        anyjson.force_implementation("json")
    except ImportError:
        return

    assert "foo" in anyjson.loads('{"foo": "bar"}')