Skip to content

Allow copy of date objects #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions neo4j/time/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/time/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from unittest import TestCase

import pytz
import copy

from neo4j.time import Duration, Date, UnixEpoch, ZeroDate

Expand Down Expand Up @@ -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)
14 changes: 13 additions & 1 deletion tests/unit/time/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down