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 repository(self, repository_name): """ Returns the repository corresponding to a given model. :param repository_name: The name of the repository to load. :type repository_name: str :returns: graphalchemy.repository.BulbsNodeRepository -- the requested repository, loaded with the model metadata. """ if repository_name in self._repositorys: return self._repositorys[repository_name] found = False for module_name in self.model_paths: module = importlib.import_module(module_name) if repository_name not in module.__dict__: continue found = True break if not found: raise Exception('Repository not found.') # Register proxy and metdata model = module.__dict__[repository_name] # Build repository if issubclass(model, Node): repository = BulbsNodeRepository(model, self.client, graph=self.graph, logger=self.logger) elif issubclass(model, Relationship): self.graph.add_proxy(repository_name, model) repository = BulbsRelationshipRepository(model, self.client, graph=self.graph, logger=self.logger) else: raise Exception( 'Element class seems not to be Node nor Relationship.') self._repositorys[repository_name] = repository return repository
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_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__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_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__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())