ext/test/unittest_rest.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 03 Jul 2013 14:33:27 +0200
branchstable
changeset 9130 0f1504a9fb51
parent 8852 59a29405688c
child 9322 2dae5bf5ea68
permissions -rw-r--r--
[constraint] more robust unicity constraint failures reporting for end-users Postgres or Sqlserver have limits on the index names (around resp. 64 and 128 characters). Because `logilab.database` encodes the `unique together` constraint rtypes in the index names, we sometimes get truncated index names, from which it is impossible to retrieve all rtypes. In the long run, the way such index are named should be changed. In the short term, we try to reduce the end-user confusion resulting from this design flaw: * in source/native, the regex filtering ``IntegrityError`` message does not impose an `_idx` suffix, which indeed may be absent (the result being an UI message that resembles a catastrophic failure), * also we avoid including a trailing " (double quote) from the error message * in entities/adapters, the well-named ``IUserFriendly`` adapter is made a bit smarter about how to handle missing rtypes. * the adapter also always produces a global message explaining the issue (and the fact that sometimes, the user is not shown all the relevant info) * i18n is updated Closes #2793789
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8255
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
from logilab.common.testlib import unittest_main
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    19
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    21
from cubicweb.ext.rest import rest_publish
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    22
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    23
class RestTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    def context(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    25
        return self.execute('CWUser X WHERE X login "admin"').get_entity(0, 0)
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    26
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
    def test_eid_role(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
        context = self.context()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    29
        self.assertEqual(rest_publish(context, ':eid:`%s`' % context.eid),
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    30
                          '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">#%s</a></p>\n' % context.eid)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    31
        self.assertEqual(rest_publish(context, ':eid:`%s:some text`' %  context.eid),
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    32
                          '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">some text</a></p>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    def test_bad_rest_no_crash(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        data = rest_publish(self.context(), '''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
| card | implication     |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
| 1-1  | N1 = N2         |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
| 1-?  | N1 <= N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
| 1-+  | N1 >= N2        |
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    41
| 1-*  | N1>0 => N2>0    |
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
| ?-?  | N1 # N2         |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
| ?-+  | N1 >= N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
| ?-*  | N1 #  N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
| +-+  | N1>0 => N2>0 et |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
|      | N2>0 => N1>0    |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
| +-*  | N1>+ => N2>0    |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
| *-*  | N1#N2           |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
''')
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    55
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    56
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    57
    def test_rql_role_with_vid(self):
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    58
        context = self.context()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    59
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`')
8255
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    60
        self.assertTrue(out.endswith('<a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a>'
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    61
                                     '</td></tr>\n</tbody></table></div></p>\n'))
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    62
7732
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    63
    def test_rql_role_with_vid_empty_rset(self):
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    64
        context = self.context()
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    65
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser, X login "nono":table`')
8255
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    66
        self.assertTrue(out.endswith('<p><div class="searchMessage"><strong>No result matching query</strong></div>\n</p>\n'))
7732
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    67
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    68
    def test_rql_role_with_unknown_vid(self):
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    69
        context = self.context()
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    70
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:toto`')
8255
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    71
        self.assertTrue(out.startswith("<p>an error occured while interpreting this rql directive: ObjectNotFound(u'toto',)</p>"))
7732
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    72
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    73
    def test_rql_role_without_vid(self):
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    74
        context = self.context()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    75
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`')
8255
25a00e4a8722 [test] fix ReST extensions tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7732
diff changeset
    76
        self.assertEqual(out, u'<p><h1>CWUser_plural</h1><div class="section"><a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin</a></div><div class="section"><a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a></div></p>\n')
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    77
8852
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    78
    def test_bookmark_role(self):
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    79
        context = self.context()
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    80
        rset = self.execute('INSERT Bookmark X: X title "hello", X path "/view?rql=Any X WHERE X is CWUser"')
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    81
        eid = rset[0][0]
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    82
        out = rest_publish(context, ':bookmark:`%s`' % eid)
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    83
        self.assertEqual(out, u'<p><h1>CWUser_plural</h1><div class="section"><a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin</a></div><div class="section"><a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a></div></p>\n')
59a29405688c [ext/rest] add directive bookmark to rest (closes #2545595)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8255
diff changeset
    84
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    unittest_main()