1 from nose.tools import assert_equals
2 from rdflib import (ConjunctiveGraph as Graph, URIRef, Literal, BNode,
3 Namespace, RDF)
4 from oort.rdfview import (RdfQuery, one, each, one_where_self_is,
5 each_where_self_is, collection, localized, i18n_dict, each_localized,
6 localized_xml, Sorter, Filter, run_queries, THIS_QUERY, selector)
7 from oort.util import queries
8
9
10 from oort.test.helper import siblingpath
11
13 testfilepath = siblingpath(__file__, filename)
14 graph = Graph()
15 graph.load(open(testfilepath), format='n3')
16 return graph
17
18 testgraph = _create_test_graph()
19
20
21
23 assert set(unicode(v) for v in results) == set(expected)
24
28
29
30
31
32
33
34 T = Namespace("http://example.org/oort/test#")
35
36 itemX = URIRef('tag:oort.to,2006:test:item:x')
37 en, sv = 'en', 'sv'
38
39
40 -class Part(RdfQuery):
42
52
53
55
58
60 assert self.item.name == u'Item X'
61
64
66 relItemNames = [item.name for item in self.item.relations]
67 assert set(relItemNames) == set(["Related 1", "Related 2"])
68
70 collItems = [item.name for item in self.item.partlist]
71 assert set(collItems) == set(["Part 1", "Part 2"])
72
74 collItems = [item.name for item in self.item.multiple_partlist]
75 assert set(collItems) == set(["Part 1", "Part 2"])
76
78 titleLangs = self.item.titleLang
79 assert set(titleLangs.keys()) == set(['en', 'sv'])
80 assert_all_equals(titleLangs.values(), [u'Example Item', u'Exempelsak'])
81
82
86
90
91
94
96 for l, v in [(en, u'XML Data'), (sv, u'XML-data')]:
97 treeData = list(RichItem(testgraph, l, itemX).xmlData)
98 ok = False
99 for stream in treeData:
100 for item in stream:
101 if v in item: ok = True
102 assert ok
103
104
107
112
113
116
122
123
127
128
129
132
133
134
143
146
147
160
170 yield assert_sorted, item
171
172 def assert_sortedrels(rels):
173 assert ['Related 2', 'Related 1'] == [it.name for it in rels]
174 yield assert_sortedrels, item.sortedRelations_attr
175 yield assert_sortedrels, item.sortedRelations_func
176
177 def assert_some(item):
178 assert set(item.someKeywords) == set(['q', 'r', 'y'])
179 yield assert_some, item
180
181
183
184 class ItemWithSelectorAndFilter(Item):
185 @selector.filtered_by(Sorter('name'), lambda l: l[0])
186 def firstRelation(self, graph, lang, subject):
187 return self.relations
188
189 item = ItemWithSelectorAndFilter(testgraph, en, itemX)
190 assert item.firstRelation.name == 'Related 1'
191
192
196
201
207
215
216
218 data = dict(
219 name = "Item X",
220 title = "Example Item",
221 titleLang = { 'en': "Example Item", 'sv': "Exempelsak" },
222 labels = [ "En", "eN" ],
223 relations = [
224 {'name': "Related 1"},
225 {'name': "Related 2"},
226 ],
227 xmlData = "<div/>",
228 )
229 item = Item.from_dict(data, "en", URIRef("tag:oort.to,2006/test/item/x_fromdict"))
230
231 item.to_graph()
232
233
234
236 bound = Item.bound_with(itemX)
237 yield assert_equals, bound.query, Item
238 yield assert_equals, bound.__name__, 'Item'
239 yield assert_equals, bound(testgraph, en, BNode()).name, u'Item X'
240 bound = Item.bound_with(itemX, sv)
241 yield assert_equals, bound(testgraph, None, BNode()).title, Literal(u'Exempelsak', sv)
242
243
245 found = list(Item.find_by(testgraph, en, name=Literal(u'Item X')))
246 yield assert_equals, found[0].uri, itemX
247 for l, v in (en, u'Example Item'), (sv, u'Exempelsak'):
248 found = Item.find_by(testgraph, l, title=Literal(v, l))
249 yield assert_equals, list(found)[0].uri, itemX
250
251
252
253
259
263
281
282
283
284
285
286
288 queries = [Item, CustomItem]
289 for item, query in zip(run_queries(queries, testgraph, en, itemX), queries):
290 assert isinstance(item, query)
291 test = TestItem()
292 test.item = item
293 yield test.test_one ,
294 yield test.test_one_relation ,
295 yield test.test_each ,
296 yield test.test_collection ,
297 yield test.test_collection_multiple ,
298 yield test.test_i18n_dict ,
299