[dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
# 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.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.fromlogilab.common.testlibimportunittest_main,mock_objectfromcubicweb.devtools.testlibimportCubicWebTCfromcubicweb.web.viewsimportuicfgfromcubicweb.web.formwidgetsimportAutoCompletionWidgetfromcubicweb.schemaimportRRQLExpressionAFFK=uicfg.autoform_field_kwargsAFS=uicfg.autoform_sectiondefrbc(entity,formtype,section):ifsectionin('attributes','metadata','hidden'):permission='update'else:permission='add'return[(rschema.type,x)forrschema,tschemas,xinAFS.relations_by_section(entity,formtype,section,permission)]classAutomaticEntityFormTC(CubicWebTC):deftest_custom_widget(self):withself.admin_access.web_request()asreq:AFFK.tag_subject_of(('CWUser','login','*'),{'widget':AutoCompletionWidget(autocomplete_initfunc='get_logins')})form=self.vreg['forms'].select('edition',req,entity=req.user)field=form.field_by_name('login','subject')self.assertIsInstance(field.widget,AutoCompletionWidget)AFFK.del_rtag('CWUser','login','*','subject')deftest_cwuser_relations_by_category(self):withself.admin_access.web_request()asreq:e=self.vreg['etypes'].etype_class('CWUser')(req)# see custom configuration in views.cwuserself.assertEqual(rbc(e,'main','attributes'),[('login','subject'),('upassword','subject'),('firstname','subject'),('surname','subject'),('in_group','subject'),])self.assertEqual(rbc(e,'muledit','attributes'),[('login','subject'),('upassword','subject'),('in_group','subject'),])self.assertCountEqual(rbc(e,'main','metadata'),[('last_login_time','subject'),('cw_source','subject'),('creation_date','subject'),('modification_date','subject'),('created_by','subject'),('owned_by','subject'),('bookmarked_by','object'),])# XXX skip 'tags' relation here and in the hidden category because# of some test interdependancy when pytest is launched on whole cw# (appears here while expected in hiddenself.assertCountEqual([xforxinrbc(e,'main','relations')ifx!=('tags','object')],[('connait','subject'),('custom_workflow','subject'),('primary_email','subject'),('checked_by','object'),])self.assertListEqual(rbc(e,'main','inlined'),[('use_email','subject'),])# owned_by is defined both as subject and object relations on CWUserself.assertListEqual(sorted(xforxinrbc(e,'main','hidden')ifx!=('tags','object')),sorted([('for_user','object'),('created_by','object'),('wf_info_for','object'),('owned_by','object'),]))deftest_inlined_view(self):self.assertIn('main_inlined',AFS.etype_get('CWUser','use_email','subject','EmailAddress'))self.assertNotIn('main_inlined',AFS.etype_get('CWUser','primary_email','subject','EmailAddress'))self.assertIn('main_relations',AFS.etype_get('CWUser','primary_email','subject','EmailAddress'))deftest_personne_relations_by_category(self):withself.admin_access.web_request()asreq:e=self.vreg['etypes'].etype_class('Personne')(req)self.assertListEqual(rbc(e,'main','attributes'),[('nom','subject'),('prenom','subject'),('sexe','subject'),('promo','subject'),('titre','subject'),('ass','subject'),('web','subject'),('tel','subject'),('fax','subject'),('datenaiss','subject'),('test','subject'),('description','subject'),('salary','subject'),])self.assertListEqual(rbc(e,'muledit','attributes'),[('nom','subject'),])self.assertCountEqual(rbc(e,'main','metadata'),[('cw_source','subject'),('creation_date','subject'),('modification_date','subject'),('created_by','subject'),('owned_by','subject'),])self.assertCountEqual(rbc(e,'main','relations'),[('travaille','subject'),('manager','object'),('connait','object'),])self.assertListEqual(rbc(e,'main','hidden'),[])deftest_edition_form(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X LIMIT 1')form=self.vreg['forms'].select('edition',req,rset=rset,row=0,col=0)# should be also selectable by specifying entityself.vreg['forms'].select('edition',req,entity=rset.get_entity(0,0))self.assertFalse(any(fforfinform.fieldsiffisNone))deftest_edition_form_with_action(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X LIMIT 1')form=self.vreg['forms'].select('edition',req,rset=rset,row=0,col=0,action='my_custom_action')self.assertEqual(form.form_action(),'my_custom_action')deftest_attribute_add_permissions(self):# https://www.cubicweb.org/ticket/4342844withself.admin_access.repo_cnx()ascnx:self.create_user(cnx,u'toto')cnx.commit()withself.new_access(u'toto').web_request()asreq:e=self.vreg['etypes'].etype_class('Personne')(req)cform=self.vreg['forms'].select('edition',req,entity=e)self.assertIn('sexe',[rschema.typeforrschema,_incform.editable_attributes()])withself.new_access(u'toto').repo_cnx()ascnx:person_eid=cnx.create_entity('Personne',nom=u'Robert').eidcnx.commit()person=req.entity_from_eid(person_eid)mform=self.vreg['forms'].select('edition',req,entity=person)self.assertNotIn('sexe',[rschema.typeforrschema,_inmform.editable_attributes()])deftest_inlined_relations(self):withself.admin_access.web_request()asreq:withself.temporary_permissions(EmailAddress={'add':()}):autoform=self.vreg['forms'].select('edition',req,entity=req.user)self.assertEqual(list(autoform.inlined_form_views()),[])deftest_check_inlined_rdef_permissions(self):# try to check permissions when creating an entity ('user' below is a# fresh entity without an eid)withself.admin_access.web_request()asreq:ttype='EmailAddress'rschema=self.schema['use_email']rdef=rschema.rdefs[('CWUser',ttype)]tschema=self.schema[ttype]role='subject'withself.temporary_permissions((rdef,{'add':()})):user=self.vreg['etypes'].etype_class('CWUser')(req)autoform=self.vreg['forms'].select('edition',req,entity=user)self.assertFalse(autoform.check_inlined_rdef_permissions(rschema,role,tschema,ttype))# we actually don't care about the actual expression,# may_have_permission only checks the presence of such expressionsexpr=RRQLExpression('S use_email O')withself.temporary_permissions((rdef,{'add':(expr,)})):user=self.vreg['etypes'].etype_class('CWUser')(req)autoform=self.vreg['forms'].select('edition',req,entity=user)self.assertTrue(autoform.check_inlined_rdef_permissions(rschema,role,tschema,ttype))classFormViewsTC(CubicWebTC):deftest_delete_conf_formview(self):withself.admin_access.web_request()asreq:rset=req.execute('CWGroup X')self.view('deleteconf',rset,template=None,req=req).sourcedeftest_automatic_edition_formview(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X')self.view('edition',rset,row=0,template=None,req=req).sourcedeftest_automatic_edition_copyformview(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X')self.view('copy',rset,row=0,template=None,req=req).sourcedeftest_automatic_creation_formview(self):withself.admin_access.web_request()asreq:self.view('creation',None,etype='CWUser',template=None,req=req).sourcedeftest_automatic_muledit_formview(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X')self.view('muledit',rset,template=None,req=req).sourcedeftest_automatic_reledit_formview(self):withself.admin_access.web_request()asreq:rset=req.execute('CWUser X')self.view('reledit',rset,row=0,rtype='login',template=None,req=req).sourcedeftest_automatic_inline_edit_formview(self):withself.admin_access.web_request()asreq:geid=req.execute('CWGroup X LIMIT 1')[0][0]rset=req.execute('CWUser X LIMIT 1')self.view('inline-edition',rset,row=0,col=0,rtype='in_group',peid=geid,role='object',i18nctx='',pform=MOCKPFORM,template=None,req=req).sourcedeftest_automatic_inline_creation_formview(self):withself.admin_access.web_request()asreq:geid=req.execute('CWGroup X LIMIT 1')[0][0]self.view('inline-creation',None,etype='CWUser',rtype='in_group',peid=geid,petype='CWGroup',i18nctx='',role='object',pform=MOCKPFORM,template=None,req=req)MOCKPFORM=mock_object(form_previous_values={},form_valerror=None)if__name__=='__main__':unittest_main()