|
1
|
|
|
import unittest |
|
2
|
|
|
from pyramid import testing |
|
3
|
|
|
|
|
4
|
|
|
from pyramid.httpexceptions import HTTPMethodNotAllowed |
|
5
|
|
|
from skosprovider.providers import DictionaryProvider |
|
6
|
|
|
from skosprovider_sqlalchemy.models import ( |
|
7
|
|
|
Concept, Collection, |
|
8
|
|
|
Note, Label, Source, |
|
9
|
|
|
ConceptScheme, |
|
10
|
|
|
Match, MatchType |
|
11
|
|
|
) |
|
12
|
|
|
from skosprovider_sqlalchemy.providers import SQLAlchemyProvider |
|
13
|
|
|
from skosprovider.skos import( |
|
14
|
|
|
Concept as SkosConcept, |
|
15
|
|
|
Collection as SkosCollection |
|
16
|
|
|
) |
|
17
|
|
|
from atramhasis.utils import from_thing, internal_providers_only, update_last_visited_concepts |
|
18
|
|
|
|
|
19
|
|
|
species = { |
|
20
|
|
|
'id': 3, |
|
21
|
|
|
'uri': 'http://id.trees.org/3', |
|
22
|
|
|
'labels': [ |
|
23
|
|
|
{'type': 'prefLabel', 'language': 'en', 'label': 'Trees by species'}, |
|
24
|
|
|
{'type': 'prefLabel', 'language': 'nl', 'label': 'Bomen per soort'} |
|
25
|
|
|
], |
|
26
|
|
|
'type': 'collection', |
|
27
|
|
|
'members': ['1', '2'], |
|
28
|
|
|
'notes': [ |
|
29
|
|
|
{ |
|
30
|
|
|
'type': 'editorialNote', |
|
31
|
|
|
'language': 'en', |
|
32
|
|
|
'note': 'As seen in How to Recognise Different Types of Trees from Quite a Long Way Away.' |
|
33
|
|
|
} |
|
34
|
|
|
] |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
class TestFromThing(unittest.TestCase): |
|
39
|
|
|
def setUp(self): |
|
40
|
|
|
conceptscheme = ConceptScheme() |
|
41
|
|
|
conceptscheme.uri = 'urn:x-atramhasis-demo' |
|
42
|
|
|
conceptscheme.id = 1 |
|
43
|
|
|
self.concept = Concept() |
|
44
|
|
|
self.concept.type = 'concept' |
|
45
|
|
|
self.concept.id = 11 |
|
46
|
|
|
self.concept.concept_id = 101 |
|
47
|
|
|
self.concept.uri = 'urn:x-atramhasis-demo:TREES:101' |
|
48
|
|
|
self.concept.conceptscheme_id = 1 |
|
49
|
|
|
self.concept.conceptscheme = conceptscheme |
|
50
|
|
|
|
|
51
|
|
|
notes = [] |
|
52
|
|
|
note = Note(note='test note', notetype_id='example', language_id='en') |
|
53
|
|
|
note2 = Note(note='note def', notetype_id='definition', language_id='en') |
|
54
|
|
|
notes.append(note) |
|
55
|
|
|
notes.append(note2) |
|
56
|
|
|
self.concept.notes = notes |
|
57
|
|
|
|
|
58
|
|
|
labels = [] |
|
59
|
|
|
label = Label(label='een label', labeltype_id='prefLabel', language_id='nl') |
|
60
|
|
|
label2 = Label(label='other label', labeltype_id='altLabel', language_id='en') |
|
61
|
|
|
label3 = Label(label='and some other label', labeltype_id='altLabel', language_id='en') |
|
62
|
|
|
labels.append(label) |
|
63
|
|
|
labels.append(label2) |
|
64
|
|
|
labels.append(label3) |
|
65
|
|
|
self.concept.labels = labels |
|
66
|
|
|
|
|
67
|
|
|
sources = [] |
|
68
|
|
|
source = Source(citation='Kinsella S. & Carlisle P. 2015: Alice.') |
|
69
|
|
|
sources.append(source) |
|
70
|
|
|
self.concept.sources = sources |
|
71
|
|
|
|
|
72
|
|
|
matches = [] |
|
73
|
|
|
match1 = Match() |
|
74
|
|
|
match1.uri ='urn:test' |
|
75
|
|
|
match1.concept = self.concept |
|
76
|
|
|
match1.matchtype = MatchType(name='closeMatch', description='') |
|
77
|
|
|
match2 = Match() |
|
78
|
|
|
match2.uri ='urn:test' |
|
79
|
|
|
match2.concept = self.concept |
|
80
|
|
|
match2.matchtype = MatchType(name='closeMatch', description='') |
|
81
|
|
|
matches.append(match1) |
|
82
|
|
|
matches.append(match2) |
|
83
|
|
|
self.matches = matches |
|
84
|
|
|
|
|
85
|
|
|
self.collection = Collection() |
|
86
|
|
|
self.collection.type = 'collection' |
|
87
|
|
|
self.collection.id = 12 |
|
88
|
|
|
self.collection.concept_id = 102 |
|
89
|
|
|
self.collection.uri = 'urn:x-atramhasis-demo:TREES:102' |
|
90
|
|
|
self.collection.conceptscheme_id = 1 |
|
91
|
|
|
self.collection.conceptscheme = conceptscheme |
|
92
|
|
|
|
|
93
|
|
|
def test_thing_to_concept(self): |
|
94
|
|
|
skosconcept = from_thing(self.concept) |
|
95
|
|
|
self.assertTrue(isinstance(skosconcept, SkosConcept)) |
|
96
|
|
|
self.assertEqual(skosconcept.id, 101) |
|
97
|
|
|
self.assertEqual(len(skosconcept.labels), 3) |
|
98
|
|
|
self.assertEqual(len(skosconcept.notes), 2) |
|
99
|
|
|
self.assertEqual(len(skosconcept.sources), 1) |
|
100
|
|
|
self.assertEqual(skosconcept.uri, 'urn:x-atramhasis-demo:TREES:101') |
|
101
|
|
|
|
|
102
|
|
|
def test_thing_to_collection(self): |
|
103
|
|
|
skoscollection = from_thing(self.collection) |
|
104
|
|
|
self.assertTrue(isinstance(skoscollection, SkosCollection)) |
|
105
|
|
|
self.assertEqual(skoscollection.id, 102) |
|
106
|
|
|
self.assertEqual(skoscollection.uri, 'urn:x-atramhasis-demo:TREES:102') |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
class DummyViewClassForTest(object): |
|
110
|
|
|
|
|
111
|
|
|
def __init__(self): |
|
112
|
|
|
self.provider = None |
|
113
|
|
|
self.dummy = None |
|
114
|
|
|
|
|
115
|
|
|
def all_providers(self, dummy): |
|
116
|
|
|
self.dummy = dummy |
|
117
|
|
|
|
|
118
|
|
|
@internal_providers_only |
|
119
|
|
|
def internal_providers(self, dummy): |
|
120
|
|
|
self.dummy = dummy |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
class TestInternalProviderOnly(unittest.TestCase): |
|
124
|
|
|
|
|
125
|
|
|
def setUp(self): |
|
126
|
|
|
self.dummy = DummyViewClassForTest() |
|
127
|
|
|
|
|
128
|
|
|
def test_all_providers(self): |
|
129
|
|
|
self.dummy.provider = DictionaryProvider(list=[species], metadata={'id': 'Test'}) |
|
130
|
|
|
self.dummy.all_providers('ok') |
|
131
|
|
|
self.assertEqual(self.dummy.dummy, 'ok') |
|
132
|
|
|
|
|
133
|
|
|
def test_internal_providers(self): |
|
134
|
|
|
self.dummy.provider = SQLAlchemyProvider(metadata={'id': 'Test', 'conceptscheme_id': 1}, |
|
135
|
|
|
session_maker=None) |
|
136
|
|
|
self.dummy.internal_providers('ok') |
|
137
|
|
|
self.assertEqual(self.dummy.dummy, 'ok') |
|
138
|
|
|
|
|
139
|
|
|
def test_external_providers(self): |
|
140
|
|
|
self.dummy.provider = SQLAlchemyProvider(metadata={'id': 'Test', 'conceptscheme_id': 1, |
|
141
|
|
|
'subject': ['external']}, |
|
142
|
|
|
session_maker=None) |
|
143
|
|
|
self.assertRaises(HTTPMethodNotAllowed, self.dummy.internal_providers, 'ok') |
|
144
|
|
|
self.assertIsNone(self.dummy.dummy) |
|
145
|
|
|
|
|
146
|
|
|
def test_no_sqlalchemyprovider(self): |
|
147
|
|
|
self.dummy.provider = DictionaryProvider(list=[species], metadata={'id': 'Test'}) |
|
148
|
|
|
self.assertRaises(HTTPMethodNotAllowed, self.dummy.internal_providers, 'ok') |
|
149
|
|
|
self.assertIsNone(self.dummy.dummy) |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
class TestUpdateLastVisitedConceptsProviderOnly(unittest.TestCase): |
|
153
|
|
|
|
|
154
|
|
|
def setUp(self): |
|
155
|
|
|
self.config = testing.setUp() |
|
156
|
|
|
self.request = testing.DummyRequest() |
|
157
|
|
|
self.request.session = {} |
|
158
|
|
|
|
|
159
|
|
|
def tearDown(self): |
|
160
|
|
|
testing.tearDown() |
|
161
|
|
|
|
|
162
|
|
|
def test_update_last_visited_concepts(self): |
|
163
|
|
|
c = Concept() |
|
164
|
|
|
c.id= 2 |
|
165
|
|
|
c.labels = [Label('test', language_id='en-us')] |
|
166
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(55)}) |
|
167
|
|
|
c = Concept() |
|
168
|
|
|
c.id= 33 |
|
169
|
|
|
c.labels = [Label('test', language_id='nl-be')] |
|
170
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(2)}) |
|
171
|
|
|
self.assertEqual(2, len(self.request.session['last_visited'])) |
|
172
|
|
|
|
|
173
|
|
|
def test_update_last_visited_concepts_max(self): |
|
174
|
|
|
for id in range(50): |
|
175
|
|
|
c = Concept() |
|
176
|
|
|
c.id = id |
|
177
|
|
|
c.labels = [Label('test', language_id='en-us')] |
|
178
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(id)}) |
|
179
|
|
|
self.assertEqual(4, len(self.request.session['last_visited'])) |
|
180
|
|
|
last = self.request.session['last_visited'].pop() |
|
181
|
|
|
self.assertEqual('http://test.test/49', last['url']) |
|
182
|
|
|
|
|
183
|
|
|
def test_no_double_last_visited_concepts(self): |
|
184
|
|
|
c = Concept() |
|
185
|
|
|
c.id = 2 |
|
186
|
|
|
c.labels = [Label('test', language_id='en-us')] |
|
187
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(55)}) |
|
188
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(55)}) |
|
189
|
|
|
c = Concept() |
|
190
|
|
|
c.id = 33 |
|
191
|
|
|
c.labels = [Label('test', language_id='nl-be')] |
|
192
|
|
|
update_last_visited_concepts(self.request, {'label': c.label(), 'url': 'http://test.test/{0}'.format(2)}) |
|
193
|
|
|
self.assertEqual(2, len(self.request.session['last_visited'])) |
|
194
|
|
|
|