|
1 # copyright 2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 from datetime import datetime |
|
20 |
|
21 from cubicweb.devtools.testlib import CubicWebTC |
|
22 |
|
23 from cubicweb.sobjects.parsers import CWEntityXMLParser |
|
24 |
|
25 orig_parse = CWEntityXMLParser.parse |
|
26 |
|
27 def parse(url): |
|
28 try: |
|
29 url = RELATEDXML[url.split('?')[0]] |
|
30 except KeyError: |
|
31 pass |
|
32 return orig_parse(url) |
|
33 |
|
34 def setUpModule(): |
|
35 CWEntityXMLParser.parse = staticmethod(parse) |
|
36 |
|
37 def tearDownModule(): |
|
38 CWEntityXMLParser.parse = orig_parse |
|
39 |
|
40 |
|
41 BASEXML = ''.join(u''' |
|
42 <rset size="1"> |
|
43 <CWUser eid="5" cwuri="http://pouet.org/eid/5"> |
|
44 <login>sthenault</login> |
|
45 <upassword>toto</upassword> |
|
46 <last_login_time>2011-01-25 14:14:06</last_login_time> |
|
47 <creation_date>2010-01-22 10:27:59</creation_date> |
|
48 <modification_date>2011-01-25 14:14:06</modification_date> |
|
49 <use_email role="subject"> |
|
50 <EmailAddress cwuri="http://pouet.org/eid/6" eid="6"/> |
|
51 </use_email> |
|
52 <in_group role="subject"> |
|
53 <CWGroup cwuri="http://pouet.org/eid/7" eid="7"/> |
|
54 <CWGroup cwuri="http://pouet.org/eid/8" eid="8"/> |
|
55 </in_group> |
|
56 <tags role="object"> |
|
57 <Tag cwuri="http://pouet.org/eid/9" eid="9"/> |
|
58 <Tag cwuri="http://pouet.org/eid/10" eid="10"/> |
|
59 </tags> |
|
60 </CWUser> |
|
61 </rset> |
|
62 '''.splitlines()) |
|
63 |
|
64 RELATEDXML ={ |
|
65 'http://pouet.org/eid/6': u''' |
|
66 <rset size="1"> |
|
67 <EmailAddress eid="6" cwuri="http://pouet.org/eid/6"> |
|
68 <address>syt@logilab.fr</address> |
|
69 <modification_date>2010-04-13 14:35:56</modification_date> |
|
70 <creation_date>2010-04-13 14:35:56</creation_date> |
|
71 </EmailAddress> |
|
72 </rset> |
|
73 ''', |
|
74 'http://pouet.org/eid/7': u''' |
|
75 <rset size="1"> |
|
76 <CWGroup eid="7" cwuri="http://pouet.org/eid/7"> |
|
77 <name>users</name> |
|
78 </CWGroup> |
|
79 </rset> |
|
80 ''', |
|
81 'http://pouet.org/eid/8': u''' |
|
82 <rset size="1"> |
|
83 <CWGroup eid="8" cwuri="http://pouet.org/eid/8"> |
|
84 <name>unknown</name> |
|
85 </CWGroup> |
|
86 </rset> |
|
87 ''', |
|
88 'http://pouet.org/eid/9': u''' |
|
89 <rset size="1"> |
|
90 <Tag eid="9" cwuri="http://pouet.org/eid/9"> |
|
91 <name>hop</name> |
|
92 </Tag> |
|
93 </rset> |
|
94 ''', |
|
95 'http://pouet.org/eid/10': u''' |
|
96 <rset size="1"> |
|
97 <Tag eid="10" cwuri="http://pouet.org/eid/10"> |
|
98 <name>unknown</name> |
|
99 </Tag> |
|
100 </rset> |
|
101 ''', |
|
102 } |
|
103 |
|
104 class CWEntityXMLParserTC(CubicWebTC): |
|
105 def setup_database(self): |
|
106 req = self.request() |
|
107 source = req.create_entity('CWSource', name=u'myfeed', type=u'datafeed', |
|
108 parser=u'cw.entityxml', url=BASEXML) |
|
109 self.commit() |
|
110 source.init_mapping([(('CWUser', 'use_email', '*'), |
|
111 u'role=subject\naction=copy'), |
|
112 (('CWUser', 'in_group', '*'), |
|
113 u'role=subject\naction=link\nlinkattr=name'), |
|
114 (('*', 'tags', 'CWUser'), |
|
115 u'role=object\naction=link-or-create\nlinkattr=name'), |
|
116 ]) |
|
117 req.create_entity('Tag', name=u'hop') |
|
118 |
|
119 def test_actions(self): |
|
120 dfsource = self.repo.sources_by_uri['myfeed'] |
|
121 self.assertEqual(dfsource.mapping, |
|
122 {u'CWUser': { |
|
123 (u'in_group', u'subject', u'link'): [ |
|
124 (u'CWGroup', {u'linkattr': u'name'})], |
|
125 (u'tags', u'object', u'link-or-create'): [ |
|
126 (u'Tag', {u'linkattr': u'name'})], |
|
127 (u'use_email', u'subject', u'copy'): [ |
|
128 (u'EmailAddress', {})] |
|
129 } |
|
130 }) |
|
131 session = self.repo.internal_session() |
|
132 stats = dfsource.pull_data(session, force=True) |
|
133 self.assertEqual(sorted(stats.keys()), ['created', 'updated']) |
|
134 self.assertEqual(len(stats['created']), 2) |
|
135 self.assertEqual(stats['updated'], set()) |
|
136 |
|
137 user = self.execute('CWUser X WHERE X login "sthenault"').get_entity(0, 0) |
|
138 self.assertEqual(user.creation_date, datetime(2010, 01, 22, 10, 27, 59)) |
|
139 self.assertEqual(user.modification_date, datetime(2011, 01, 25, 14, 14, 06)) |
|
140 self.assertEqual(user.cwuri, 'http://pouet.org/eid/5') |
|
141 self.assertEqual(user.cw_source[0].name, 'myfeed') |
|
142 self.assertEqual(len(user.use_email), 1) |
|
143 # copy action |
|
144 email = user.use_email[0] |
|
145 self.assertEqual(email.address, 'syt@logilab.fr') |
|
146 self.assertEqual(email.cwuri, 'http://pouet.org/eid/6') |
|
147 self.assertEqual(email.cw_source[0].name, 'myfeed') |
|
148 # link action |
|
149 self.assertFalse(self.execute('CWGroup X WHERE X name "unknown"')) |
|
150 groups = sorted([g.name for g in user.in_group]) |
|
151 self.assertEqual(groups, ['users']) |
|
152 # link or create action |
|
153 tags = sorted([t.name for t in user.reverse_tags]) |
|
154 self.assertEqual(tags, ['hop', 'unknown']) |
|
155 tag = self.execute('Tag X WHERE X name "unknown"').get_entity(0, 0) |
|
156 self.assertEqual(tag.cwuri, 'http://testing.fr/cubicweb/eid/%s' % tag.eid) |
|
157 self.assertEqual(tag.cw_source[0].name, 'system') |
|
158 |
|
159 stats = dfsource.pull_data(session, force=True) |
|
160 self.assertEqual(stats['created'], set()) |
|
161 self.assertEqual(len(stats['updated']), 2) |
|
162 self.repo._type_source_cache.clear() |
|
163 self.repo._extid_cache.clear() |
|
164 stats = dfsource.pull_data(session, force=True) |
|
165 self.assertEqual(stats['created'], set()) |
|
166 self.assertEqual(len(stats['updated']), 2) |
|
167 |
|
168 if __name__ == '__main__': |
|
169 from logilab.common.testlib import unittest_main |
|
170 unittest_main() |