From eef1b027ab70704bcaa60a089e4ae1592c504b86 Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Thu, 9 Jan 2020 19:11:46 +0530 Subject: [PATCH] Add test cases for dataclasses. (#17909) * Add test cases for dataclasses. * Add test for repr output of field. * Add test for ValueError to be raised when both default and default_factory are passed. --- Lib/test/test_dataclasses.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 8f9fb2ce8c1..e8fe455fc19 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -45,6 +45,25 @@ class TestCase(unittest.TestCase): o = C(42) self.assertEqual(o.x, 42) + def test_field_default_default_factory_error(self): + msg = "cannot specify both default and default_factory" + with self.assertRaisesRegex(ValueError, msg): + @dataclass + class C: + x: int = field(default=1, default_factory=int) + + def test_field_repr(self): + int_field = field(default=1, init=True, repr=False) + int_field.name = "id" + repr_output = repr(int_field) + expected_output = "Field(name='id',type=None," \ + f"default=1,default_factory={MISSING!r}," \ + "init=True,repr=False,hash=None," \ + "compare=True,metadata=mappingproxy({})," \ + "_field_type=None)" + + self.assertEqual(repr_output, expected_output) + def test_named_init_params(self): @dataclass class C: