def test_get_region_specific_preflabel_when_language_only_version_does_not_exist(self): """ Given a language only and the system only has values that with regions specified, then the system should pick the first preflabel with the same language code """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en-US', 'language': 'en-US' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual(concept.get_preflabel(lang='en').value, 'test pref label en-US')
def test_get_label_no_exact_match(self): """ Given no match on language code or region, return the first prefLabel found """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'value': 'bier', 'language': 'nl' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'beer', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] pl = concept.get_preflabel('fr-BE') self.assertEquals(pl.type,'prefLabel') self.assertEquals(pl.value,'bier' or 'beer') self.assertEquals(pl.language,'nl' or 'es-SP')
def test_get_altlabel_when_no_preflabel_exists_and_altlabel_only_specifies_lang_code( self): """ Given a language and region and the system only has values that specify a language code, the the system should pick the altlabel even if the altlabel doesn't specifiy a region """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en', 'language': 'en' }) ] self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test alt label en')
def test_prefer_preflabel_with_just_lang_code_match_over_exact_match_with_altlabel(self): """ Given a language and region, test should pick the preflabel even if that preflabel specifies a language only and even if an altlabel exists with the exact match """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en', 'language': 'en' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test pref label en')
def concept_value(request): if request.method == 'DELETE': data = JSONDeserializer().deserialize(request.body) if data: with transaction.atomic(): value = ConceptValue(data) value.delete_index() value.delete() return JSONResponse(value) return HttpResponseNotFound
def test_get_altlabel_when_no_preflabel_exists(self): """ Given a language and region, test should pick the altlabel when no preflabel exists """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label es", "language": "es-SP"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en-US", "language": "en-US"}), ] self.assertEqual(concept.get_preflabel(lang="en-US").value, "test alt label en-US")
def test_get_altlabel_when_no_preflabel_exists_and_altlabel_only_specifies_lang_code(self): """ Given a language and region and the system only has values that specify a language code, the the system should pick the altlabel even if the altlabel doesn't specifiy a region """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label es", "language": "es"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en", "language": "en"}), ] self.assertEqual(concept.get_preflabel(lang="en-US").value, "test alt label en")
def import_concepts(reference_data): concepts = reference_data[0]['concepts'] values = reference_data[1]['values'] relations = reference_data[2]['relations'] concept_objs = {} for concept in concepts: concept_obj = Concept() concept_obj.id = concept['conceptid'] concept_obj.nodetype = concept['nodetype'] concept_obj.legacyoid = concept['legacyoid'] concept_obj.save() concept_objs[concept_obj.id] = concept_obj existing_valuetypes = [o.valuetype for o in models.DValueType.objects.all()] for value in values: if value['valuetype'] not in existing_valuetypes: models.DValueType.objects.create(valuetype = value['valuetype'], category = 'undefined', namespace = 'arches') existing_valuetypes.append(value['valuetype']) conceptvalue_obj = ConceptValue() conceptvalue_obj.id = value['valueid'] conceptvalue_obj.conceptid = value['conceptid'] conceptvalue_obj.type = value['valuetype'] conceptvalue_obj.value = value['value'] conceptvalue_obj.language = value['languageid'] conceptvalue_obj.save() for relation in relations: if relation['conceptidfrom'] in concept_objs and relation['conceptidto'] in concept_objs: conceptfrom = concept_objs[relation['conceptidfrom']] conceptto = concept_objs[relation['conceptidto']] conceptfrom.add_relation(conceptto, relation['relationtype'])
def test_get_region_specific_preflabel_when_language_only_version_does_not_exist(self): """ Given a language only and the system only has values that with regions specified, then the system should pick the first preflabel with the same language code """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label en-US", "language": "en-US"}), ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label es", "language": "es-SP"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en-US", "language": "en-US"}), ] self.assertEqual(concept.get_preflabel(lang="en").value, "test pref label en-US")
def test_prefer_preflabel_with_just_lang_code_match_over_exact_match_with_altlabel(self): """ Given a language and region, test should pick the preflabel even if that preflabel specifies a language only and even if an altlabel exists with the exact match """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label en", "language": "en"}), ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label es", "language": "es-SP"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en-US", "language": "en-US"}), ] self.assertEqual(concept.get_preflabel(lang="en-US").value, "test pref label en")
def test_create_concept(self): """ Test of basic CRUD on a Concept model """ concept_in = Concept() concept_in.nodetype = 'Concept' concept_in.values = [ConceptValue({ #id: '', #conceptid: '', 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label', 'language': 'en-US' })] concept_in.save() concept_out = Concept().get(id=concept_in.id) self.assertEqual(concept_out.id, concept_in.id) self.assertEqual(concept_out.values[0].value, 'test pref label') label = concept_in.values[0] label.value = 'updated pref label' concept_in.values[0] = label concept_in.save() concept_out = Concept().get(id=concept_in.id) self.assertEqual(concept_out.values[0].value, 'updated pref label') concept_out.delete(delete_self=True) with self.assertRaises(models.Concept.DoesNotExist): deleted_concept = Concept().get(id=concept_out.id)
def concept_value(request): if request.method == 'DELETE': data = JSONDeserializer().deserialize(request.body) if data: with transaction.atomic(): value = ConceptValue(data) value.delete_index() value.delete() return JSONResponse(value) if request.method == 'GET': valueid = request.GET.get('valueid') value = models.Value.objects.get(pk=valueid) return JSONResponse(value) return HttpResponseNotFound
def test_prefer_preflabel_over_altlabel(self): """ Test to confirm the proper retrieval of the prefLabel based on different language requirements """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label en-US", "language": "en-US"}), ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label en", "language": "en"}), ConceptValue({"type": "prefLabel", "category": "label", "value": "test pref label es-SP", "language": "es-SP"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en-US", "language": "en-US"}), ] self.assertEqual(concept.get_preflabel(lang="en-US").value, "test pref label en-US") self.assertEqual(concept.get_preflabel(lang="en").value, "test pref label en") self.assertEqual(concept.get_preflabel().value, "test pref label %s" % (test_settings.LANGUAGE_CODE))
def test_get_label_no_exact_match(self): """ Given no match on language code or region, return the first prefLabel found """ concept = Concept() concept.values = [ ConceptValue({"type": "prefLabel", "value": "bier", "language": "nl"}), ConceptValue({"type": "prefLabel", "category": "label", "value": "beer", "language": "es-SP"}), ConceptValue({"type": "altLabel", "category": "label", "value": "test alt label en-US", "language": "en-US"}), ] pl = concept.get_preflabel("fr-BE") self.assertEqual(pl.type, "prefLabel") self.assertEqual(pl.value, "bier" or "beer") self.assertEqual(pl.language, "nl" or "es-SP")
def to_rdf(self, edge_info, edge): g = Graph() myuri = self.get_rdf_uri(None, edge_info["range_tile_data"]) if edge_info["r_uri"] == myuri: c = ConceptValue(str(edge_info["range_tile_data"])) g.add((edge_info["r_uri"], RDF.type, URIRef(edge.rangenode.ontologyclass))) g.add((edge_info["d_uri"], URIRef(edge.ontologyproperty), edge_info["r_uri"])) g.add((edge_info["r_uri"], URIRef(RDFS.label), Literal(c.value))) return g
def test_prefer_preflabel_over_altlabel(self): """ Test to confirm the proper retrieval of the prefLabel based on different language requirements """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en-US', 'language': 'en-US' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en', 'language': 'en' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es-SP', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test pref label en-US') self.assertEqual( concept.get_preflabel(lang='en').value, 'test pref label en') self.assertEqual(concept.get_preflabel().value, 'test pref label %s' % (test_settings.LANGUAGE_CODE))
def get_rdf_uri(self, node, data, which="r"): if not data: return None c = ConceptValue(str(data)) assert c.value is not None, "Null or blank concept value" ext_ids = [ ident.value for ident in models.Value.objects.all().filter(concept_id__exact=c.conceptid, valuetype__category="identifiers") ] for p in settings.PREFERRED_CONCEPT_SCHEMES: for id_uri in ext_ids: if str(id_uri).startswith(p): return URIRef(id_uri) return URIRef(archesproject[f"concepts/{c.conceptid}"])
def test_get_altlabel_when_no_preflabel_exists(self): """ Given a language and region, test should pick the altlabel when no preflabel exists """ concept = Concept() concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual(concept.get_preflabel(lang='en-US').value, 'test alt label en-US')
def import_concepts(reference_data): concepts = reference_data[0]['concepts'] values = reference_data[1]['values'] relations = reference_data[2]['relations'] concept_objs = {} for concept in concepts: concept_obj = Concept() concept_obj.id = concept['conceptid'] concept_obj.nodetype = concept['nodetype'] concept_obj.legacyoid = concept['legacyoid'] concept_obj.save() concept_objs[concept_obj.id] = concept_obj existing_valuetypes = [ o.valuetype for o in models.DValueType.objects.all() ] for value in values: if value['valuetype'] not in existing_valuetypes: models.DValueType.objects.create(valuetype=value['valuetype'], category='undefined', namespace='arches') existing_valuetypes.append(value['valuetype']) conceptvalue_obj = ConceptValue() conceptvalue_obj.id = value['valueid'] conceptvalue_obj.conceptid = value['conceptid'] conceptvalue_obj.type = value['valuetype'] conceptvalue_obj.value = value['value'] conceptvalue_obj.language = value['languageid'] conceptvalue_obj.save() for relation in relations: if relation['conceptidfrom'] in concept_objs and relation[ 'conceptidto'] in concept_objs: conceptfrom = concept_objs[relation['conceptidfrom']] conceptto = concept_objs[relation['conceptidto']] conceptfrom.add_relation(conceptto, relation['relationtype'])
def to_rdf(self, edge_info, edge): g = Graph() # logic: No data -> empty graph # concept_id, but no value -> node linked to class of Concept, no label # value but no concept_id -> node linked to BNode, labelled # concept_id + value -> normal expected functionality def get_rangenode(arches_uri, ext_ids): rangenode = arches_uri for id_uri in ext_ids: if str(id_uri).startswith(settings.PREFERRED_CONCEPT_SCHEME): rangenode = id_uri return rangenode if edge_info['range_tile_data'] is not None: c = ConceptValue(str(edge_info['range_tile_data'])) # create a default node arches_uri = BNode() ext_idents = [] # Use the conceptid URI rather than the pk for the ConceptValue if c.conceptid is not None: arches_uri = URIRef(archesproject['concepts/%s' % c.conceptid]) # get other identifiers: ext_idents = [ ident.value for ident in models.Value.objects.all().filter( concept_id=c.conceptid, valuetype__category="identifiers") ] rangenode = get_rangenode(arches_uri, ext_idents) g.add((rangenode, RDF.type, URIRef(edge.rangenode.ontologyclass))) g.add( (edge_info['d_uri'], URIRef(edge.ontologyproperty), rangenode)) assert c.value is not None, "Null or blank concept value" g.add((rangenode, URIRef(RDFS.label), Literal(c.value, lang=c.language))) return g
def test_create_concept(self): """ Test of basic CRUD on a Concept model """ concept_in = Concept() concept_in.nodetype = "Concept" concept_in.values = [ ConceptValue( { # id: '', # conceptid: '', "type": "prefLabel", "category": "label", "value": "test pref label", "language": "en-US", } ) ] concept_in.save() concept_out = Concept().get(id=concept_in.id) self.assertEqual(concept_out.id, concept_in.id) self.assertEqual(concept_out.values[0].value, "test pref label") label = concept_in.values[0] label.value = "updated pref label" concept_in.values[0] = label concept_in.save() concept_out = Concept().get(id=concept_in.id) self.assertEqual(concept_out.values[0].value, "updated pref label") concept_out.delete(delete_self=True) with self.assertRaises(models.Concept.DoesNotExist): deleted_concept = Concept().get(id=concept_out.id)
def test_prefLabel(self): """ Test to confirm the proper retrieval of the prefLabel based on different language requirements """ concept = Concept() concept.nodetype = 'Concept' concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en-US', 'language': 'en-US' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en', 'language': 'en' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es-SP', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test pref label en-US') self.assertEqual( concept.get_preflabel(lang='en').value, 'test pref label en') self.assertEqual(concept.get_preflabel().value, 'test pref label %s' % (test_settings.LANGUAGE_CODE)) concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en', 'language': 'en' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] # should pick the base language if it can't find the more specific version self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test pref label en') concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test alt label en-US') concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en', 'language': 'en' }) ] self.assertEqual( concept.get_preflabel(lang='en-US').value, 'test alt label en') concept.values = [ ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label en-US', 'language': 'en-US' }), ConceptValue({ 'type': 'prefLabel', 'category': 'label', 'value': 'test pref label es', 'language': 'es-SP' }), ConceptValue({ 'type': 'altLabel', 'category': 'label', 'value': 'test alt label en-US', 'language': 'en-US' }) ] self.assertEqual( concept.get_preflabel(lang='en').value, 'test pref label en-US')