Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_default_value_callable_type():
# check our object gets converted to str without serializing and reload
def factory():
class Foo(object):
def __str__(self):
return "123"
return Foo()
class DefaultTestValueThree(StructuredNode):
uid = StringProperty(default=factory, index=True)
x = DefaultTestValueThree()
assert x.uid == '123'
x.save()
assert x.uid == '123'
x.refresh()
assert x.uid == '123'
def test_default_value_callable():
def uid_generator():
return 'xx'
class DefaultTestValueTwo(StructuredNode):
uid = StringProperty(default=uid_generator, index=True)
a = DefaultTestValueTwo().save()
assert a.uid == 'xx'
def test_unique_index_prop_not_required():
class ConstrainedTestNode(StructuredNode):
required_property = StringProperty(required=True)
unique_property = StringProperty(unique_index=True)
unique_required_property = StringProperty(unique_index=True, required=True)
unconstrained_property = StringProperty()
# Create a node with a missing required property
with raises(RequiredProperty):
x = ConstrainedTestNode(required_property="required", unique_property="unique")
x.save()
# Create a node with a missing unique (but not required) property.
x = ConstrainedTestNode()
x.required_property = "required"
x.unique_required_property = "unique and required"
x.unconstrained_property = "no contraints"
x.save()
# check database property name on low level
results, meta = db.cypher_query("MATCH (n:ConstrainedTestNode) RETURN n")
node_properties = _get_node_properties(results[0][0])
def test_independent_property_name_get_or_create():
class TestNode(StructuredNode):
uid = UniqueIdProperty()
name_ = StringProperty(db_property="name", required=True)
# create the node
TestNode.get_or_create({'uid': 123, 'name_': 'jim'})
# test that the node is retrieved correctly
x = TestNode.get_or_create({'uid': 123, 'name_': 'jim'})[0]
# check database property name on low level
results, meta = db.cypher_query("MATCH (n:TestNode) RETURN n")
node_properties = _get_node_properties(results[0][0])
assert node_properties['name'] == "jim"
assert 'name_' not in node_properties
# delete node afterwards
x.delete()
def test_independent_property_name():
class TestDBNamePropertyNode(StructuredNode):
name_ = StringProperty(db_property="name")
x = TestDBNamePropertyNode()
x.name_ = "jim"
x.save()
# check database property name on low level
results, meta = db.cypher_query("MATCH (n:TestDBNamePropertyNode) RETURN n")
node_properties = _get_node_properties(results[0][0])
assert node_properties['name'] == "jim"
node_properties = _get_node_properties(results[0][0])
assert not 'name_' in node_properties
assert not hasattr(x, 'name')
assert hasattr(x, 'name_')
assert TestDBNamePropertyNode.nodes.filter(name_="jim").all()[0].name_ == x.name_
assert TestDBNamePropertyNode.nodes.get(name_="jim").name_ == x.name_
def test_default_value():
class DefaultTestValue(StructuredNode):
name_xx = StringProperty(default='jim', index=True)
a = DefaultTestValue()
assert a.name_xx == 'jim'
a.save()
return
b = DefaultTestValue.index.get(name='jim')
assert b.name == 'jim'
c = DefaultTestValue(name=None)
assert c.name == 'jim'
def test_uid_property():
prop = UniqueIdProperty()
prop.name = 'uid'
prop.owner = object()
myuid = prop.default_value()
assert len(myuid)
class CheckMyId(StructuredNode):
uid = UniqueIdProperty()
cmid = CheckMyId().save()
assert len(cmid.uid)
class ArrayProps(StructuredNode):
uid = StringProperty(unique_index=True)
untyped_arr = ArrayProperty()
typed_arr = ArrayProperty(IntegerProperty())
def test_array_properties():
# untyped
ap1 = ArrayProps(uid='1', untyped_arr=['Tim', 'Bob']).save()
assert 'Tim' in ap1.untyped_arr
ap1 = ArrayProps.nodes.get(uid='1')
assert 'Tim' in ap1.untyped_arr
# typed
try:
ArrayProps(uid='2', typed_arr=['a', 'b']).save()
except DeflateError as e:
assert 'unsaved node' in str(e)
def test_unique_index_prop_enforced():
class UniqueNullableNameNode(StructuredNode):
name = StringProperty(unique_index=True)
# Nameless
x = UniqueNullableNameNode()
x.save()
y = UniqueNullableNameNode()
y.save()
# Named
z = UniqueNullableNameNode(name="named")
z.save()
with raises(UniqueProperty):
a = UniqueNullableNameNode(name="named")
a.save()
# Check nodes are in database
results, meta = db.cypher_query("MATCH (n:UniqueNullableNameNode) RETURN n")
def test_unique_index_prop_not_required():
class ConstrainedTestNode(StructuredNode):
required_property = StringProperty(required=True)
unique_property = StringProperty(unique_index=True)
unique_required_property = StringProperty(unique_index=True, required=True)
unconstrained_property = StringProperty()
# Create a node with a missing required property
with raises(RequiredProperty):
x = ConstrainedTestNode(required_property="required", unique_property="unique")
x.save()
# Create a node with a missing unique (but not required) property.
x = ConstrainedTestNode()
x.required_property = "required"
x.unique_required_property = "unique and required"
x.unconstrained_property = "no contraints"
x.save()
# check database property name on low level
results, meta = db.cypher_query("MATCH (n:ConstrainedTestNode) RETURN n")
def default_value(self):
return self.normalize(super(StringProperty, self).default_value())