async def _add_edge(self, edge): """Convenience function for generating crud traversals.""" props = mapper.map_props_to_db(edge, edge.__mapping__) traversal = self._g.V(Binding('sid', edge.source.id)) traversal = traversal.addE(edge.__mapping__._label) traversal = traversal.to(__.V(Binding('tid', edge.target.id))) traversal, _, _ = self._add_properties(traversal, props) result = await self._simple_traversal(traversal, edge) return result
async def test_unregistered_edge_desialization(self, app): session = await app.session() p1 = await session.g.addV('person').next() p2 = await session.g.addV('person').next() e1 = await session.g.V(Binding('p1_id', p1.id)).addE('unregistered').to( session.g.V(Binding('p2_id', p2.id)) ).property('how_long', 1).next() assert isinstance(e1, element.GenericEdge) assert e1.how_long == 1 assert e1.__label__ == 'unregistered' await app.close()
async def test_edge_desialization(self, app, knows_class): session = await app.session() p1 = await session.g.addV('person').next() p2 = await session.g.addV('person').next() e1 = await session.g.V(Binding('p1_id', p1.id)).addE('knows').to( session.g.V(Binding('p2_id', p2.id)) ).property(knows_class.notes, 'somehow').property('how_long', 1).next() assert isinstance(e1, knows_class) assert e1.notes == 'somehow' assert e1.how_long == 1 await app.close()
async def test_remove_vertex(self, app, person): session = await app.session() person.name = 'dave' person.age = 35 await session.save(person) result = await session.g.V(Binding('vid', person.id)).next() assert result is person rid = result.id await session.remove_vertex(person) result = await session.g.V(Binding('rid', rid)).next() assert not result await app.close()
async def _update_vertex_properties(self, vertex, traversal, props): traversal, removals, metaprops = self._add_properties(traversal, props) for k in removals: await self._g.V(Binding('vid', vertex.id)).properties(k).drop().next() result = await self._simple_traversal(traversal, vertex) if metaprops: removals = await self._add_metaprops(result, metaprops) for db_name, key, value in removals: await self._g.V(Binding('vid', vertex.id)).properties( db_name).has(key, value).drop().next() traversal = self._g.V(Binding('vid', vertex.id)) result = await self._simple_traversal(traversal, vertex) return result
async def _update_edge_properties(self, edge, traversal, props): traversal, removals, _ = self._add_properties(traversal, props) eid = edge.id if isinstance(eid, dict): eid = Binding('eid', edge.id) for k in removals: await self._g.E(eid).properties(k).drop().next() return await self._simple_traversal(traversal, edge)
async def test_property_deserialization(self, app): # In a future version this should deserialize to a Goblin vert prop??? session = await app.session() p1 = await session.g.addV('person').property('name', 'leif').next() traversal = session.g.V(Binding('p1_id', p1.id)).properties('name') name = await traversal.next() assert name.value == 'leif' assert name.label == 'name' await app.close()
async def get_vertex(self, vertex): """ Get a vertex from the db. Vertex must have id. :param goblin.element.Vertex element: Vertex to be retrieved :returns: :py:class:`Vertex<goblin.element.Vertex>` | None """ return await self.g.V(Binding('vid', vertex.id)).next()
async def test_submit_traversal(remote_graph, remote_connection): g = remote_graph.traversal().withRemote(remote_connection) resp = g.addV('person').property('name', 'leifur').valueMap(True) leif = await resp.next() assert leif['name'][0] == 'leifur' assert leif['label'] == 'person' resp = g.V(Binding('vid', leif['id'])).drop() none = await resp.next() assert none is None await remote_connection.close()
async def _add_vertex(self, vertex): """Convenience function for generating crud traversals.""" props = mapper.map_props_to_db(vertex, vertex.__mapping__) traversal = self._g.addV(vertex.__mapping__.label) traversal, _, metaprops = self._add_properties(traversal, props) result = await self._simple_traversal(traversal, vertex) if metaprops: await self._add_metaprops(result, metaprops) traversal = self._g.V(Binding('vid', vertex.id)) result = await self._simple_traversal(traversal, vertex) return result
async def update_vertex(self, vertex): """ Update a vertex, generally to change/remove property values. :param goblin.element.Vertex vertex: Vertex to be updated :returns: :py:class:`Vertex<goblin.element.Vertex>` object """ props = mapper.map_props_to_db(vertex, vertex.__mapping__) traversal = self._g.V(Binding('vid', vertex.id)) return await self._update_vertex_properties(vertex, traversal, props)
async def test_remove_edge(self, app, person_class, place_class, lives_in_class): try: session = await app.session() jon = person_class() jon.name = 'jonathan' jon.age = 38 montreal = place_class() montreal.name = 'Montreal' lives_in = lives_in_class(jon, montreal) session.add(jon, montreal, lives_in) await session.flush() result = await session.g.E(Binding('eid', lives_in.id)).next() assert result is lives_in rid = result.id await session.remove_edge(lives_in) result = await session.g.E(Binding('rid', rid)).next() assert not result finally: await app.close()
async def get_edge(self, edge): """ Get a edge from the db. Edge must have id. :param goblin.element.Edge element: Edge to be retrieved :returns: :py:class:`Edge<goblin.element.Edge>` | None """ eid = edge.id if isinstance(eid, dict): eid = Binding('eid', edge.id) return await self.g.E(eid).next()
async def _add_metaprops(self, result, metaprops): potential_removals = [] for metaprop in metaprops: db_name, (binding, value), metaprops = metaprop for key, val in metaprops.items(): if val: traversal = self._g.V(Binding('vid', result.id)).properties( db_name).hasValue(value).property(key, val) await traversal.next() else: potential_removals.append((db_name, key, value)) return potential_removals
async def remove_vertex(self, vertex): """ Remove a vertex from the db. :param goblin.element.Vertex vertex: Vertex to be removed """ traversal = self._g.V(Binding('vid', vertex.id)).drop() result = await self._simple_traversal(traversal, vertex) hashable_id = self._get_hashable_id(vertex.id) vertex = self.current.pop(hashable_id) del vertex return result
async def update_edge(self, edge): """ Update an edge, generally to change/remove property values. :param goblin.element.Edge edge: Edge to be updated :returns: :py:class:`Edge<goblin.element.Edge>` object """ props = mapper.map_props_to_db(edge, edge.__mapping__) eid = edge.id if isinstance(eid, dict): eid = Binding('eid', edge.id) traversal = self._g.E(eid) return await self._update_edge_properties(edge, traversal, props)
async def remove_edge(self, edge): """ Remove an edge from the db. :param goblin.element.Edge edge: Element to be removed """ eid = edge.id if isinstance(eid, dict): eid = Binding('eid', edge.id) traversal = self._g.E(eid).drop() result = await self._simple_traversal(traversal, edge) hashable_id = self._get_hashable_id(edge.id) edge = self.current.pop(hashable_id) del edge return result
async def test_non_element_deserialization(self, app): session = await app.session() p1 = await session.g.addV('person').property('name', 'leif').next() one = await session.g.V(Binding('p1_id', p1.id)).count().next() assert one == 1 await app.close()
async def _check_edge(self, edge): """Used to check for existence, does not update session edge""" eid = edge.id if isinstance(eid, dict): eid = Binding('eid', edge.id) return await self._g.E(eid).next()
async def _check_vertex(self, vertex): """Used to check for existence, does not update session vertex""" msg = await self._g.V(Binding('vid', vertex.id)).next() return msg