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

71 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2026-03-06 11:42 +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.apis_entities.rdfconfigs.group import ( 

9 E74_GroupFromDNB, 

10) 

11from apis_core.apis_entities.rdfconfigs.person import ( 

12 E21_PersonFromDNB, 

13) 

14from apis_core.apis_entities.rdfconfigs.place import ( 

15 E53_PlaceFromDNB, 

16 E53_PlaceFromGeonames, 

17 E53_PlaceFromWikidata, 

18) 

19from apis_core.utils import rdf 

20 

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

22 

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

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

25 

26 

27class RdfTest(TestCase): 

28 def test_get_definition_from_dict_place_from_geonames(self): 

29 achensee = { 

30 "latitude": ["47.5"], 

31 "longitude": ["11.7"], 

32 "label": ["Achensee"], 

33 } 

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

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

36 

37 attributes = rdf.load_uri_using_path(uri, E53_PlaceFromGeonames) 

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

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

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

41 

42 def test_get_definition_from_dict_place_from_dnb(self): 

43 wien = { 

44 "label": ["Wien"], 

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

46 "longitude": ["016.371690"], 

47 } 

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

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

50 

51 attributes = rdf.load_uri_using_path(uri, E53_PlaceFromDNB) 

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

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

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

55 

56 def test_get_definition_from_dict_person_from_dnb(self): 

57 pierre = { 

58 "forename": [ 

59 "Pierre", 

60 "Pʹer", 

61 "Rudolf", 

62 "Rodolphe", 

63 "...", 

64 "Ramus, Pierre", 

65 ], 

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

67 "alternative_names": [ 

68 "Ramus, Pʹer", 

69 "Großmann, Rudolf", 

70 "Grossmann, Rudolf", 

71 "Grossman, Rudolf", 

72 "Grossman, Rodolphe", 

73 "Grossmann, Rodolphe", 

74 "Libertarian, ...", 

75 ], 

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

77 "date_of_death": ["1942"], 

78 } 

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

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

81 

82 attributes = rdf.load_uri_using_path(uri, E21_PersonFromDNB) 

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

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

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

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

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

88 

89 def test_get_definition_from_dict_institution_from_dnb(self): 

90 pierre_ges = { 

91 "label": [ 

92 "Pierre-Ramus-Gesellschaft", 

93 "Ramus-Gesellschaft", 

94 "Pierre Ramus-Gesellschaft", 

95 ], 

96 } 

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

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

99 

100 attributes = rdf.load_uri_using_path(uri, E74_GroupFromDNB) 

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

102 

103 def test_get_definition_from_dict_institution_from_dnb2(self): 

104 oeaw = { 

105 "label": [ 

106 "Akademie der Wissenschaften in Wien", 

107 "Academy of Sciences in Vienna", 

108 "Akademie der Wissenschaften (Wien)", 

109 ], 

110 } 

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

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

113 

114 attributes = rdf.load_uri_using_path(uri, E74_GroupFromDNB) 

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

116 

117 def test_dnb_geographic_unit(self): 

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

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

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

121 

122 attributes = rdf.load_uri_using_path(uri, E53_PlaceFromDNB) 

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

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

125 

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

127 

128 def test_wikidata_geographic_unit(self): 

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

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

131 

132 attributes = rdf.load_uri_using_path(uri, E53_PlaceFromWikidata) 

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

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

135 

136 def test_empty_rdf_file(self): 

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

138 

139 attributes = rdf.load_uri_using_path(uri, E53_PlaceFromWikidata) 

140 self.assertEqual(attributes, None) 

141 

142 def test_dnb_MusicalCorporateBody(self): 

143 # https://d-nb.info/gnd/2098220-3 

144 uri = str(testdata / "groups/Salzburger_Marionettentheater.ttl") 

145 

146 attributes = rdf.load_uri_using_path(uri, E74_GroupFromDNB) 

147 label = [ 

148 "Salzburger Marionettentheater", 

149 "Salzburger Marionettentheater Gesellschaft m.b.H.", 

150 "Salzburger Marionettentheater Ges.m.b.H.", 

151 "Salzburger Marionettentheater GmbH", 

152 "Marionettentheater (Salzburger Marionettentheater)", 

153 "Salzburger Marionetten (Körperschaft)", 

154 "Aichlers Marionettentheater", 

155 "Marionette Theatre Salzburg", 

156 "Salzburg Marionette Theatre", 

157 "Marionettentheater Salzburg", 

158 ] 

159 sameas = [ 

160 "http://viaf.org/viaf/149602965", 

161 "https://isni.org/isni/0000000115374839", 

162 "http://www.wikidata.org/entity/Q2215994", 

163 "http://id.loc.gov/rwo/agents/n86090792", 

164 "https://d-nb.info/gnd/4198684-2", 

165 ] 

166 self.assertEqual(attributes["label"], label) 

167 self.assertEqual(attributes["sameas"], sameas) 

168 

169 def test_dnb_OrganOfCorporateBody(self): 

170 # https://d-nb.info/gnd/4257718-4 

171 uri = str(testdata / "groups/Landgericht_Meran.ttl") 

172 

173 attributes = rdf.load_uri_using_path(uri, E74_GroupFromDNB) 

174 label = ["Meran. Landgericht Meran"] 

175 sameas = ["http://viaf.org/viaf/248639199"] 

176 self.assertEqual(attributes["label"], label) 

177 self.assertEqual(attributes["sameas"], sameas)