def __init__(self, element_class, client, graph=None, logger=None): """ Initializes the repository with the bulbs proxy and our metadata. :param graph: Bulbs graph object. :type graph: bulbs.base.graph.Graph :param name: Name of the repository. :type name: str :param metadata: Metadata associated with the current model. :type metadata: graphalchemy.metadata.GraphMetadata :param logger: Optional logger to listen on queries. :type logger: jerome.application.service.logger.LoggerInterface """ self.graph = graph if hasattr(element_class, 'element_type'): repository_name = str(element_class.element_type) elif hasattr(element_class, 'label'): repository_name = str(element_class.label) else: raise Exception('Element class seems not to be Node nor Relationship.') if graph: self.graph.add_proxy(repository_name, element_class) client = self.graph.client self.name = repository_name super(BulbsNodeRepository, self).__init__(element_class, client) self.metadata = BulbsMetadata(element_class) self.logger = logger
def test__build_query_filter(self): self.graph = TestGraph() self.graph.client = self.ogm.client self.metadata = BulbsMetadata(Website) # No metadata metadata = None repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = metadata self.assertEquals((u'g.v(eid)', { 'eid': 123 }), repository._build_query_filter(eid=123)) self.assertEquals((u'g.V.has("name", name)', { u'name': 'Foo' }), repository._build_query_filter(name='Foo')) self.assertEquals((u'g.V.has("content", content).has("name", name)', { u'content': 'Bar', u'name': 'Foo' }), repository._build_query_filter(name='Foo', content='Bar')) # Empty metadata self.metadata.indices = [] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals((u'g.v(eid)', { 'eid': 123 }), repository._build_query_filter(eid=123)) self.assertEquals((u'g.V.has("name", name)', { u'name': 'Foo' }), repository._build_query_filter(name='Foo')) self.assertEquals((u'g.V.has("content", content).has("name", name)', { u'content': 'Bar', u'name': 'Foo' }), repository._build_query_filter(name='Foo', content='Bar')) # Metadata with an index self.metadata.indices = ['name'] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals((u'g.v(eid)', { 'eid': 123 }), repository._build_query_filter(eid=123)) self.assertEquals((u'g.V("name", name)', { u'name': 'Foo' }), repository._build_query_filter(name='Foo')) self.assertEquals((u'g.V("name", name).has("content", content)', { u'content': 'Bar', u'name': 'Foo' }), repository._build_query_filter(name='Foo', content='Bar')) self.assertEquals((u'g.v(eid).has("name", name)', { 'eid': 123, u'name': 'Foo' }), repository._build_query_filter(eid=123, name='Foo'))
def test__get_index(self): self.graph = TestGraph() self.graph.client = self.ogm.client self.metadata = BulbsMetadata(Website) repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = None self.assertIs(None, repository._get_index()) self.metadata.indices = [] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals([], repository._get_index()) self.metadata.indices = ['name'] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals(['name'], repository._get_index())
def test__get_index_among(self): self.graph = TestGraph() self.graph.client = self.ogm.client self.metadata = BulbsMetadata(Website) self.metadata.indices = Mock() repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = None self.assertIs(None, repository._get_index_among([])) self.metadata.indices = [] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertIs(None, repository._get_index_among([])) self.metadata.indices = ['name'] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals(['name'], repository._get_index_among(['name', 'content'])) self.metadata.indices = ['name'] repository = BulbsNodeRepository(Website, self.graph.client, graph=self.graph) repository.metadata = self.metadata self.assertEquals( None, repository._get_index_among(['content', 'description']))
def test___init__(self): metadata = BulbsMetadata(User) self.assertEquals(metadata.indices, ['firstname'])