diff --git a/neo4j/time/__init__.py b/neo4j/time/__init__.py index 71652efe..54460f31 100644 --- a/neo4j/time/__init__.py +++ b/neo4j/time/__init__.py @@ -389,6 +389,12 @@ def __repr__(self): def __str__(self): return self.iso_format() + def __copy__(self): + return self.__new(self.ticks, self.hour, self.minute, self.second, self.tzinfo) + + def __deepcopy__(self, memodict={}): + return self.__copy__() + @classmethod def from_iso_format(cls, s): m = DURATION_ISO_PATTERN.match(s) @@ -845,6 +851,12 @@ def __sub__(self, other): except TypeError: return NotImplemented + def __copy__(self): + return self.__new(self.__ordinal, self.__year, self.__month, self.__day) + + def __deepcopy__(self, *args, **kwargs): + return self.__copy__() + # INSTANCE METHODS # def replace(self, **kwargs): @@ -1133,6 +1145,12 @@ def __add__(self, other): def __sub__(self, other): return NotImplemented + def __copy__(self): + return self.__new(self.__ticks, self.__hour, self.__minute, self.__second, self.__tzinfo) + + def __deepcopy__(self, *args, **kwargs): + return self.__copy__() + # INSTANCE METHODS # def replace(self, **kwargs): @@ -1474,6 +1492,12 @@ def __sub__(self, other): return self.__add__(-other) return NotImplemented + def __copy__(self): + return self.combine(self.__date, self.__time) + + def __deepcopy__(self, *args, **kwargs): + return self.__copy__() + # INSTANCE METHODS # def date(self): diff --git a/tests/unit/time/test_date.py b/tests/unit/time/test_date.py index 2ab0f120..6b76d1b5 100644 --- a/tests/unit/time/test_date.py +++ b/tests/unit/time/test_date.py @@ -24,6 +24,7 @@ from unittest import TestCase import pytz +import copy from neo4j.time import Duration, Date, UnixEpoch, ZeroDate @@ -532,3 +533,15 @@ def test_from_iso_format(self): expected = Date(2018, 10, 1) actual = Date.from_iso_format("2018-10-01") self.assertEqual(expected, actual) + + def test_date_copy(self): + d = Date(2010, 10, 1) + d2 = copy.copy(d) + self.assertIsNot(d, d2) + self.assertEqual(d, d2) + + def test_date_deep_copy(self): + d = Date(2010, 10, 1) + d2 = copy.deepcopy(d) + self.assertIsNot(d, d2) + self.assertEqual(d, d2) diff --git a/tests/unit/time/test_datetime.py b/tests/unit/time/test_datetime.py index 0f508bc3..42c9d676 100644 --- a/tests/unit/time/test_datetime.py +++ b/tests/unit/time/test_datetime.py @@ -18,7 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. - +import copy from unittest import TestCase from datetime import ( datetime, @@ -337,6 +337,18 @@ def test_from_iso_format_with_negative_long_tz(self): actual = DateTime.from_iso_format("2018-10-01T12:34:56.123456789-12:34:56.123456") self.assertEqual(expected, actual) + def test_datetime_copy(self): + d = DateTime(2010, 10, 1, 10, 0, 10) + d2 = copy.copy(d) + self.assertIsNot(d, d2) + self.assertEqual(d, d2) + + def test_datetime_deep_copy(self): + d = DateTime(2010, 10, 1, 10, 0, 12) + d2 = copy.deepcopy(d) + self.assertIsNot(d, d2) + self.assertEqual(d, d2) + def test_iso_format_with_time_zone_case_1(): # python -m pytest tests/unit/time/test_datetime.py -s -v -k test_iso_format_with_time_zone_case_1