# HG changeset patch # User Aurelien Campeas # Date 1401962179 -7200 # Node ID 156a3fb4a8847d5ec25887c0c8d77bb50ab93941 # Parent f8e430fb4d07e5464a145df1dccc8d9510989d01 [exttests/rest] use the new connection api diff -r f8e430fb4d07 -r 156a3fb4a884 ext/test/unittest_rest.py --- a/ext/test/unittest_rest.py Wed Jun 04 14:04:40 2014 +0200 +++ b/ext/test/unittest_rest.py Thu Jun 05 11:56:19 2014 +0200 @@ -1,4 +1,4 @@ -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This file is part of CubicWeb. @@ -21,18 +21,23 @@ from cubicweb.ext.rest import rest_publish class RestTC(CubicWebTC): - def context(self): - return self.execute('CWUser X WHERE X login "admin"').get_entity(0, 0) + + def context(self, req): + return req.execute('CWUser X WHERE X login "admin"').get_entity(0, 0) def test_eid_role(self): - context = self.context() - self.assertEqual(rest_publish(context, ':eid:`%s`' % context.eid), - '

#%s

\n' % context.eid) - self.assertEqual(rest_publish(context, ':eid:`%s:some text`' % context.eid), - '

some text

\n') + with self.admin_access.web_request() as req: + context = self.context(req) + self.assertEqual(rest_publish(context, ':eid:`%s`' % context.eid), + '

' + '#%s

\n' % context.eid) + self.assertEqual(rest_publish(context, ':eid:`%s:some text`' % context.eid), + '

' + 'some text

