web/test/unittest_views_forms.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2014 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 logilab.common import tempattr, attrdict
       
    20 
       
    21 from cubicweb.devtools.testlib import CubicWebTC
       
    22 from cubicweb.web.views.autoform import InlinedFormField
       
    23 
       
    24 class InlinedFormTC(CubicWebTC):
       
    25 
       
    26     def test_linked_to(self):
       
    27         with self.admin_access.web_request() as req:
       
    28             formview = req.vreg['views'].select(
       
    29                 'inline-creation', req,
       
    30                 etype='File', rtype='described_by_test', role='subject',
       
    31                 peid=123,
       
    32                 petype='Salesterm')
       
    33             self.assertEqual({('described_by_test', 'object'): [123]},
       
    34                              formview.form.linked_to)
       
    35 
       
    36     def test_linked_to_parent_being_created(self):
       
    37         with self.admin_access.web_request() as req:
       
    38             formview = req.vreg['views'].select(
       
    39                 'inline-creation', req,
       
    40                 etype='File', rtype='described_by_test', role='subject',
       
    41                 peid='A',
       
    42                 petype='Salesterm')
       
    43             self.assertEqual(formview.form.linked_to, {})
       
    44 
       
    45     def test_remove_js_depending_on_cardinality(self):
       
    46         with self.admin_access.web_request() as req:
       
    47             formview = req.vreg['views'].select(
       
    48                 'inline-creation', req,
       
    49                 etype='File', rtype='described_by_test', role='subject',
       
    50                 peid='A',
       
    51                 petype='Salesterm')
       
    52             # cardinality is 1, can't remove
       
    53             self.assertIsNone(formview._get_removejs())
       
    54             rdef = self.schema['Salesterm'].rdef('described_by_test')
       
    55             with tempattr(rdef, 'cardinality', '?*'):
       
    56                 self.assertTrue(formview._get_removejs())
       
    57             with tempattr(rdef, 'cardinality', '+*'):
       
    58                 # formview has no parent info (pform). This is what happens
       
    59                 # when an inline form is requested through AJAX.
       
    60                 self.assertTrue(formview._get_removejs())
       
    61                 fakeview = attrdict(dict(rtype='described_by_test', role='subject'))
       
    62                 # formview is first, can't be removed
       
    63                 formview.pform = attrdict(fields=[InlinedFormField(view=formview),
       
    64                                                   InlinedFormField(view=fakeview)])
       
    65                 self.assertIsNone(formview._get_removejs())
       
    66                 # formview isn't first, can be removed
       
    67                 formview.pform = attrdict(fields=[InlinedFormField(view=fakeview),
       
    68                                                   InlinedFormField(view=formview)])
       
    69                 self.assertTrue(formview._get_removejs())
       
    70 
       
    71 
       
    72 if __name__ == '__main__':
       
    73     from logilab.common.testlib import unittest_main
       
    74     unittest_main()