def test_to_string_witn_non_default_params(self): url_params = dict(page="1", sort_by="rdfs:label", per_page="10") handler = MockHandler(**url_params) pd = ParamDict(handler, **url_params) self.assertEquals( pd.to_string(), "expand_uri=0&lang=pt&page=1&per_page=10&sort_by=rdfs:label")
def test_initialize(self): handler = MockHandler() pd = ParamDict(handler, context_name="context_name", class_name="class_name") self.assertIn("context_name", pd) self.assertIn("class_name", pd) self.assertEquals("context_name", pd.get("context_name")) self.assertEquals("class_name", pd.get("class_name"))
def get(self, context_name, class_name): valid_params = SEARCH_PARAMS + PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, **valid_params) self.query_params.validate_required(self, valid_params) response = do_search(self.query_params) self.finalize(response)
def test_build_key_for_instance(self): url_params = dict(graph_uri="graph", class_uri="Class", instance_uri="instance") handler = MockHandler(**url_params) params = ParamDict(handler, **url_params) computed = build_instance_key(params) expected = "_@@_@@instance@@expand_uri=1&instance_uri=instance&lang=pt##instance" self.assertEqual(computed, expected)
def post(self): valid_params = PAGING_PARAMS with safe_params(valid_params): try: raw_body_params = json.loads(self.request.body) except ValueError: error_message = _("JSON malformed. Received: {0}.") raise HTTPError(400, log_message=error_message.format( self.request.body)) body_params = normalize_all_uris_recursively(raw_body_params) if '@context' in body_params: del body_params['@context'] validate_json_schema(body_params, SUGGEST_PARAM_SCHEMA) self.query_params = ParamDict(self, **valid_params) response = do_suggest(self.query_params, body_params) if self.query_params['expand_uri'] == "0": response = normalize_all_uris_recursively(response, mode=SHORTEN) self.finalize(response)
def test_do_suggest_with_data(self, mock_get_predicate_ranges, mock_get_subproperties, mock_run_analyze, mock_run_search, mock_build_items, mock_decorate): handler = MockHandler() params = { 'lang': 'pt', 'expand_uri': '0', 'do_item_count': '0', 'per_page': '10', 'page': '0' } query_params = ParamDict(handler, **params) suggest_params = { u'search': { u'target': u'http://semantica.globo.com/upper/isPartOf', u'pattern': u'Globoland', u'graphs': [u'http://semantica.globo.com/place/'], u'classes': [u'http://semantica.globo.com/place/City'], u'fields': [u'http://semantica.globo.com/upper/name', u'http://semantica.globo.com/upper/fullName'] } } computed = suggest.do_suggest(query_params, suggest_params) expected = { '@context': {'@language': 'pt'}, '_base_url': 'http://mock.test.com/', 'items': [ { '@id': u'http://semantica.globo.com/place/City/bc9e708f-35c4-4067-836d-48aaa51d746d', '@type': u'http://semantica.globo.com/place/City', 'title': u'Globoland: is the best', 'type_title': u'Cidade' } ] } self.assertEqual(computed, expected)
def test_assemble_list_json_with_class_prefix(self): handler = MockHandler(uri="http://poke.oioi/company/") params = ParamDict(handler, context_name="company", **LIST_PARAMS) item = { u'class': { u'type': u'uri', u'value': u'http://dbpedia.org/ontology/Company' }, u'label': { u'type': u'literal', u'value': u'Company' } } query_result_dict = {'results': {'bindings': [item]}} computed = get_context.assemble_list_json(params, query_result_dict) expected_context = {'@language': settings.DEFAULT_LANG} expected_items = [{ '@id': u'http://dbpedia.org/ontology/Company', 'resource_id': u'Company', 'title': u'Company', 'class_prefix': u'http://dbpedia.org/ontology/' }] self.assertEqual(computed['@context'], expected_context) self.assertEqual(computed['items'], expected_items) self.assertEqual("http://poke.oioi/company", computed['_base_url'])
def test_without_item_count(self): base_url = "http://api.semantica.dev.globoi.com" param_dict = {"do_item_count": "0", "per_page": "30", "page": "0"} handler = MockHandler(uri=base_url) params = ParamDict(handler, **param_dict) computed = list_all_contexts(params) self.assertTrue("item_count" not in computed)
def test_no_context_found(self, mock1): param_dict = {"per_page": "3", "page": "1"} base_url = "http://api.semantica.dev.globoi.com" handler = MockHandler(uri=base_url) params = ParamDict(handler, **param_dict) result = list_all_contexts(params) self.assertEqual(result["items"], [])
def test_unspecified_context_with_class_uri_must_set_class_name_and_graph_uri( self): handler = MockHandler( querystring="class_uri=http://dbpedia.org/ontology/News") response = ParamDict(handler, context_name="_", class_uri="News") self.assertEqual(response['graph_uri'], 'http://dbpedia.org/ontology/') self.assertEqual(response['class_name'], 'News')
def test_query_with_prev_and_next_args_with_sort_by_and_lang(self, mock_get_schema): handler = MockHandler(querystring="page=2&per_page=1&sort_by=rdfs:label&lang=en") params = ParamDict(handler, context_name="subject", class_name="Maths", **(LIST_PARAMS)) items = [] computed = build_json(items, params) self.assertQueryStringArgsEqual(computed["_previous_args"], 'per_page=1&page=1&sort_by=rdfs%3Alabel&lang=en') self.assertQueryStringArgsEqual(computed["_next_args"], 'per_page=1&page=3&sort_by=rdfs%3Alabel&lang=en')
def delete(self, context_name, class_name, instance_id): valid_params = INSTANCE_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, instance_id=instance_id, **valid_params) del context_name del class_name del instance_id deleted = delete_instance(self.query_params) if deleted: response = 204 if settings.NOTIFY_BUS: self._notify_bus(action="DELETE") cache.purge_an_instance(self.query_params['instance_uri']) else: msg = _(u"Instance ({0}) of class ({1}) in graph ({2}) was not found.") error_message = msg.format(self.query_params["instance_uri"], self.query_params["class_uri"], self.query_params["graph_uri"]) raise HTTPError(404, log_message=error_message) self.finalize(response)
def test_self_url(self): params = {'page': 1, 'per_page': 2} handler = MockHandler(uri="http://any.uri") query_params = ParamDict(handler, **params) computed = self_url(query_params) expected = 'http://any.uri' self.assertEqual(expected, computed)
def test_class_uri_sets_class_prefix_overriding_slug(self): handler = MockHandler() params = ParamDict(handler, context_name="glb", class_uri="base:Conteudo") self.assertEquals(u'http://semantica.globo.com/base/', params.get("class_prefix"))
def test_instance_uri_and_prefix(self): handler = MockHandler() params = ParamDict(handler, context_name='dbpedia', class_name='klass', instance_id='inst', instance_uri="http://this/should/be/used", instance_prefix="http://this/is/less/important/than/instance_uri") self.assertEquals(params["instance_uri"], "http://this/should/be/used")
def test_defaults_with_basic_params(self): handler = MockHandler() params = ParamDict(handler, context_name='ctx_name', class_name='klass') self.assertEquals('ctx_name', params.get("context_name")) self.assertEquals('klass', params.get("class_name"))
def get(self): valid_params = LIST_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, **valid_params) response = list_prefixes() self.finalize(response)
def test_crud_links_with_params_ok(self): params = { 'instance_id': 'instance', 'context_name': 'context', 'class_name': 'Class' } handler = MockHandler(uri="http://any.uri/context/Class/instance", querystring="lang=en", **params) query_params = ParamDict(handler, **params) class_url = 'http://any.uri/context/Class' computed = crud_links(query_params, class_url) expected = [{ 'href': 'http://any.uri/context/Class/{_resource_id}?instance_prefix={_instance_prefix}&lang=en', 'method': 'DELETE', 'rel': 'delete' }, { 'href': 'http://any.uri/context/Class/{_resource_id}?instance_prefix={_instance_prefix}&lang=en', 'method': 'PUT', 'rel': 'update', 'schema': { '$ref': 'http://any.uri/context/Class/_schema' } }] self.assertEqual(sorted(computed), sorted(expected))
def get(self, context_name, class_name, instance_id): optional_params = INSTANCE_PARAMS with safe_params(optional_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, instance_id=instance_id, **optional_params) response = memoize(self.query_params, get_instance, key=build_instance_key(self.query_params), function_arguments=self.query_params) if response is None: error_message = u"Instance ({0}) of class ({1}) in graph ({2}) was not found.".format( self.query_params['instance_uri'], self.query_params['class_uri'], self.query_params['graph_uri']) raise HTTPError(404, log_message=error_message) response_meta = response['meta'] response = response['body'] if self.query_params["expand_uri"] == "0": response = normalize_all_uris_recursively(response, mode=SHORTEN) self.add_cache_headers(response_meta) self.finalize(response)
def test_instance_prefix_is_created_from_instance_id(self): handler = MockHandler() pd = { 'class_uri': None, 'instance_prefix': None, 'class_name': u'Activity', 'class_prefix': None, 'instance_id': u'AdministrationAndSupportService', 'context_name': u'organization', 'graph_uri': None, 'instance_uri': None } response = ParamDict(handler, **pd) self.assertEqual(response['class_uri'], 'http://semantica.globo.com/organization/Activity') self.assertEqual(response['instance_prefix'], 'http://semantica.globo.com/organization/') self.assertEqual(response['class_prefix'], 'http://semantica.globo.com/organization/') self.assertEqual(response['instance_id'], u'AdministrationAndSupportService') self.assertEqual( response['instance_uri'], 'http://semantica.globo.com/organization/Activity/AdministrationAndSupportService' )
def test_class_uri_from_context_and_class_with_class_prefix(self): handler = MockHandler(querystring="class_prefix=http://someprefix/") params = ParamDict(handler, context_name='dbpedia', class_name='Actor', class_prefix=None) self.assertEquals(params["class_prefix"], "http://someprefix/") self.assertEquals(params["class_uri"], "http://someprefix/Actor")
def test_override(self): handler = MockHandler( querystring="context_name=dbpedia&class_name=overriden_class_name") params = ParamDict(handler, context_name='dbpedia', class_name="default_class_name", class_prefix=None) self.assertEquals("overriden_class_name", params.get("class_name"))
def get(self): valid_params = PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, **valid_params) response = get_stored_queries(self.query_params) self.write(response)
def get(self): valid_params = PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, **valid_params) response = get_stored_queries(self.query_params) self.finalize_with_cache(response, 120)
def test_class_uri_from_context_and_class(self): handler = MockHandler() params = ParamDict(handler, context_name="dbpedia", class_name="Actor", class_uri=None) self.assertEquals(params["class_uri"], "http://dbpedia.org/ontology/Actor")
def get(self, context_name, class_name): query_params = ParamDict(self, context_name=context_name, class_name=class_name) try: self.finalize(collection_schema(query_params)) except schema_resource.SchemaNotFound as e: raise HTTPError(404, log_message=e.message)
def test_class_prefix(self): handler = MockHandler() params = ParamDict(handler, context_name='dbpedia', class_name='klass', class_prefix="http://this/should/be/used/") self.assertEquals(params["class_uri"], "http://this/should/be/used/klass")
class SearchHandler(BrainiakRequestHandler): @greenlet_asynchronous def get(self, context_name, class_name): valid_params = SEARCH_PARAMS + PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, **valid_params) self.query_params.validate_required(self, valid_params) response = do_search(self.query_params) self.finalize(response) def finalize(self, response): self.write(response) self.set_header("Content-Type", content_type_profile(build_schema_url(self.query_params)))
def purge(self): if settings.ENABLE_CACHE: valid_params = PAGING_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, **valid_params) recursive = int(self.request.headers.get('X-Cache-Recursive', '0')) cache.purge_root(recursive) else: raise HTTPError(405, log_message=_("Cache is disabled (Brainaik's settings.ENABLE_CACHE is set to False)"))
def test_class_name_from_class_uri(self): handler = MockHandler() pd = ParamDict( handler, class_uri='http://somedomain/someGraphName/someClassName') self.assertEquals(pd["class_uri"], "http://somedomain/someGraphName/someClassName") self.assertEquals(pd["class_name"], "someClassName") self.assertEquals(pd["class_prefix"], "http://somedomain/someGraphName/")
def test_ctx_class_inst_id(self): handler = MockHandler() params = ParamDict(handler, context_name='person', class_name='Gender', instance_id="Male", **INSTANCE_PARAMS) self.assertEquals(params["instance_uri"], "http://semantica.globo.com/person/Gender/Male")
def test_to_string_witn_non_default_params(self): url_params = dict(page="1", sort_by="rdfs:label", per_page="10") handler = MockHandler(**url_params) pd = ParamDict(handler, **url_params) self.assertEquals(pd.to_string(), "expand_uri=0&lang=pt&page=1&per_page=10&sort_by=rdfs:label")
def test_to_string_with_just_default_params(self): url_params = {} handler = MockHandler(**url_params) pd = ParamDict(handler, **url_params) self.assertEquals(pd.to_string(), "expand_uri=0&lang=pt")