def test_inline_dict(encoder_cls): class TestDict(dict, InlineTableDict): pass t = copy.deepcopy(TEST_DICT) t['d'] = TestDict() t['d']['x'] = "abc" o: Dict[str, Any] = loads(dumps(t, encoder=encoder_cls)) assert o == loads(dumps(o, encoder=encoder_cls))
def test_dict_decoder(): class TestDict(dict): pass test_dict_decoder = TomlDecoder(TestDict) assert isinstance(loads(TEST_STR, decoder=test_dict_decoder), TestDict)
def test_decoder(): config = dedent("""\ [project] license = {file = "LICENSE"} """) data = loads(config)["project"] assert isinstance(data, dict) assert isinstance(data["license"], dict) assert isinstance(data["license"], InlineTableDict) data = loads(config, decoder=TomlPureDecoder)["project"] assert isinstance(data, dict) assert isinstance(data["license"], dict) assert not isinstance(data["license"], InlineTableDict)
def test_parse_valid_config( toml_config: str, tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, ): (tmp_pathplus / "pyproject.toml").write_clean(toml_config) config = PEP621Parser().parse( dom_toml.loads(toml_config, decoder=TomlPureDecoder)["project"]) advanced_data_regression.check(config)
def test_decimal(): PLACES = Decimal(10)**-4 d = {'a': Decimal("0.1")} o: Dict[str, Any] = loads(dumps(d)) assert o == loads(dumps(o)) assert Decimal(o['a']).quantize(PLACES) == d['a'].quantize(PLACES) with pytest.raises(TypeError): loads(2) # type: ignore[call-overload] with pytest.raises(TypeError, match="expected str, bytes or os.PathLike object, not int"): load(2) # type: ignore[call-overload] with pytest.raises(TypeError, match="expected str, bytes or os.PathLike object, not list"): load([]) # type: ignore[call-overload] with pytest.raises( TypeError, match="argument should be a str object or an os.PathLike object returning str, not <class 'bytes'>" ): load(b"test.toml") # type: ignore[call-overload]
def test_numpy_ints(): np = pytest.importorskip("numpy") encoder = TomlNumpyEncoder() d = {'a': np.array([1, 3], dtype=np.int64)} o: Dict[str, Any] = loads(dumps(d, encoder=encoder)) assert o == loads(dumps(o, encoder=encoder)) d = {'a': np.array([1, 3], dtype=np.int32)} o = loads(dumps(d, encoder=encoder)) assert o == loads(dumps(o, encoder=encoder)) d = {'a': np.array([1, 3], dtype=np.int16)} o = loads(dumps(d, encoder=encoder)) assert o == loads(dumps(o, encoder=encoder))
def test__dict(): class TestDict(dict): pass assert isinstance(loads(TEST_STR, dict_=TestDict), TestDict)
def test_bug_196(): d = datetime.datetime.now() bug_dict = {'x': d} round_trip_bug_dict: Dict[str, Any] = loads(dumps(bug_dict)) assert round_trip_bug_dict == bug_dict assert round_trip_bug_dict['x'] == bug_dict['x']
def test_deepcopy_timezone(): o: Dict[str, Any] = loads("dob = 1979-05-24T07:32:00-08:00") o2: Dict[str, Any] = copy.deepcopy(o) assert o2["dob"] == o["dob"] assert o2["dob"] is not o["dob"]
def test_commutativity(): o: Dict[str, Any] = loads(dumps(TEST_DICT)) assert o == loads(dumps(o))
def test_tuple(): d = {'a': (3, 4)} o: Dict[str, Any] = loads(dumps(d)) assert o == loads(dumps(o))
def test_ordered(encoder_cls, decoder_cls): o: Dict[str, Any] = loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls) assert o == loads(dumps(TEST_DICT, encoder=encoder_cls), decoder=decoder_cls)
def test_array_sep(encoder_cls): d = {'a': [1, 2, 3]} o: Dict[str, Any] = loads(dumps(d, encoder=encoder_cls)) assert o == loads(dumps(o, encoder=encoder_cls))
def test_parse_config_errors(config: str, expects: Type[Exception], match: str, tmp_pathplus: PathPlus): with pytest.raises(expects, match=match): PEP621Parser().parse(dom_toml.loads(config)["project"], set_defaults=True)
def test_loads_decoder_none(self): with pytest.warns(DeprecationWarning, match=self.match): assert dom_toml.loads( "hello = 'world'", decoder=None) == self.expected # type: ignore[call-overload]