def test_save_as_dict(): c = Config.from_str(TEST_CONFIG) d = Config(c.to_dict()) assert c == d e = Config.from_str(TEST_CONFIG, interpolate=False) f = Config(e.to_dict()) assert e == f
def test_dict_merging(): c = Config({"a": {"b": 1, "c": 42}}) c.update({"a": {"b": 123}}) assert c == {"a": {"b": 123, "c": 42}} c = Config.from_str(""" [a] x = 42 [b.c] y = "asdf" """) c.update({"a": {"z": 1}}, merge_schema=True) c.update({"b": {"d": 2}}, merge_schema=True) assert c == {"a": {"x": 42, "z": 1}, "b": {"c": {"y": "asdf"}, "d": 2}}
def test_load_merging(): c = Config() c.load_str(""" [a] b = 1 [c] d = "foo" """) with pytest.raises(ValidationError): c.load_str(""" [a] ab = "yeah" [aa] cd = "abba" """) assert c == {"a": {"b": 1}, "c": {"d": "foo"}} c.load_str( """ [a] ab = "yeah" [aa] cd = "abba" """, merge_schema=True, ) assert c == { "a": { "b": 1, "ab": "yeah" }, "c": { "d": "foo" }, "aa": { "cd": "abba" } }
def test_illegal_types(): with pytest.raises(TypeError): Config({"x": {None: 2}}) with pytest.raises(TypeError): Config({"x": b"ugh"}) with pytest.raises(TypeError): Config({"x": dt.datetime.utcnow()}) with pytest.raises(TypeError): Config({"x": dt.timedelta(days=1)}) with pytest.raises(TypeError): Config({"x": Decimal("3.3")}) with pytest.raises(TypeError): Config({"x": complex(1, 2)})
def test_2d_numpy_arrays(dtype): value = np.ones((5, 5), dtype=dtype) c = Config({"x": value}) assert isinstance(c.x, type(value.tolist())) and c.x == value.tolist() assert_valid_json(c)
def test_numpy_scalars(dtype): value = np.ones(1, dtype=dtype)[0] c = Config({"x": value}) assert isinstance(c.x, type(value.item())) and c.x == value.item() assert_valid_json(c)
def test_json_types(value): c = Config({"x": value}) assert isinstance(c.x, type(value)) assert c.x == value or (isinstance(c.x, float) and isnan(c.x) and isinstance(value, float) and isnan(value)) assert_valid_json(c)