refactor and don't crash on error while generating a cube's catalogs
fromcubicweb.devtools.apptestimportEnvBasedTCfromcubicweb.web.facetimportinsert_attr_select_relation,prepare_facets_rqlstclassInsertAttrRelationTC(EnvBasedTC):defparse(self,query):rqlst=self.vreg.parse(self.session,query)select=rqlst.children[0]# XXX done in real life?select.remove_groups()returnrqlstdef_generate(self,rqlst,rel,role,attr):mainvar=prepare_facets_rqlst(rqlst)[0]insert_attr_select_relation(rqlst.children[0],mainvar,rel,role,attr)returnrqlst.as_string()@propertydefselect(self):returnself.parse('Any B,(NOW - CD),S,V,U,GROUP_CONCAT(TN),VN,P,CD,BMD ''GROUPBY B,CD,S,V,U,VN,P,BMD ''WHERE B in_state S, B creation_date CD, ''B modification_date BMD, T? tags B, T name TN, ''V? bookmarked_by B, V title VN, B created_by U?, ''B in_group P, P name "managers"')deftest_1(self):self.assertEquals(self._generate(self.select,'in_state','subject','name'),"DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name 'managers', ""B in_state A, A name C, B is CWUser")deftest_2(self):self.assertEquals(self._generate(self.select,'tags','object','name'),"DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name 'managers', ""A tags B, A name C, B is CWUser")deftest_3(self):self.assertEquals(self._generate(self.select,'created_by','subject','login'),"DISTINCT Any A,C ORDERBY C WHERE B in_group P, P name 'managers', ""B created_by A, A login C, B is CWUser")deftest_4(self):self.assertEquals(self._generate(self.parse('Any X WHERE X is CWUser'),'created_by','subject','login'),"DISTINCT Any A,B ORDERBY B WHERE X is CWUser, X created_by A, A login B")deftest_5(self):self.assertEquals(self._generate(self.parse('Any X,L WHERE X is CWUser, X login L'),'created_by','subject','login'),"DISTINCT Any A,B ORDERBY B WHERE X is CWUser, X created_by A, A login B")deftest_nonregr1(self):select=self.parse('Any T,V WHERE T bookmarked_by V?, ''V in_state VS, VS name "published", T created_by U')self.assertEquals(self._generate(select,'created_by','subject','login'),"DISTINCT Any A,B ORDERBY B WHERE T created_by U, ""T created_by A, A login B, T is Bookmark")deftest_nonregr2(self):#'DISTINCT Any X,TMP,N WHERE P name TMP, X version_of P, P is Project, X is Version, not X in_state S,S name "published", X num N ORDERBY TMP,N'select=self.parse('DISTINCT Any V,TN,L ORDERBY TN,L WHERE T nom TN, V connait T, T is Personne, V is CWUser,''NOT V in_state VS, VS name "published", V login L')rschema=self.schema['connait']fors,oinrschema.iter_rdefs():rschema.set_rproperty(s,o,'cardinality','++')try:self.assertEquals(self._generate(select,'in_state','subject','name'),"DISTINCT Any A,B ORDERBY B WHERE V is CWUser, ""NOT V in_state VS, VS name 'published', ""V in_state A, A name B")finally:fors,oinrschema.iter_rdefs():rschema.set_rproperty(s,o,'cardinality','**')deftest_nonregr3(self):#'DISTINCT Any X,TMP,N WHERE P name TMP, X version_of P, P is Project, X is Version, not X in_state S,S name "published", X num N ORDERBY TMP,N'select=self.parse('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is CWUser, Y is Bookmark, X in_group A')self.assertEquals(self._generate(select,'in_group','subject','name'),"DISTINCT Any B,C ORDERBY C WHERE X is CWUser, X in_group B, B name C")if__name__=='__main__':fromlogilab.common.testlibimportunittest_mainunittest_main()