1
1
import requests
2
- import json
3
2
import logging
4
- import os
5
3
import urllib
6
4
import datetime
7
5
8
- from nose .tools import raises , assert_equal
6
+ from nose .tools import assert_equal
9
7
from ckan import model
10
8
from ckan .tests import BaseCase
11
9
from ckan .logic import get_action
12
10
13
11
import ckanext .qa .tasks
14
12
from ckanext .qa .tasks import resource_score , extension_variants
15
13
import ckanext .archiver
14
+ import ckanext .archiver .tasks
16
15
from ckanext .dgu .lib .formats import Formats
17
16
from ckanext .qa import model as qa_model
18
17
from ckanext .archiver import model as archiver_model
@@ -43,6 +42,43 @@ def set_sniffed_format(format_display_name):
43
42
44
43
TODAY = datetime .datetime (year = 2008 , month = 10 , day = 10 )
45
44
45
+ class TestTask (BaseCase ):
46
+
47
+ @classmethod
48
+ def setup_class (cls ):
49
+ archiver_model .init_tables (model .meta .engine )
50
+ qa_model .init_tables (model .meta .engine )
51
+
52
+ def teardown (self ):
53
+ model .repo .rebuild_db ()
54
+
55
+ def test_trigger_on_archival (cls ):
56
+ # create package
57
+ context = {'model' : model , 'ignore_auth' : True , 'session' : model .Session , 'user' : 'test' }
58
+ pkg = {'name' : 'testpkg' , 'license_id' : 'uk-ogl' , 'resources' : [
59
+ {'url' : 'http://test.com/' , 'format' : 'CSV' , 'description' : 'Test' }
60
+ ]}
61
+ pkg = get_action ('package_create' )(context , pkg )
62
+ resource_dict = pkg ['resources' ][0 ]
63
+ res_id = resource_dict ['id' ]
64
+ # create record of archival
65
+ archival = Archival .create (res_id )
66
+ cache_filepath = __file__ # just needs to exist
67
+ archival .cache_filepath = cache_filepath
68
+ archival .updated = TODAY
69
+ model .Session .add (archival )
70
+ model .Session .commit ()
71
+ # TODO show that QA hasn't run yet
72
+
73
+ # create a send_data from ckanext-archiver, that gets picked up by
74
+ # ckanext-qa to put a task on the queue
75
+ ckanext .archiver .tasks .notify (resource_dict , cache_filepath )
76
+ # this is useful on its own (without any asserts) because it checks
77
+ # there are no exceptions when running it
78
+
79
+ # TODO run celery and check it actually ran...
80
+
81
+
46
82
class TestResourceScore (BaseCase ):
47
83
48
84
@classmethod
@@ -60,11 +96,7 @@ def setup_class(cls):
60
96
}
61
97
62
98
def teardown (self ):
63
- pkg = model .Package .get (u'testpkg' )
64
- if pkg :
65
- model .repo .new_revision ()
66
- pkg .purge ()
67
- model .repo .commit_and_remove ()
99
+ model .repo .rebuild_db ()
68
100
69
101
def _test_resource (self , url = 'anything' , format = 'TXT' , archived = True , cached = True , license_id = 'uk-ogl' ):
70
102
context = {'model' : model , 'ignore_auth' : True , 'session' : model .Session , 'user' : 'test' }
@@ -79,7 +111,7 @@ def _test_resource(self, url='anything', format='TXT', archived=True, cached=Tru
79
111
archival .updated = TODAY
80
112
model .Session .add (archival )
81
113
model .Session .commit ()
82
- return res_id
114
+ return model . Resource . get ( res_id )
83
115
84
116
@classmethod
85
117
def _set_task_status (cls , task_type , task_status_str ):
@@ -173,16 +205,16 @@ def test_available_but_not_open(self):
173
205
assert 'License not open' in result ['openness_score_reason' ], result
174
206
175
207
def test_not_available_and_not_open (self ):
176
- res_id = self ._test_resource (license_id = None , format = None , cached = False )
177
- archival = Archival .get_for_resource (res_id )
208
+ res = self ._test_resource (license_id = None , format = None , cached = False )
209
+ archival = Archival .get_for_resource (res . id )
178
210
archival .status_id = Status .by_text ('Download error' )
179
211
archival .reason = 'Server returned 500 error'
180
212
archival .last_success = None
181
213
archival .first_failure = datetime .datetime (year = 2008 , month = 10 , day = 1 , hour = 6 , minute = 30 )
182
214
archival .failure_count = 16
183
215
archival .is_broken = True
184
216
model .Session .commit ()
185
- result = resource_score (res_id , log )
217
+ result = resource_score (res , log )
186
218
assert result ['openness_score' ] == 0 , result
187
219
assert_equal (result ['format' ], None )
188
220
# in preference it should report that it is not available
@@ -192,22 +224,22 @@ def test_not_available_any_more(self):
192
224
# A cache of the data still exists from the previous run, but this
193
225
# time, the archiver found the file gave a 404.
194
226
# The record of the previous (successful) run of QA.
195
- res_id = self ._test_resource (license_id = None , format = None )
196
- qa = qa_model .QA .create (res_id )
227
+ res = self ._test_resource (license_id = None , format = None )
228
+ qa = qa_model .QA .create (res . id )
197
229
qa .format = 'CSV'
198
230
model .Session .add (qa )
199
231
model .Session .commit ()
200
232
# cache still exists from the previous run, but this time, the archiver
201
233
# found the file gave a 404.
202
- archival = Archival .get_for_resource (res_id )
234
+ archival = Archival .get_for_resource (res . id )
203
235
archival .cache_filepath = __file__
204
236
archival .status_id = Status .by_text ('Download error' )
205
237
archival .reason = 'Server returned 404 error'
206
238
archival .last_success = datetime .datetime (year = 2008 , month = 10 , day = 1 )
207
239
archival .first_failure = datetime .datetime (year = 2008 , month = 10 , day = 2 )
208
240
archival .failure_count = 1
209
241
archival .is_broken = True
210
- result = resource_score (res_id , log )
242
+ result = resource_score (res , log )
211
243
assert result ['openness_score' ] == 0 , result
212
244
assert_equal (result ['format' ], 'CSV' )
213
245
# in preference it should report that it is not available
0 commit comments