\n') def test_bad_rest_no_crash(self): - data = rest_publish(self.context(), ''' + with self.admin_access.web_request() as req: + rest_publish(self.context(req), ''' | card | implication | -------------------------- | 1-1 | N1 = N2 | @@ -55,159 +60,172 @@ def test_rql_role_with_vid(self): - context = self.context() - out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`') - self.assertTrue(out.endswith('anon' - '\n

\n')) + with self.admin_access.web_request() as req: + context = self.context(req) + out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`') + self.assertTrue(out.endswith('anon\n' + '

\n')) def test_rql_role_with_vid_empty_rset(self): - context = self.context() - out = rest_publish(context, ':rql:`Any X WHERE X is CWUser, X login "nono":table`') - self.assertTrue(out.endswith('

No result matching query
\n

\n')) + with self.admin_access.web_request() as req: + context = self.context(req) + out = rest_publish(context, ':rql:`Any X WHERE X is CWUser, X login "nono":table`') + self.assertTrue(out.endswith('

' + 'No result matching query
\n

\n')) def test_rql_role_with_unknown_vid(self): - context = self.context() - out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:toto`') - self.assertTrue(out.startswith("

an error occurred while interpreting this rql directive: ObjectNotFound(u'toto',)

")) + with self.admin_access.web_request() as req: + context = self.context(req) + out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:toto`') + self.assertTrue(out.startswith("

an error occurred while interpreting this " + "rql directive: ObjectNotFound(u'toto',)

")) def test_rql_role_without_vid(self): - context = self.context() - out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`') - self.assertEqual(out, u'

CWUser_plural

admin
anon

\n') + with self.admin_access.web_request() as req: + context = self.context(req) + out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`') + self.assertEqual(out, u'

CWUser_plural

' + 'admin' + '
' + 'anon' + '

\n') def test_bookmark_role(self): - context = self.context() - rset = self.execute('INSERT Bookmark X: X title "hello", X path "/view?rql=Any X WHERE X is CWUser"') - eid = rset[0][0] - out = rest_publish(context, ':bookmark:`%s`' % eid) - self.assertEqual(out, u'

CWUser_plural

admin
anon

\n') + with self.admin_access.web_request() as req: + context = self.context(req) + rset = req.execute('INSERT Bookmark X: X title "hello", X path ' + '"/view?rql=Any X WHERE X is CWUser"') + eid = rset[0][0] + out = rest_publish(context, ':bookmark:`%s`' % eid) + self.assertEqual(out, u'

CWUser_plural

' + 'admin' + '
' + 'anon' + '

\n') def test_rqltable_nocontent(self): - context = self.context() - out = rest_publish(context, """.. rql-table::""") - self.assertIn("System Message: ERROR", out) - self.assertIn("Content block expected for the "rql-table" " - "directive; none found" , out) + with self.admin_access.web_request() as req: + context = self.context(req) + out = rest_publish(context, """.. rql-table::""") + self.assertIn("System Message: ERROR", out) + self.assertIn("Content block expected for the "rql-table" " + "directive; none found" , out) def test_rqltable_norset(self): - context = self.context() - rql = "Any X WHERE X is CWUser, X firstname 'franky'" - out = rest_publish( - context, """\ + with self.admin_access.web_request() as req: + context = self.context(req) + rql = "Any X WHERE X is CWUser, X firstname 'franky'" + out = rest_publish( + context, """\ .. rql-table:: - %(rql)s""" % {'rql': rql}) - self.assertIn("System Message: WARNING", out) - self.assertIn("empty result set", out) + %(rql)s""" % {'rql': rql}) + self.assertIn("System Message: WARNING", out) + self.assertIn("empty result set", out) def test_rqltable_nooptions(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - out = rest_publish( - self.context(), """\ + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + out = rest_publish( + self.context(req), """\ .. rql-table:: %(rql)s - """ % {'rql': rql}) - req = self.request() - view = self.vreg['views'].select('table', req, rset=req.execute(rql)) - self.assertEqual(view.render(w=None)[49:], out[49:]) + """ % {'rql': rql}) + view = self.vreg['views'].select('table', req, rset=req.execute(rql)) + self.assertEqual(view.render(w=None)[49:], out[49:]) def test_rqltable_vid(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - vid = 'mytable' - out = rest_publish( - self.context(), """\ + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + vid = 'mytable' + out = rest_publish( + self.context(req), """\ .. rql-table:: :vid: %(vid)s %(rql)s - """ % {'rql': rql, 'vid': vid}) - req = self.request() - view = self.vreg['views'].select(vid, req, rset=req.execute(rql)) - self.assertEqual(view.render(w=None)[49:], out[49:]) - self.assertIn(vid, out[:49]) + """ % {'rql': rql, 'vid': vid}) + view = self.vreg['views'].select(vid, req, rset=req.execute(rql)) + self.assertEqual(view.render(w=None)[49:], out[49:]) + self.assertIn(vid, out[:49]) def test_rqltable_badvid(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - vid = 'mytabel' - out = rest_publish( - self.context(), """\ + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + vid = 'mytabel' + out = rest_publish( + self.context(req), """\ .. rql-table:: :vid: %(vid)s %(rql)s - """ % {'rql': rql, 'vid': vid}) - self.assertIn("fail to select '%s' view" % vid, out) + """ % {'rql': rql, 'vid': vid}) + self.assertIn("fail to select '%s' view" % vid, out) def test_rqltable_headers(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - headers = ["nom", "prenom", "identifiant"] - out = rest_publish( - self.context(), """\ -.. rql-table:: - :headers: %(headers)s - - %(rql)s - """ % {'rql': rql, 'headers': ', '.join(headers)}) - req = self.request() - view = self.vreg['views'].select('table', req, rset=req.execute(rql)) - view.headers = headers - self.assertEqual(view.render(w=None)[49:], out[49:]) - - def test_rqltable_headers_missing(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - headers = ["nom", "", "identifiant"] - out = rest_publish( - self.context(), """\ + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + headers = ["nom", "prenom", "identifiant"] + out = rest_publish( + self.context(req), """\ .. rql-table:: :headers: %(headers)s %(rql)s - """ % {'rql': rql, 'headers': ', '.join(headers)}) - req = self.request() - view = self.vreg['views'].select('table', req, rset=req.execute(rql)) - view.headers = [headers[0], None, headers[2]] - self.assertEqual(view.render(w=None)[49:], out[49:]) + """ % {'rql': rql, 'headers': ', '.join(headers)}) + view = self.vreg['views'].select('table', req, rset=req.execute(rql)) + view.headers = headers + self.assertEqual(view.render(w=None)[49:], out[49:]) - def test_rqltable_headers_missing_edges(self): - rql = """Any S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - headers = [" ", "prenom", ""] - out = rest_publish( - self.context(), """\ + def test_rqltable_headers_missing(self): + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + headers = ["nom", "", "identifiant"] + out = rest_publish( + self.context(req), """\ .. rql-table:: :headers: %(headers)s %(rql)s - """ % {'rql': rql, 'headers': ', '.join(headers)}) - req = self.request() - view = self.vreg['views'].select('table', req, rset=req.execute(rql)) - view.headers = [None, headers[1], None] - self.assertEqual(view.render(w=None)[49:], out[49:]) + """ % {'rql': rql, 'headers': ', '.join(headers)}) + view = self.vreg['views'].select('table', req, rset=req.execute(rql)) + view.headers = [headers[0], None, headers[2]] + self.assertEqual(view.render(w=None)[49:], out[49:]) + + def test_rqltable_headers_missing_edges(self): + with self.admin_access.web_request() as req: + rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + headers = [" ", "prenom", ""] + out = rest_publish( + self.context(req), """\ +.. rql-table:: + :headers: %(headers)s + + %(rql)s + """ % {'rql': rql, 'headers': ', '.join(headers)}) + view = self.vreg['views'].select('table', req, rset=req.execute(rql)) + view.headers = [None, headers[1], None] + self.assertEqual(view.render(w=None)[49:], out[49:]) def test_rqltable_colvids(self): - rql = """Any X,S,F,L WHERE X is CWUser, X surname S, - X firstname F, X login L""" - colvids = {0: "oneline"} - out = rest_publish( - self.context(), """\ + with self.admin_access.web_request() as req: + rql = "Any X,S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L" + colvids = {0: "oneline"} + out = rest_publish( + self.context(req), """\ .. rql-table:: :colvids: %(colvids)s %(rql)s - """ % {'rql': rql, - 'colvids': ', '.join(["%d=%s" % (k, v) - for k, v in colvids.iteritems()]) - }) - req = self.request() - view = self.vreg['views'].select('table', req, rset=req.execute(rql)) - view.cellvids = colvids - self.assertEqual(view.render(w=None)[49:], out[49:]) + """ % {'rql': rql, + 'colvids': ', '.join(["%d=%s" % (k, v) + for k, v in colvids.iteritems()]) + }) + view = self.vreg['views'].select('table', req, rset=req.execute(rql)) + view.cellvids = colvids + self.assertEqual(view.render(w=None)[49:], out[49:]) if __name__ == '__main__':