Coverage for apis_core/utils/test_rdf.py: 100%

62 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-10-10 13:36 +0000

1# SPDX-FileCopyrightText: 2023 Birger Schacht 

2# SPDX-License-Identifier: MIT 

3 

4from pathlib import Path 

5 

6from django.test import TestCase 

7 

8from apis_core.utils import rdf 

9 

10# use `curl -H "Accept: application/rdf+xml" -L $URI` to fetch data 

11 

12testdata = Path(__file__).parent / "testdata" 

13rdf_configs = Path(__file__).parent.parent / "apis_entities/triple_configs" 

14 

15 

16class RdfTest(TestCase): 

17 def test_get_definition_from_dict_place_from_geonames(self): 

18 achensee = { 

19 "latitude": ["47.5"], 

20 "longitude": ["11.7"], 

21 "label": ["Achensee"], 

22 } 

23 # https://www.geonames.org/2783029/achensee.html 

24 uri = str(testdata / "achensee.rdf") 

25 config = rdf_configs / "E53_PlaceFromGeonames.toml" 

26 

27 attributes = rdf.load_uri_using_path(uri, config) 

28 self.assertEqual(achensee["latitude"], attributes["latitude"]) 

29 self.assertEqual(achensee["longitude"], attributes["longitude"]) 

30 self.assertEqual(achensee["label"], attributes["label"]) 

31 

32 def test_get_definition_from_dict_place_from_dnb(self): 

33 wien = { 

34 "label": ["Wien"], 

35 "latitude": ["+048.208199"], 

36 "longitude": ["016.371690"], 

37 } 

38 # https://d-nb.info/gnd/4066009-6 

39 uri = str(testdata / "wien.rdf") 

40 config = rdf_configs / "E53_PlaceFromDNB.toml" 

41 

42 attributes = rdf.load_uri_using_path(uri, config) 

43 self.assertEqual(wien["latitude"], attributes["latitude"]) 

44 self.assertEqual(wien["longitude"], attributes["longitude"]) 

45 self.assertEqual(wien["label"], attributes["label"]) 

46 

47 def test_get_definition_from_dict_person_from_dnb(self): 

48 pierre = { 

49 "forename": [ 

50 "Pierre", 

51 "Pʹer", 

52 "Rudolf", 

53 "Rodolphe", 

54 "...", 

55 "Ramus, Pierre", 

56 ], 

57 "surname": ["Ramus", "Großmann", "Grossmann", "Grossman", "Libertarian"], 

58 "alternative_names": [ 

59 "Ramus, Pʹer", 

60 "Großmann, Rudolf", 

61 "Grossmann, Rudolf", 

62 "Grossman, Rudolf", 

63 "Grossman, Rodolphe", 

64 "Grossmann, Rodolphe", 

65 "Libertarian, ...", 

66 ], 

67 "date_of_birth": ["1882-04-15"], 

68 "date_of_death": ["1942"], 

69 } 

70 # https://d-nb.info/gnd/118833197 

71 uri = str(testdata / "ramus.rdf") 

72 config = rdf_configs / "E21_PersonFromDNB.toml" 

73 

74 attributes = rdf.load_uri_using_path(uri, config) 

75 self.assertEqual(pierre["forename"], attributes["forename"]) 

76 self.assertEqual(pierre["surname"], attributes["surname"]) 

77 # self.assertEqual(pierre["alternative_names"], attributes["alternative_names"]) 

78 self.assertEqual(pierre["date_of_birth"], attributes["date_of_birth"]) 

79 self.assertEqual(pierre["date_of_death"], attributes["date_of_death"]) 

80 

81 def test_get_definition_from_dict_institution_from_dnb(self): 

82 pierre_ges = { 

83 "label": [ 

84 "Pierre-Ramus-Gesellschaft", 

85 "Ramus-Gesellschaft", 

86 "Pierre Ramus-Gesellschaft", 

87 ], 

88 } 

89 # https://d-nb.info/gnd/415006-5 

90 uri = str(testdata / "ramus_gesellschaft.rdf") 

91 config = rdf_configs / "E74_GroupFromDNB.toml" 

92 

93 attributes = rdf.load_uri_using_path(uri, config) 

94 self.assertEqual(pierre_ges["label"], attributes["label"]) 

95 

96 def test_get_definition_from_dict_institution_from_dnb2(self): 

97 oeaw = { 

98 "label": [ 

99 "Akademie der Wissenschaften in Wien", 

100 "Academy of Sciences in Vienna", 

101 "Akademie der Wissenschaften (Wien)", 

102 ], 

103 } 

104 # https://d-nb.info/gnd/35077-1 

105 uri = str(testdata / "oeaw.rdf") 

106 config = rdf_configs / "E74_GroupFromDNB.toml" 

107 

108 attributes = rdf.load_uri_using_path(uri, config) 

109 self.assertEqual(oeaw["label"], attributes["label"]) 

110 

111 def test_dnb_geographic_unit(self): 

112 expected = {"label": "Wutai Shan"} 

113 # https://d-nb.info/gnd/4241848-3 

114 uri = str(testdata / "wutaishan.rdf") 

115 config = rdf_configs / "E53_PlaceFromDNB.toml" 

116 

117 attributes = rdf.load_uri_using_path(uri, config) 

118 self.assertEqual(len(attributes.get("latitude", [])), 0) 

119 self.assertEqual(len(attributes.get("longitude", [])), 0) 

120 

121 self.assertIn(expected["label"], attributes["label"]) 

122 

123 def test_wikidata_geographic_unit(self): 

124 # https://www.wikidata.org/wiki/Q82601 

125 uri = str(testdata / "tierra_del_fugo.rdf") 

126 config = rdf_configs / "E53_PlaceFromWikidata.toml" 

127 

128 attributes = rdf.load_uri_using_path(uri, config) 

129 self.assertEqual(["-70"], attributes["longitude"]) 

130 self.assertEqual(["-54"], attributes["latitude"]) 

131 

132 def test_empty_rdf_file(self): 

133 uri = str(testdata / "empty.rdf") 

134 config = rdf_configs / "E53_PlaceFromWikidata.toml" 

135 

136 attributes = rdf.load_uri_using_path(uri, config) 

137 self.assertEqual(attributes, None)