/ tests / utils / test_numpy_encoder.py
test_numpy_encoder.py
 1  import datetime
 2  import json
 3  
 4  import numpy as np
 5  import pandas as pd
 6  import pytest
 7  
 8  from evidently.legacy.utils import NumpyEncoder
 9  from evidently.legacy.utils.types import ApproxValue
10  
11  
12  @pytest.mark.parametrize(
13      "value,expected",
14      [
15          *[
16              (t(0), "0")
17              for t in (
18                  np.int_,
19                  np.intc,
20                  np.intp,
21                  np.int8,
22                  np.int16,
23                  np.int32,
24                  np.int64,
25                  np.uint8,
26                  np.uint16,
27                  np.uint32,
28                  np.uint64,
29              )
30          ],
31          *[(t(1.0), "1.0") for t in (np.double, np.float16, np.float32, np.float64)],
32          (np.array([1, 2]), "[1, 2]"),
33          (np.bool_(False), "false"),
34          (pd.Timedelta(1), '"0 days 00:00:00.000000001"'),
35          (np.void(0), "null"),
36          (pd.NaT, "null"),
37          (pd.Timestamp(year=2000, month=1, day=1), '"2000-01-01T00:00:00"'),
38          (datetime.datetime(2000, 1, 1), '"2000-01-01T00:00:00"'),
39          (datetime.date(2000, 1, 1), '"2000-01-01"'),
40          (ApproxValue(1), '{"value": 1, "relative": 1e-06, "absolute": 1e-12}'),
41          (pd.Series([0]), "[0]"),
42      ],
43  )
44  def test_encoder(value, expected):
45      assert json.dumps({"value": value}, cls=NumpyEncoder) == f'{{"value": {expected}}}'