MCPcopy Index your code
hub / github.com/python/cpython / test_subclass_alternate_constructors

Method test_subclass_alternate_constructors

Lib/test/datetimetester.py:1921–1963  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1919 self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month - 7)
1920
1921 def test_subclass_alternate_constructors(self):
1922 # Test that alternate constructors call the constructor
1923 class DateSubclass(self.theclass):
1924 def __new__(cls, *args, **kwargs):
1925 result = self.theclass.__new__(cls, *args, **kwargs)
1926 result.extra = 7
1927
1928 return result
1929
1930 args = (2003, 4, 14)
1931 d_ord = 731319 # Equivalent ordinal date
1932 d_isoformat = '2003-04-14' # Equivalent isoformat()
1933
1934 base_d = DateSubclass(*args)
1935 self.assertIsInstance(base_d, DateSubclass)
1936 self.assertEqual(base_d.extra, 7)
1937
1938 # Timestamp depends on time zone, so we'll calculate the equivalent here
1939 ts = datetime.combine(base_d, time(0)).timestamp()
1940
1941 test_cases = [
1942 ('fromordinal', (d_ord,)),
1943 ('fromtimestamp', (ts,)),
1944 ('fromisoformat', (d_isoformat,)),
1945 ]
1946
1947 for constr_name, constr_args in test_cases:
1948 for base_obj in (DateSubclass, base_d):
1949 # Test both the classmethod and method
1950 with self.subTest(base_obj_type=type(base_obj),
1951 constr_name=constr_name):
1952 constr = getattr(base_obj, constr_name)
1953
1954 dt = constr(*constr_args)
1955
1956 # Test that it creates the right subclass
1957 self.assertIsInstance(dt, DateSubclass)
1958
1959 # Test that it's equal to the base object
1960 self.assertEqual(dt, base_d)
1961
1962 # Test that it called the constructor
1963 self.assertEqual(dt.extra, 7)
1964
1965 def test_pickling_subclass_date(self):
1966

Callers

nothing calls this directly

Calls 7

timeClass · 0.90
assertIsInstanceMethod · 0.80
timestampMethod · 0.80
DateSubclassClass · 0.70
assertEqualMethod · 0.45
combineMethod · 0.45
subTestMethod · 0.45

Tested by

no test coverage detected