def assemble_list_json(query_params, query_result_dict): context = MemorizeContext() expand_uri = bool(int(query_params.get('expand_uri', '0'))) items_list = compress_keys_and_values( query_result_dict, keymap={"class": "@id", "label": "title"}, context=context, expand_uri=expand_uri) items_list = compress_duplicated_ids(items_list) decorate_with_resource_id(items_list) decorate_with_class_prefix(items_list) context_section = context.context context_section.update({"@language": query_params.get("lang")}) json_dict = { '_base_url': remove_last_slash(query_params.base_url), 'items': items_list, '@context': context_section, '@id': query_params['graph_uri'] } def calculate_total_items(): count_query_result_dict = query_count_classes(query_params) total_items = int(get_one_value(count_query_result_dict, "total_items")) return total_items decorate_dict_with_pagination(json_dict, query_params, calculate_total_items) return json_dict
class MemorizeContextTestCase(unittest.TestCase): def setUp(self): self.context = MemorizeContext() def test_unique_object_properties(self): self.context.add_object_property("ctx:field_name", "upper:Entity") self.assertIn('ctx:field_name', self.context.object_properties) def test_object_properties_with_URL(self): self.context.add_object_property( "http://www.bbc.co.uk/ontologies/asset/Asset", "http://www.bbc.co.uk/ontologies/asset/primaryAsset") self.assertIn('http://www.bbc.co.uk/ontologies/asset/Asset', self.context.object_properties) self.assertEqual( self.context. object_properties['http://www.bbc.co.uk/ontologies/asset/Asset'], "http://www.bbc.co.uk/ontologies/asset/primaryAsset") def test_prefix_to_slug(self): self.assertEqual( self.context.prefix_to_slug( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'), 'rdf') self.assertIn('rdf', self.context.context) self.assertEqual(self.context.context['rdf'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') def test_shorten_uri(self): self.assertEqual( self.context.shorten_uri( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), 'rdf:type') self.assertIn('rdf', self.context.context) self.assertEqual(self.context.context['rdf'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
def get_schema(query_params): context = MemorizeContext(normalize_uri=query_params['expand_uri']) class_schema = query_class_schema(query_params) if not class_schema["results"]["bindings"]: return superclasses = query_superclasses(query_params) predicates_and_cardinalities = get_predicates_and_cardinalities( context, query_params, superclasses) response_dict = assemble_schema_dict(query_params, get_one_value(class_schema, "title"), predicates_and_cardinalities, context, comment=get_one_value( class_schema, "comment")) return response_dict
def test_normalize_prefix_invalid_mode(self): context = MemorizeContext(normalize_uri='INVALID_MODE') self.assertRaises(InvalidModeForNormalizeUriError, context.normalize_prefix_value, "rdf:type")
def test_normalize_prefix_to_expand_inversed(self): prefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" context = MemorizeContext(normalize_uri=EXPAND) normalized = context.normalize_prefix_value(prefix) self.assertEqual(normalized, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
def test_normalize_prefix_to_shorten_inversed(self): prefix = "rdf" context = MemorizeContext(normalize_uri=SHORTEN) normalized = context.normalize_prefix_value(prefix) self.assertEqual(normalized, 'rdf')
def test_normalize_prefix_to_shorten(self): prefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" context = MemorizeContext(normalize_uri=SHORTEN) normalized = context.normalize_prefix_value(prefix) self.assertEqual(normalized, 'rdf')
def test_shorten_uri(self): context = MemorizeContext(normalize_uri=SHORTEN) self.assertEqual( context.normalize_uri( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), "rdf:type")
def test_expand_uri(self): context = MemorizeContext(normalize_uri=EXPAND) self.assertEqual(context.normalize_uri("rdf:type"), "http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
def setUp(self): self.context = MemorizeContext()