Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Conjeaud committed Jun 7, 2024
1 parent dd6ea2f commit 399dfc5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions neomodel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BooleanProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
DateTimeProperty,
EmailProperty,
FloatProperty,
Expand Down
21 changes: 10 additions & 11 deletions neomodel/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys
import uuid
from datetime import date, datetime
import neo4j.time

import neo4j.time
import pytz
Expand Down Expand Up @@ -448,25 +447,26 @@ def deflate(self, value):
return float((value - epoch_date).total_seconds())


class DateTimeFormatNeo4j(Property):
class DateTimeNeo4jFormatProperty(Property):
"""
Store a datetime by native neo4j format
:param default_now: If ``True``, the creation time (Local) will be used as default.
Defaults to ``False``.
:type default_now: :class:`bool`
"""
form_field_class = 'DateTimeFormatFieldNeo4j'

form_field_class = "DateTimeNeo4jFormatField"

def __init__(self, default_now=False, **kwargs):
if default_now:
if 'default' in kwargs:
raise ValueError('too many defaults')
kwargs['default'] = lambda: datetime.now()
if "default" in kwargs:
raise ValueError("too many defaults")
kwargs["default"] = datetime.now()

Check warning on line 466 in neomodel/properties.py

View check run for this annotation

Codecov / codecov/patch

neomodel/properties.py#L464-L466

Added lines #L464 - L466 were not covered by tests

self.format = format
super(DateTimeFormatNeo4j, self).__init__(**kwargs)
super(DateTimeNeo4jFormatProperty, self).__init__(**kwargs)

@validator
def inflate(self, value):
Expand All @@ -475,11 +475,10 @@ def inflate(self, value):
@validator
def deflate(self, value):
if not isinstance(value, datetime):
raise ValueError('datetime object expected, got {0}.'.format(type(value)))
raise ValueError("datetime object expected, got {0}.".format(type(value)))
return neo4j.time.DateTime.from_native(value)




class JSONProperty(Property):
"""
Store a data structure as a JSON string.
Expand Down
7 changes: 5 additions & 2 deletions neomodel/scripts/neomodel_generate_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
BooleanProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
DateTimeProperty,
FloatProperty,
IntegerProperty,
Expand Down Expand Up @@ -107,8 +108,10 @@ def transform_property_type(prop_definition):
return "bool"
elif isinstance(prop_definition, DateProperty):
return "date"
elif isinstance(prop_definition, DateTimeProperty) or isinstance(
prop_definition, DateTimeFormatProperty
elif (
isinstance(prop_definition, DateTimeProperty)
or isinstance(prop_definition, DateTimeFormatProperty)
or isinstance(prop_definition, DateTimeNeo4jFormatProperty)
):
return "datetime"
elif isinstance(prop_definition, IntegerProperty):
Expand Down
14 changes: 14 additions & 0 deletions test/async_/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import date, datetime
from test._async_compat import mark_async_test

from neo4j import time
from pytest import mark, raises
from pytz import timezone

Expand All @@ -16,6 +17,7 @@
ArrayProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
DateTimeProperty,
EmailProperty,
IntegerProperty,
Expand Down Expand Up @@ -132,6 +134,18 @@ def test_datetime_format():
assert prop.inflate("2019-03-19 15:36:25") == some_datetime


def test_datetime_neo4j_format():
prop = DateTimeNeo4jFormatProperty()
prop.name = "foo"
prop.owner = FooBar
some_datetime = datetime(2022, 12, 10, 14, 00, 00)
assert prop.deflate(some_datetime) == time.DateTime(2022, 12, 10, 14, 00, 00)
assert prop.inflate(time.DateTime(2022, 12, 10, 14, 00, 00)) == some_datetime

with raises(ValueError, match=r"datetime object expected, got.*"):
prop.deflate(1234)


def test_datetime_exceptions():
prop = DateTimeProperty()
prop.name = "created"
Expand Down
14 changes: 14 additions & 0 deletions test/sync_/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import date, datetime
from test._async_compat import mark_sync_test

from neo4j import time
from pytest import mark, raises
from pytz import timezone

Expand All @@ -16,6 +17,7 @@
ArrayProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
DateTimeProperty,
EmailProperty,
IntegerProperty,
Expand Down Expand Up @@ -132,6 +134,18 @@ def test_datetime_format():
assert prop.inflate("2019-03-19 15:36:25") == some_datetime


def test_datetime_neo4j_format():
prop = DateTimeNeo4jFormatProperty()
prop.name = "foo"
prop.owner = FooBar
some_datetime = datetime(2022, 12, 10, 14, 00, 00)
assert prop.deflate(some_datetime) == time.DateTime(2022, 12, 10, 14, 00, 00)
assert prop.inflate(time.DateTime(2022, 12, 10, 14, 00, 00)) == some_datetime

with raises(ValueError, match=r"datetime object expected, got.*"):
prop.deflate(1234)


def test_datetime_exceptions():
prop = DateTimeProperty()
prop.name = "created"
Expand Down

0 comments on commit 399dfc5

Please sign in to comment.