Passed
Push — main ( 87238c...9f1476 )
by Douglas
02:33
created

tests.pocketutils.tools.test_json_utils   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 49
dl 0
loc 93
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A TestJsonUtils.test_preserve_inf() 0 5 1
A TestJsonUtils.test_to_json() 0 47 1
A TestJsonUtils.test_new_default() 0 21 2
1
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to typed-dfs
2
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/typed-dfs
3
# SPDX-License-Identifier: Apache-2.0
4
import inspect
5
6
import numpy as np
7
import pytest
8
from typeddfs.utils.json_utils import JsonUtils
9
10
11
class TestJsonUtils:
12
    def test_preserve_inf(self):
13
        matrix = np.zeros((2, 2))
14
        assert (JsonUtils.preserve_inf(matrix) == matrix.astype(str)).all()
15
        matrix = np.asarray([[2, float("inf")], [float("inf"), 2]])
16
        assert (JsonUtils.preserve_inf(matrix) == matrix.astype(str)).all()
17
        # TODO: nested tests
18
19
    def test_new_default(self):
20
        class X:
21
            def __str__(self):
22
                return "from-str"
23
24
            def __repr__(self):
25
                return "from-repr"
26
27
        default = JsonUtils.new_default()
28
        x = default(None)
29
        assert x is None
30
        assert default(X()) == "from-str"
31
        default = JsonUtils.new_default(last=repr)
32
        assert default(X()) == "from-repr"
33
34
        def fixer(obj):
35
            if isinstance(obj, X):
36
                return "gotcha!"
37
38
        default = JsonUtils.new_default(fixer)
39
        assert default(X()) == "gotcha!"
40
41
    def test_to_json(self):
42
        assert JsonUtils.encoder().as_str("hi") == '"hi"\n'
43
        assert JsonUtils.encoder().as_str(["hi", "bye"]) == '[\n  "hi",\n  "bye"\n]\n'
44
        data = {
45
            "list": [
46
                {
47
                    "numbers": {
48
                        1: np.asarray([float("inf"), 0]),
49
                        2: np.asarray([1, 1]),
50
                        3: np.half(float("inf")),
51
                        4: np.half(float("-inf")),
52
                        5: float("inf"),
53
                        6: float("-inf"),
54
                        7: 1,
55
                    },
56
                },
57
            ],
58
        }
59
        x = JsonUtils.encoder().as_str(data)
60
        assert (
61
            x
62
            == inspect.cleandoc(
63
                """
64
            {
65
              "list": [
66
                {
67
                  "numbers": {
68
                    "1": [
69
                      "inf",
70
                      "0.0"
71
                    ],
72
                    "2": [
73
                      "1",
74
                      "1"
75
                    ],
76
                    "3": "inf",
77
                    "4": "-inf",
78
                    "5": "inf",
79
                    "6": "-inf",
80
                    "7": 1
81
                  }
82
                }
83
              ]
84
            }
85
            """,
86
            )
87
            + "\n"
88
        )
89
90
91
if __name__ == "__main__":
92
    pytest.main()
93