-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_feed.py
173 lines (133 loc) · 5.64 KB
/
test_feed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# encoding: utf-8
from ckan.lib.helpers import url_for
import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
import ckan.plugins as plugins
from ckan.views.feed import _FixedAtomFeed
from werkzeug.contrib.atom import AtomFeed, FeedEntry
class TestFeedNew(helpers.FunctionalTestBase):
@classmethod
def teardown_class(cls):
helpers.reset_db()
def test_atom_feed_page_zero_gives_error(self):
group = factories.Group()
offset = url_for(u'feeds.group', id=group['name']) + '?page=0'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + u'?page=0'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_atom_feed_page_negative_gives_error(self):
group = factories.Group()
offset = url_for(u'feeds.group', id=group['name']) + '?page=-2'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + '?page=-2'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_atom_feed_page_not_int_gives_error(self):
group = factories.Group()
offset = url_for(u'feeds.group', id=group['name']) + '?page=abc'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + '?page=abc'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res
def test_general_atom_feed_works(self):
dataset = factories.Dataset()
offset = url_for(u'feeds.general')
app = self._get_test_app()
offset = url_for(u'feeds.general')
res = app.get(offset)
assert u'<title type="text">{0}</title>'.format(
dataset['title']) in res.body
def test_group_atom_feed_works(self):
group = factories.Group()
dataset = factories.Dataset(groups=[{'id': group['id']}])
offset = url_for(u'feeds.group', id=group['name'])
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name'])
res = app.get(offset)
assert u'<title type="text">{0}</title>'.format(
dataset['title']) in res.body
def test_organization_atom_feed_works(self):
group = factories.Organization()
dataset = factories.Dataset(owner_org=group['id'])
offset = url_for(u'feeds.organization', id=group['name'])
app = self._get_test_app()
offset = url_for(u'feeds.organization', id=group['name'])
res = app.get(offset)
assert u'<title type="text">{0}</title>'.format(
dataset['title']) in res.body
def test_custom_atom_feed_works(self):
dataset1 = factories.Dataset(
title=u'Test weekly',
extras=[{
'key': 'frequency',
'value': 'weekly'
}])
dataset2 = factories.Dataset(
title=u'Test daily',
extras=[{
'key': 'frequency',
'value': 'daily'
}])
offset = url_for('feeds.custom')
params = {'q': 'frequency:weekly'}
app = self._get_test_app()
res = app.get(offset, params=params)
assert u'<title type="text">{0}</title>'.format(
dataset1['title']) in res.body
assert u'<title type="text">{0}</title>'.format(
dataset2['title']) not in res.body
class TestFeedInterface(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestFeedInterface, cls).setup_class()
if not plugins.plugin_loaded('test_feed_plugin'):
plugins.load('test_feed_plugin')
@classmethod
def teardown_class(cls):
helpers.reset_db()
plugins.unload('test_feed_plugin')
def test_custom_class_used(self):
offset = url_for(u'feeds.custom')
app = self._get_test_app()
res = app.get(offset)
assert 'xmlns="http://www.w3.org/2005/Atom"' in res.body, res.body
assert 'CKAN - Custom query' in res.body, res.body
def test_additional_fields_added(self):
metadata = {
'ymin': '-2373790',
'xmin': '2937940',
'ymax': '-1681290',
'xmax': '3567770',
}
extras = [{'key': k, 'value': v} for (k, v) in metadata.items()]
factories.Dataset(extras=extras)
offset = url_for(u'feeds.custom')
app = self._get_test_app()
res = app.get(offset)
assert '<geometry>-2373790.000000 2937940.000000 -1681290.000000 3567770.000000</geometry>' in res.body, res.body
# assert '<extras>' in res.body, res.body
class MockFeedPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IFeed)
def get_feed_class(self):
return _GeoAtomFeed
def get_item_additional_fields(self, dataset_dict):
extras = {e['key']: e['value'] for e in dataset_dict['extras']}
box = tuple(
float(extras.get(n)) for n in ('ymin', 'xmin', 'ymax', 'xmax'))
return {'geometry': box}
class _GeoAtomFeed(_FixedAtomFeed):
def __init__(self, **kwargs):
_FixedAtomFeed.__init__(self, **kwargs)
def add(self, *args, **kwargs):
import pdb; pdb.set_trace()
if u'extras' in kwargs:
kwargs.pop(u'extras')
extras = {u'extras': None}
extras.update(kwargs)
entrie = _CustomFeedEntry(**extras)
_FixedAtomFeed.add(self, entrie, id=entrie.id, updated=entrie.updated)
class _CustomFeedEntry(FeedEntry):
def __init__(self, **kwargs):
FeedEntry.__init__(self, **kwargs)
self.extras = kwargs.get('extras')