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_prohibited_constructor_forms():
"""
Tests all the possible forms by which construction of NeomodelPoints should fail.
:return:
"""
# Neo4j versions lower than 3.4.0 do not support Point. In that case, skip the test.
check_and_skip_neo4j_least_version(340, 'This version does not support spatial data types.')
# Absurd CRS
with pytest.raises(ValueError, match='Invalid CRS\(blue_hotel\)'):
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0,0), crs='blue_hotel')
# Absurd coord dimensionality
with pytest.raises(ValueError, match='Invalid vector dimensions. Expected 2 or 3, received 7'):
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0,0,0,0,0,0,0), crs='cartesian')
# Absurd datatype passed to copy constructor
with pytest.raises(TypeError, match='Invalid object passed to copy constructor'):
new_point = neomodel.contrib.spatial_properties.NeomodelPoint('it don''t mean a thing if it '
'ain''t got that swing', crs='cartesian')
# Trying to instantiate a point with any of BOTH x,y,z or longitude, latitude, height
with pytest.raises(ValueError, match='Invalid instantiation via arguments'):
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0,
longitude=0.0, latitude=2.0, height=-2.0,
crs='cartesian')
def test_property_accessors_depending_on_crs():
"""
Tests that points are accessed via their respective accessors.
:return:
"""
# Neo4j versions lower than 3.4.0 do not support Point. In that case, skip the test.
check_and_skip_neo4j_least_version(340, 'This version does not support spatial data types.')
# Geometrical points only have x,y,z coordinates
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='cartesian-3d')
with pytest.raises(AttributeError, match='Invalid coordinate \("longitude"\)'):
new_point.longitude
with pytest.raises(AttributeError, match='Invalid coordinate \("latitude"\)'):
new_point.latitude
with pytest.raises(AttributeError, match='Invalid coordinate \("height"\)'):
new_point.height
# Geographical points only have longitude, latitude, height coordinates
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
with pytest.raises(AttributeError, match='Invalid coordinate \("x"\)'):
new_point.x
with pytest.raises(AttributeError, match='Invalid coordinate \("y"\)'):
new_point.y
with pytest.raises(AttributeError, match='Invalid coordinate \("z"\)'):
new_point.z
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
basic_type_assertions(ground_truth_object, new_point,
'Explicit 3d geographical point with tuple of coords instantiation')
# Cartesian point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 3d point with named arguments')
# Geographical point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0, height=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0 ,height=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 3d point with named arguments')
def get_some_point():
return neomodel.contrib.spatial_properties.NeomodelPoint((random.random(),random.random()))
"""
A very simple entity to try out the default value assignment.
"""
identifier = neomodel.UniqueIdProperty()
location = neomodel.contrib.spatial_properties.PointProperty(crs='cartesian', default=get_some_point)
# Neo4j versions lower than 3.4.0 do not support Point. In that case, skip the test.
check_and_skip_neo4j_least_version(340, 'This version does not support spatial data types.')
# Save an object
an_object = LocalisableEntity().save()
coords = an_object.location.coords[0]
# Retrieve it
retrieved_object = LocalisableEntity.nodes.get(identifier=an_object.identifier)
# Check against an independently created value
assert retrieved_object.location == neomodel.contrib.spatial_properties.NeomodelPoint(coords), \
"Default value assignment failed."
# Cartesian point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 3d point with named arguments')
# Geographical point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0, height=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0 ,height=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 3d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0), crs='wgs-84')
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0), crs='wgs-84')
basic_type_assertions(ground_truth_object, new_point,
'Explicit 2d geographical point with tuple of coords instantiation')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
basic_type_assertions(ground_truth_object, new_point,
'Explicit 3d geographical point with tuple of coords instantiation')
# Cartesian point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 3d point with named arguments')
# Geographical point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0, height=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0 ,height=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 3d point with named arguments')
basic_type_assertions(ground_truth_object, new_point, 'Implicit 3d cartesian point instantiation')
# Explicit geographical point with coords
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0), crs='wgs-84')
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0), crs='wgs-84')
basic_type_assertions(ground_truth_object, new_point,
'Explicit 2d geographical point with tuple of coords instantiation')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
new_point = neomodel.contrib.spatial_properties.NeomodelPoint((0.0, 0.0, 0.0), crs='wgs-84-3d')
basic_type_assertions(ground_truth_object, new_point,
'Explicit 3d geographical point with tuple of coords instantiation')
# Cartesian point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(x=0.0, y=0.0, z=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Cartesian 3d point with named arguments')
# Geographical point with named arguments
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 2d point with named arguments')
ground_truth_object = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0, height=0.0)
new_point = neomodel.contrib.spatial_properties.NeomodelPoint(longitude=0.0, latitude=0.0 ,height=0.0)
basic_type_assertions(ground_truth_object, new_point, 'Geographical 3d point with named arguments')
:return:
"""
class AnotherLocalisableEntity(neomodel.StructuredNode):
"""
A very simple entity with an array of locations
"""
identifier = neomodel.UniqueIdProperty()
locations = neomodel.ArrayProperty(neomodel.contrib.spatial_properties.PointProperty(crs='cartesian'))
# Neo4j versions lower than 3.4.0 do not support Point. In that case, skip the test.
check_and_skip_neo4j_least_version(340, 'This version does not support spatial data types.')
an_object = AnotherLocalisableEntity(locations=
[neomodel.contrib.spatial_properties.NeomodelPoint((0.0,0.0)),
neomodel.contrib.spatial_properties.NeomodelPoint((1.0,0.0))]).save()
retrieved_object = AnotherLocalisableEntity.nodes.get(identifier=an_object.identifier)
assert type(retrieved_object.locations) is list, "Array of Points definition failed."
assert retrieved_object.locations == [neomodel.contrib.spatial_properties.NeomodelPoint((0.0,0.0)),
neomodel.contrib.spatial_properties.NeomodelPoint((1.0,0.0))], \
"Array of Points incorrect values."
def deflate(self, value):
"""
Handles the marshalling from NeomodelPoint to Neo4J POINT
:param value: The point that was assigned as value to a property in the model
:type value: NeomodelPoint
:return: Neo4J POINT
"""
if not isinstance(value, NeomodelPoint):
raise TypeError('Invalid datatype to deflate. Expected NeomodelPoint, received {}'.format(type(value)))
if not value.crs == self._crs:
raise ValueError('Invalid CRS. '
'Expected NeomodelPoint defined over {}, '
'received NeomodelPoint defined over {}'.format(self._crs, value.crs))
if value.crs == 'cartesian-3d':
return neo4j.v1.spatial.CartesianPoint((value.x, value.y, value.z))
elif value.crs == 'cartesian':
return neo4j.v1.spatial.CartesianPoint((value.x,value.y))
elif value.crs == 'wgs-84':
return neo4j.v1.spatial.WGS84Point((value.longitude, value.latitude))
elif value.crs == 'wgs-84-3d':
return neo4j.v1.spatial.WGS84Point((value.longitude, value.latitude, value.height))