def getLoader(user, repo, sha=None, prov=None): """Build a fileLoader (LocalLoader or GithubLoader) for the given repository.""" if user is None and repo is None: loader = LocalLoader() else: loader = GithubLoader(user, repo, sha, prov) return loader
def getLoader(user, repo, subdir=None, spec_url=None, sha=None, prov=None): """Build a fileLoader (LocalLoader, GithubLoader, URLLoader) for the given parameters.""" if user is None and repo is None and not spec_url: loader = LocalLoader() elif spec_url: loader = URLLoader(spec_url) else: loader = GithubLoader(user, repo, subdir, sha, prov) return loader
def getLoader(user, repo, subdir=None, query_urls=[], sha=None, prov=None): """Build a fileLoader (LocalLoader, GithubLoader, ParamLoader) for the given parameters.""" if user is None and repo is None and not query_urls: loader = LocalLoader() elif query_urls: loader = ParamLoader(query_urls) else: loader = GithubLoader(user, repo, subdir, sha, prov) return loader
class TestLocalLoader(unittest.TestCase): @classmethod def setUpClass(self): self.loader = LocalLoader('./tests/repo/') def test_fetchFiles(self): files = self.loader.fetchFiles() # Should return a list of file items self.assertIsInstance(files, list, "Should return a list of file items") # Should have N files (where N=5) self.assertEquals(len(files), 5, "Should return correct number of files") # File items should have a download_url for fItem in files: self.assertIn('download_url', fItem, "File items should have a download_url") def test_getRawRepoUri(self): repoUri = self.loader.getRawRepoUri() # Should be a string self.assertIsInstance(repoUri, six.string_types, "Should be a string") # For local repo, should be empty ? self.assertEqual(repoUri, "", "Should be an empty string") def test_getTextFor(self): files = self.loader.fetchFiles() # the contents of each file for fItem in files: text = self.loader.getTextFor(fItem) # Should be some text self.assertIsInstance(text, six.string_types, "Should be some text") # Should be non-empty for existing items self.assertGreater(len(text), 0, "Should be non-empty") # Should raise exception for invalid file items with self.assertRaises( Exception, message="Should raise exception for invalid file items"): text = self.loader.getTextFor({}) def test_getTextForName(self): testableNames = [('test-rq', qType['SPARQL']), ('test-sparql', qType['SPARQL']), ('test-tpf', qType['TPF'])] for name, expectedType in testableNames: text, actualType = self.loader.getTextForName(name) self.assertEqual( expectedType, actualType, "Query type should match %s != %s" % (expectedType, actualType))
def setUpClass(self): self.loader = LocalLoader('./tests/repo/') self.app = Flask('unittests')
class TestGQuery(unittest.TestCase): @classmethod def setUpClass(self): self.loader = LocalLoader('./tests/repo/') self.app = Flask('unittests') def test_guess_endpoint(self): with self.app.test_request_context('/?endpoint=http://url-endpoint/from-url/sparql'): endpoint, _ = gquery.guess_endpoint_uri('', self.loader) self.assertIn('from-url', endpoint, 'Should match endpoint given in url') with self.app.test_request_context('/'): endpoint, _ = gquery.guess_endpoint_uri('', self.loader) self.assertIn('from-file', endpoint, 'Should match endpoint in endpoint.txt') rq, _ = self.loader.getTextForName('test-rq') endpoint, _ = gquery.guess_endpoint_uri(rq, self.loader) self.assertIn('from-decorator', endpoint, 'Should match endpoint in test-rq.rq') def test_get_parameters(self): rq, _ = self.loader.getTextForName('test-rq') params = gquery.get_parameters(rq, '', '', {}) for paramName, param in params.items(): self.assertIn('name', param, 'Should have a name') self.assertIn('type', param, 'Should have a type') self.assertIn('required', param, 'Should have a required') orig = param['original'] if '_iri' in orig: self.assertEqual(param['type'], 'string', 'Should be type string') self.assertEqual(param['format'], 'iri', 'Should be format iri') if '_number' in orig: self.assertEqual(param['type'], 'number', 'Should be type number') if '_literal' in orig: self.assertEqual(param['type'], 'literal', 'Should be type literal') if '_en' in orig: self.assertEqual(param['type'], 'literal', 'Should be type literal') self.assertEqual(param['lang'], 'en', 'Should be en language') if '_integer' in orig: self.assertEqual( param['datatype'], 'xsd:integer', 'Should be type xsd:integer') if '_xsd_date' in orig: self.assertEqual(param['datatype'], 'xsd:date', 'Should be type xsd:date') @patch('requests.get') def test_get_enumeration(self, mock_get): mock_get.return_value = Mock(ok=True) mock_get.return_value.json.return_value = { 'results': { 'bindings': [ {'o1': {'value': 'v1'}}, {'o1': {'value': 'v2'}} ] } } rq, _ = self.loader.getTextForName('test-rq') metadata = {'enumerate': 'o1'} enumeration = gquery.get_enumeration(rq, 'o1', 'http://mock-endpoint/sparql', metadata) self.assertIsInstance(enumeration, list, 'Should return a list of values') self.assertEqual(len(enumeration), 2, 'Should have two elements') def test_get_static_enumeration(self): rq, _ = self.loader.getTextForName('test-enum') metadata = gquery.get_yaml_decorators(rq) self.assertIn('enumerate', metadata, 'Should contain enumerate') enumeration = gquery.get_enumeration(rq, 'o', 'http://mock-endpoint/sparql', metadata) self.assertIsInstance(enumeration, list, 'Should return a list of values') self.assertEqual(len(enumeration), 2, 'Should have two elements') def test_get_yaml_decorators(self): rq, _ = self.loader.getTextForName('test-sparql') decorators = gquery.get_yaml_decorators(rq) # Query always exist -- the rest must be present on the file. self.assertIn('query', decorators, 'Should have a query field') self.assertIn('summary', decorators, 'Should have a summary field') self.assertIn('pagination', decorators, 'Should have a pagination field') self.assertIn('enumerate', decorators, 'Should have a enumerate field') self.assertIsInstance( decorators['summary'], six.string_types, 'Summary should be text') self.assertIsInstance( decorators['pagination'], int, 'Pagination should be numeric') self.assertIsInstance( decorators['enumerate'], list, 'Enumerate should be a list') def test_get_json_decorators(self): rq, _ = self.loader.getTextForName('test-sparql-jsonconf') decorators = gquery.get_yaml_decorators(rq) # Query always exist -- the rest must be present on the file. self.assertIn('query', decorators, 'Should have a query field') self.assertIn('summary', decorators, 'Should have a summary field') self.assertIn('pagination', decorators, 'Should have a pagination field') self.assertIn('enumerate', decorators, 'Should have a enumerate field') self.assertIsInstance( decorators['summary'], six.string_types, 'Summary should be text') self.assertIsInstance( decorators['pagination'], int, 'Pagination should be numeric') self.assertIsInstance( decorators['enumerate'], list, 'Enumerate should be a list') def test_get_metadata(self): rq, _ = self.loader.getTextForName('test-sparql') metadata = gquery.get_metadata(rq, '') self.assertIn('type', metadata, 'Should have a type field') self.assertIn('variables', metadata, 'Should have a variables field') self.assertEqual(metadata['type'], 'SelectQuery', 'Should be type SelectQuery') self.assertIsInstance( metadata['variables'], list, 'Should be a list of variables') for var in metadata['variables']: self.assertIsInstance(var, rdflib.term.Variable, 'Should be of type Variable') def test_paginate_query(self): rq, _ = self.loader.getTextForName('test-sparql') rq_pag = gquery.paginate_query(rq, 100, {}) self.assertNotIn( 'LIMIT', rq, 'Original query should not contain LIMIT keyword') self.assertIn('LIMIT', rq_pag, 'Paginated query should contain LIMIT keyword') self.assertNotIn( 'OFFSET', rq, 'Original query should not contain OFFSET keyword') self.assertIn('OFFSET', rq_pag, 'Paginated query should contain OFFSET keyword') @staticmethod def build_get_parameter(origName, rwName): """Builds parameter description in the format returned by gquery.get_parameters""" return { 'original': '?_{}'.format(origName), 'name': rwName, 'required': False, 'enum': [], 'type': 'literal', 'datatype': 'xsd:string', 'lang': 'en', 'format': None } def test_rewrite_query(self): rq, _ = self.loader.getTextForName('test-rq') # Parameters on the format returned by gquery.get_parameters parameters = { 'o1': self.build_get_parameter('o1', 'x1'), 'o2': self.build_get_parameter('o2', 'x2'), 'o3': self.build_get_parameter('o3', 'x3'), 'o4': self.build_get_parameter('o4', 'x4'), 'o5': self.build_get_parameter('o5', 'x5'), 'o6': self.build_get_parameter('o6', 'x6'), 'o7': self.build_get_parameter('o7', 'x7') } args = { 'o1': 'x1', 'o2': 'x2', 'o3': 'x3', 'o4': 'x4', 'o5': 'x5', 'o6': 'x6', 'o7': 'x7' } # Rewritten query will probably be incorrect because parameters are not # carefully constructed, but that is not the scope of this test rq_rw = gquery.rewrite_query(rq, parameters, args) for pName, pValue in parameters.items(): self.assertIn( pName, rq, 'Original query should contain original parameter name') self.assertNotIn( pName, rq_rw, 'Rewritten query should not contain original parameter name') self.assertNotIn( pValue['name'], rq, 'Original query should not contain replacement parameter value') self.assertIn( pValue['name'], rq_rw, 'Rewritten query should contain replacement parameter value')
def setUpClass(self): self.loader = LocalLoader('./tests/repo/')
def setUpClass(self): self.loader = LocalLoader(path.join('tests', 'repo'))
"value": "o4" } }, { "p": { "type": "string", "value": "p5" }, "o": { "type": "string", "value": "o5" } }] } } def mock_process_sparql_query_text(query_text, raw_repo_uri, call_name, extraMetadata): mockItem = {"status": "This is a mock item", "call_name": call_name} return mockItem filesInRepo = [{ u'name': u'fakeFile1.rq', u'download_url': u'https://example.org/path/to/fakeFile.rq', u'decoded_content': 'CONTENT ?'.encode() # Because Github ContentFile object contains bytes. }] mockLoader = LocalLoader(base_url)
class TestUtils(unittest.TestCase): @classmethod def setUpClass(self): self.loader = LocalLoader('./tests/repo/') @patch('requests.get') def test_sparql_transformer(self, mock_get): mock_json = { "head": {}, "results": { "bindings": [{ "id": { "type": "uri", "value": "http://www.w3.org/2001/XMLSchema#anyURI" }, "class": { "type": "uri", "value": "http://www.w3.org/2000/01/rdf-schema#Datatype" }, "v2": { "type": "literal", "xml:lang": "en", "value": "xsd:anyURI" } }, { "id": { "type": "uri", "value": "http://www.w3.org/2001/XMLSchema#boolean" }, "class": { "type": "uri", "value": "http://www.w3.org/2000/01/rdf-schema#Datatype" }, "v2": { "type": "literal", "xml:lang": "en", "value": "xsd:boolean" } }] } } mock_get.return_value = Mock(ok=True) mock_get.return_value.headers = {'Content-Type': 'application/json'} mock_get.return_value.text = json.dumps(mock_json) rq, _ = self.loader.getTextForName('test-json') self.assertIn('proto', rq) resp, status, headers = utils.dispatchSPARQLQuery( rq, self.loader, content=None, requestArgs={}, acceptHeader='application/json', requestUrl='http://mock-endpoint/sparql', formData={}) self.assertEqual(status, 200) self.assertIsInstance(resp, list) self.assertIn('http', resp[0]['id']) def validateTestResponse(self, resp): self.assertIsInstance(resp, list, 'Response should be a list') self.assertEqual(len(resp), 5, 'Response should have 5 entries') for item in resp: self.assertTrue('key' in item, 'Response items should contain a key') self.assertTrue('value' in item, 'Response items should contain a value') keys = [item['key'] for item in resp] values = [item['value'] for item in resp] self.assertTrue(all(k in keys for k in ['p1', 'p2', 'p3', 'p4', 'p5']), 'Response should contain all known keys') self.assertTrue( all(v in values for v in ['o1', 'o2', 'o3', 'o4', 'o5']), 'Response should contain all known values') def setMockGetResponse(self): return_value = Mock(ok=True) return_value.headers = {'Content-Type': 'application/json'} return_value.text = json.dumps(mock_simpleSparqlResponse) return return_value @patch('requests.get') def test_dispatch_SPARQL_query(self, mock_get): mock_get.return_value = self.setMockGetResponse() rq, _ = self.loader.getTextForName('test-projection') resp, status, headers = utils.dispatchSPARQLQuery( rq, self.loader, content=None, requestArgs={'id': 'http://dbpedia.org/resource/Frida_Kahlo'}, acceptHeader='application/json', requestUrl='http://mock-endpoint/sparql', formData={}) self.validateTestResponse(resp) @patch('grlc.utils.getLoader') @patch('requests.get') def test_dispatch_query(self, mock_get, mock_loader): mock_get.return_value = self.setMockGetResponse() mock_loader.return_value = self.loader resp, status, headers = utils.dispatch_query( None, None, 'test-projection', requestArgs={'id': 'http://dbpedia.org/resource/Frida_Kahlo'}) self.validateTestResponse(resp) self.assertNotEqual(status, 404)