From f64bcf4be4c4ca19e1969055b64f92677aa2c40b Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 28 Oct 2024 13:35:41 +0100 Subject: [PATCH] More unit tests for the multilingual parse functions --- .../tests/profiles/base/test_base_profile.py | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) diff --git a/ckanext/dcat/tests/profiles/base/test_base_profile.py b/ckanext/dcat/tests/profiles/base/test_base_profile.py index 9b341efc..1b47faf3 100644 --- a/ckanext/dcat/tests/profiles/base/test_base_profile.py +++ b/ckanext/dcat/tests/profiles/base/test_base_profile.py @@ -247,6 +247,226 @@ def test_object_list_not_found(self): assert isinstance(value, list) assert value == [] + def test_object_value_multilingual(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dct:description "Una descripció qualsevol"@ca, + "Some description"@en, + "Una descripción cualquiera"@es ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + description = p._object_value_multilingual( + URIRef("https://example.org/dataset/"), DCT.description + ) + assert description["en"] == "Some description" + assert description["ca"] == "Una descripció qualsevol" + assert description["es"] == "Una descripción cualquiera" + + def test_object_value_multilingual_missing_lang(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dct:description "Una descripció qualsevol"@ca, + "Some description"@en; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + p._form_languages = ["en", "ca", "es"] + + description = p._object_value_multilingual( + URIRef("https://example.org/dataset/"), DCT.description + ) + assert description["en"] == "Some description" + assert description["ca"] == "Una descripció qualsevol" + assert description["es"] == "" + + def test_object_value_multilingual_default_lang(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dct:description "Some description"; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + description = p._object_value_multilingual( + URIRef("https://example.org/dataset/"), DCT.description + ) + assert description["en"] == "Some description" + + @pytest.mark.ckan_config("ckan.locale_default", "ca") + def test_object_value_multilingual_default_lang_config(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dct:description "Some description"; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + description = p._object_value_multilingual( + URIRef("https://example.org/dataset/"), DCT.description + ) + assert description["ca"] == "Some description" + + def test_object_value_multilingual_rdfs_label(self): + + data = """ + @prefix dcat: . + @prefix dct: . + @prefix rdfs: . + + a dcat:Dataset ; + dct:provenance [ a dct:ProvenanceStatement ; + rdfs:label "Una declaració sobre la procedència"@ca, + "Statement about provenance"@en, + "Una declaración sobre la procedencia"@es ] ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + provenance = p._object_value_multilingual( + URIRef("https://example.org/dataset/"), DCT.provenance + ) + assert provenance["en"] == "Statement about provenance" + assert provenance["ca"] == "Una declaració sobre la procedència" + assert provenance["es"] == "Una declaración sobre la procedencia" + + def test_object_value_multilingual_list(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dcat:keyword "Pins"@ca, + "Roures"@ca, + "Oaks"@en, + "Pines"@en, + "Pinos"@es, + "Robles"@es ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + keywords = p._object_value_list_multilingual( + URIRef("https://example.org/dataset/"), DCAT.keyword + ) + assert sorted(keywords["en"]) == sorted(["Oaks", "Pines"]) + assert sorted(keywords["ca"]) == sorted(["Roures", "Pins"]) + assert sorted(keywords["es"]) == sorted(["Robles", "Pinos"]) + + def test_object_value_multilingual_list_missing_lang(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dcat:keyword "Pins"@ca, + "Roures"@ca, + "Oaks"@en, + "Pines"@en ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + p._form_languages = ["en", "ca", "es"] + + keywords = p._object_value_list_multilingual( + URIRef("https://example.org/dataset/"), DCAT.keyword + ) + assert keywords["es"] == [] + + def test_object_value_multilingual_list_default_lang(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dcat:keyword "Oaks", + "Pines" ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + keywords = p._object_value_list_multilingual( + URIRef("https://example.org/dataset/"), DCAT.keyword + ) + assert sorted(keywords["en"]) == sorted(["Oaks", "Pines"]) + + @pytest.mark.ckan_config("ckan.locale_default", "ca") + def test_object_value_multilingual_list_default_lang_conf(self): + + data = """ + @prefix dcat: . + @prefix dct: . + + a dcat:Dataset ; + dcat:keyword "Oaks", + "Pines" ; + . + """ + g = Graph() + + g.parse(format='ttl', data=data) + + p = RDFProfile(g) + + keywords = p._object_value_list_multilingual( + URIRef("https://example.org/dataset/"), DCAT.keyword + ) + assert sorted(keywords["ca"]) == sorted(["Oaks", "Pines"]) + def test_time_interval_schema_org(self): data = '''