1 # coding: utf-8 |
|
2 # copyright 2011-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 |
|
20 from datetime import timedelta |
|
21 from contextlib import contextmanager |
|
22 |
|
23 from cubicweb.devtools.testlib import CubicWebTC |
|
24 from cubicweb.server.sources import datafeed |
|
25 |
|
26 |
|
27 class DataFeedTC(CubicWebTC): |
|
28 def setup_database(self): |
|
29 with self.admin_access.repo_cnx() as cnx: |
|
30 with self.base_parser(cnx): |
|
31 cnx.create_entity('CWSource', name=u'ô myfeed', type=u'datafeed', |
|
32 parser=u'testparser', url=u'ignored', |
|
33 config=u'synchronization-interval=1min') |
|
34 cnx.commit() |
|
35 |
|
36 @contextmanager |
|
37 def base_parser(self, session): |
|
38 class AParser(datafeed.DataFeedParser): |
|
39 __regid__ = 'testparser' |
|
40 def process(self, url, raise_on_error=False): |
|
41 entity = self.extid2entity('http://www.cubicweb.org/', 'Card', |
|
42 item={'title': u'cubicweb.org', |
|
43 'content': u'the cw web site'}, |
|
44 raise_on_error=raise_on_error) |
|
45 if not self.created_during_pull(entity): |
|
46 self.notify_updated(entity) |
|
47 def before_entity_copy(self, entity, sourceparams): |
|
48 entity.cw_edited.update(sourceparams['item']) |
|
49 |
|
50 with self.temporary_appobjects(AParser): |
|
51 if u'ô myfeed' in self.repo.sources_by_uri: |
|
52 yield self.repo.sources_by_uri[u'ô myfeed']._get_parser(session) |
|
53 else: |
|
54 yield |
|
55 |
|
56 def test(self): |
|
57 self.assertIn(u'ô myfeed', self.repo.sources_by_uri) |
|
58 dfsource = self.repo.sources_by_uri[u'ô myfeed'] |
|
59 self.assertNotIn('use_cwuri_as_url', dfsource.__dict__) |
|
60 self.assertEqual({'type': u'datafeed', 'uri': u'ô myfeed', 'use-cwuri-as-url': True}, |
|
61 dfsource.public_config) |
|
62 self.assertEqual(dfsource.use_cwuri_as_url, True) |
|
63 self.assertEqual(dfsource.latest_retrieval, None) |
|
64 self.assertEqual(dfsource.synchro_interval, timedelta(seconds=60)) |
|
65 self.assertFalse(dfsource.fresh()) |
|
66 # ensure source's logger name has been unormalized |
|
67 self.assertEqual(dfsource.info.__self__.name, 'cubicweb.sources.o myfeed') |
|
68 |
|
69 with self.repo.internal_cnx() as cnx: |
|
70 with self.base_parser(cnx): |
|
71 stats = dfsource.pull_data(cnx, force=True) |
|
72 cnx.commit() |
|
73 # test import stats |
|
74 self.assertEqual(sorted(stats), ['checked', 'created', 'updated']) |
|
75 self.assertEqual(len(stats['created']), 1) |
|
76 entity = cnx.execute('Card X').get_entity(0, 0) |
|
77 self.assertIn(entity.eid, stats['created']) |
|
78 self.assertEqual(stats['updated'], set()) |
|
79 # test imported entities |
|
80 self.assertEqual(entity.title, 'cubicweb.org') |
|
81 self.assertEqual(entity.content, 'the cw web site') |
|
82 self.assertEqual(entity.cwuri, 'http://www.cubicweb.org/') |
|
83 self.assertEqual(entity.cw_source[0].name, u'ô myfeed') |
|
84 self.assertEqual(entity.cw_metainformation(), |
|
85 {'type': 'Card', |
|
86 'source': {'uri': u'ô myfeed', 'type': 'datafeed', 'use-cwuri-as-url': True}, |
|
87 'extid': b'http://www.cubicweb.org/'} |
|
88 ) |
|
89 self.assertEqual(entity.absolute_url(), 'http://www.cubicweb.org/') |
|
90 # test repo cache keys |
|
91 self.assertEqual(self.repo._type_source_cache[entity.eid], |
|
92 ('Card', b'http://www.cubicweb.org/', u'ô myfeed')) |
|
93 self.assertEqual(self.repo._extid_cache[b'http://www.cubicweb.org/'], |
|
94 entity.eid) |
|
95 # test repull |
|
96 stats = dfsource.pull_data(cnx, force=True) |
|
97 self.assertEqual(stats['created'], set()) |
|
98 self.assertEqual(stats['updated'], set((entity.eid,))) |
|
99 # test repull with caches reseted |
|
100 self.repo._type_source_cache.clear() |
|
101 self.repo._extid_cache.clear() |
|
102 stats = dfsource.pull_data(cnx, force=True) |
|
103 self.assertEqual(stats['created'], set()) |
|
104 self.assertEqual(stats['updated'], set((entity.eid,))) |
|
105 self.assertEqual(self.repo._type_source_cache[entity.eid], |
|
106 ('Card', b'http://www.cubicweb.org/', u'ô myfeed')) |
|
107 self.assertEqual(self.repo._extid_cache[b'http://www.cubicweb.org/'], |
|
108 entity.eid) |
|
109 |
|
110 self.assertEqual(dfsource.source_cwuris(cnx), |
|
111 {b'http://www.cubicweb.org/': (entity.eid, 'Card')}) |
|
112 self.assertTrue(dfsource.latest_retrieval) |
|
113 self.assertTrue(dfsource.fresh()) |
|
114 |
|
115 # test_rename_source |
|
116 with self.admin_access.repo_cnx() as cnx: |
|
117 cnx.entity_from_eid(dfsource.eid).cw_set(name=u"myrenamedfeed") |
|
118 cnx.commit() |
|
119 entity = cnx.execute('Card X').get_entity(0, 0) |
|
120 self.assertEqual(entity.cwuri, 'http://www.cubicweb.org/') |
|
121 self.assertEqual(entity.cw_source[0].name, 'myrenamedfeed') |
|
122 self.assertEqual(entity.cw_metainformation(), |
|
123 {'type': 'Card', |
|
124 'source': {'uri': 'myrenamedfeed', 'type': 'datafeed', 'use-cwuri-as-url': True}, |
|
125 'extid': b'http://www.cubicweb.org/'} |
|
126 ) |
|
127 self.assertEqual(self.repo._type_source_cache[entity.eid], |
|
128 ('Card', b'http://www.cubicweb.org/', 'myrenamedfeed')) |
|
129 self.assertEqual(self.repo._extid_cache[b'http://www.cubicweb.org/'], |
|
130 entity.eid) |
|
131 |
|
132 # test_delete_source |
|
133 cnx.execute('DELETE CWSource S WHERE S name "myrenamedfeed"') |
|
134 cnx.commit() |
|
135 self.assertFalse(cnx.execute('Card X WHERE X title "cubicweb.org"')) |
|
136 self.assertFalse(cnx.execute('Any X WHERE X has_text "cubicweb.org"')) |
|
137 |
|
138 def test_parser_retrieve_url_local(self): |
|
139 with self.admin_access.repo_cnx() as cnx: |
|
140 with self.base_parser(cnx) as parser: |
|
141 value = parser.retrieve_url('a string') |
|
142 self.assertEqual(200, value.getcode()) |
|
143 self.assertEqual('a string', value.geturl()) |
|
144 |
|
145 def test_update_url(self): |
|
146 dfsource = self.repo.sources_by_uri[u'ô myfeed'] |
|
147 with self.admin_access.repo_cnx() as cnx: |
|
148 cnx.entity_from_eid(dfsource.eid).cw_set(url=u"http://pouet.com\nhttp://pouet.org") |
|
149 self.assertEqual(dfsource.urls, [u'ignored']) |
|
150 cnx.commit() |
|
151 self.assertEqual(dfsource.urls, [u"http://pouet.com", u"http://pouet.org"]) |
|
152 |
|
153 |
|
154 class DataFeedConfigTC(CubicWebTC): |
|
155 |
|
156 def test_use_cwuri_as_url_override(self): |
|
157 with self.admin_access.client_cnx() as cnx: |
|
158 cnx.create_entity('CWSource', name=u'myfeed', type=u'datafeed', |
|
159 parser=u'testparser', url=u'ignored', |
|
160 config=u'use-cwuri-as-url=no') |
|
161 cnx.commit() |
|
162 dfsource = self.repo.sources_by_uri['myfeed'] |
|
163 self.assertEqual(dfsource.use_cwuri_as_url, False) |
|
164 self.assertEqual({'type': u'datafeed', 'uri': u'myfeed', 'use-cwuri-as-url': False}, |
|
165 dfsource.public_config) |
|
166 |
|
167 if __name__ == '__main__': |
|
168 from logilab.common.testlib import unittest_main |
|
169 unittest_main() |
|