Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Conjeaud committed Jun 7, 2024
1 parent 4fde92c commit 79fe25b
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
76 changes: 76 additions & 0 deletions test/async_/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from neomodel.properties import (
ArrayProperty,
BooleanProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
Expand All @@ -26,6 +27,7 @@
RegexProperty,
StringProperty,
UniqueIdProperty,
validator,
)
from neomodel.util import get_graph_entity_properties

Expand Down Expand Up @@ -78,6 +80,12 @@ class TestChoices(AsyncStructuredNode):
node = await TestChoices(sex="M").save()
assert node.get_sex_display() == "Male"

with raises(ValueError):

class WrongChoices(AsyncStructuredNode):
WRONG = "wrong"
wrong_prop = StringProperty(choices=WRONG)


def test_deflate_inflate():
prop = IntegerProperty(required=True)
Expand All @@ -98,6 +106,25 @@ def test_deflate_inflate():
else:
assert False, "DeflateError not raised."

with raises(ValueError, match="Unknown Property method tartiflate"):

class CheeseProperty(IntegerProperty):
@validator
def tartiflate(self, value):
return int(value)


def test_boolean_property():
prop = BooleanProperty()
prop.name = "foo"
prop.owner = FooBar
assert prop.deflate(True) is True
assert prop.deflate(False) is False
assert prop.inflate(True) is True
assert prop.inflate(False) is False

assert prop.has_default is False


def test_datetimes_timezones():
prop = DateTimeProperty()
Expand Down Expand Up @@ -203,6 +230,35 @@ def test_date_exceptions():
assert False, "DeflateError not raised."


def test_base_exceptions():
# default-required conflict
with raises(
ValueError,
match="The arguments `required` and `default` are mutually exclusive.",
):
_ = StringProperty(default="kakapo", required=True)

# unique_index - index conflict
with raises(
ValueError,
match="The arguments `unique_index` and `index` are mutually exclusive.",
):
_ = IntegerProperty(index=True, unique_index=True)

# no default value
kakapo = StringProperty()
with raises(ValueError, match="No default value specified"):
kakapo.default_value()

# missing normalize method
class WoopsProperty(NormalizedProperty):
pass

woops = WoopsProperty()
with raises(NotImplementedError, match="Specialize normalize method"):
woops.normalize("kakapo")


def test_json():
prop = JSONProperty()
prop.name = "json"
Expand All @@ -214,6 +270,17 @@ def test_json():
assert prop.inflate('{"test": [1, 2, 3]}') == value


def test_indexed():
indexed = StringProperty(index=True)
assert indexed.is_indexed is True

unique_indexed = StringProperty(unique_index=True)
assert unique_indexed.is_indexed is True

not_indexed = StringProperty()
assert not_indexed.is_indexed is False


@mark_async_test
async def test_default_value():
class DefaultTestValue(AsyncStructuredNode):
Expand Down Expand Up @@ -477,6 +544,15 @@ async def test_array_properties():
ap2 = await ArrayProps.nodes.get(uid="2")
assert 2 in ap2.typed_arr

class Kakapo:
pass

with raises(TypeError, match="Expecting neomodel Property"):
ArrayProperty(Kakapo)

with raises(TypeError, match="Cannot have nested ArrayProperty"):
ArrayProperty(ArrayProperty())


def test_illegal_array_base_prop_raises():
with raises(ValueError):
Expand Down
76 changes: 76 additions & 0 deletions test/sync_/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from neomodel.properties import (
ArrayProperty,
BooleanProperty,
DateProperty,
DateTimeFormatProperty,
DateTimeNeo4jFormatProperty,
Expand All @@ -26,6 +27,7 @@
RegexProperty,
StringProperty,
UniqueIdProperty,
validator,
)
from neomodel.util import get_graph_entity_properties

Expand Down Expand Up @@ -78,6 +80,12 @@ class TestChoices(StructuredNode):
node = TestChoices(sex="M").save()
assert node.get_sex_display() == "Male"

with raises(ValueError):

class WrongChoices(StructuredNode):
WRONG = "wrong"
wrong_prop = StringProperty(choices=WRONG)


def test_deflate_inflate():
prop = IntegerProperty(required=True)
Expand All @@ -98,6 +106,25 @@ def test_deflate_inflate():
else:
assert False, "DeflateError not raised."

with raises(ValueError, match="Unknown Property method tartiflate"):

class CheeseProperty(IntegerProperty):
@validator
def tartiflate(self, value):
return int(value)


def test_boolean_property():
prop = BooleanProperty()
prop.name = "foo"
prop.owner = FooBar
assert prop.deflate(True) is True
assert prop.deflate(False) is False
assert prop.inflate(True) is True
assert prop.inflate(False) is False

assert prop.has_default is False


def test_datetimes_timezones():
prop = DateTimeProperty()
Expand Down Expand Up @@ -203,6 +230,35 @@ def test_date_exceptions():
assert False, "DeflateError not raised."


def test_base_exceptions():
# default-required conflict
with raises(
ValueError,
match="The arguments `required` and `default` are mutually exclusive.",
):
_ = StringProperty(default="kakapo", required=True)

# unique_index - index conflict
with raises(
ValueError,
match="The arguments `unique_index` and `index` are mutually exclusive.",
):
_ = IntegerProperty(index=True, unique_index=True)

# no default value
kakapo = StringProperty()
with raises(ValueError, match="No default value specified"):
kakapo.default_value()

# missing normalize method
class WoopsProperty(NormalizedProperty):
pass

woops = WoopsProperty()
with raises(NotImplementedError, match="Specialize normalize method"):
woops.normalize("kakapo")


def test_json():
prop = JSONProperty()
prop.name = "json"
Expand All @@ -214,6 +270,17 @@ def test_json():
assert prop.inflate('{"test": [1, 2, 3]}') == value


def test_indexed():
indexed = StringProperty(index=True)
assert indexed.is_indexed is True

unique_indexed = StringProperty(unique_index=True)
assert unique_indexed.is_indexed is True

not_indexed = StringProperty()
assert not_indexed.is_indexed is False


@mark_sync_test
def test_default_value():
class DefaultTestValue(StructuredNode):
Expand Down Expand Up @@ -473,6 +540,15 @@ def test_array_properties():
ap2 = ArrayProps.nodes.get(uid="2")
assert 2 in ap2.typed_arr

class Kakapo:
pass

with raises(TypeError, match="Expecting neomodel Property"):
ArrayProperty(Kakapo)

with raises(TypeError, match="Cannot have nested ArrayProperty"):
ArrayProperty(ArrayProperty())


def test_illegal_array_base_prop_raises():
with raises(ValueError):
Expand Down

0 comments on commit 79fe25b

Please sign in to comment.