[tests] Don't import QUnitTestCase into tests' global namespace
Unittest's test loader will try to execute QUnitTestCase's test_javascripts method. However this class is intended to be subclassed, not to be executed directly.
This change has the following consequences:
* slightly faster test execution
* almost all rtags warnings are gone
# copyright 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/>."""Hooks for synchronizing computed attributes"""__docformat__="restructuredtext en"fromcubicwebimport_fromcollectionsimportdefaultdictfromrqlimportnodesfromcubicweb.serverimporthookclassRecomputeAttributeOperation(hook.DataOperationMixIn,hook.Operation):"""Operation to recompute caches of computed attribute at commit time, depending on what's have been modified in the transaction and avoiding to recompute twice the same attribute """containercls=dictdefadd_data(self,computed_attribute,eid=None):try:self._container[computed_attribute].add(eid)exceptKeyError:self._container[computed_attribute]=set((eid,))defprecommit_event(self):forcomputed_attribute_rdef,eidsinself.get_data().items():attr=computed_attribute_rdef.rtypeformula=computed_attribute_rdef.formulaselect=self.cnx.repo.vreg.rqlhelper.parse(formula).children[0]xvar=select.get_variable('X')select.add_selected(xvar,index=0)select.add_group_var(xvar,index=0)ifNoneineids:select.add_type_restriction(xvar,computed_attribute_rdef.subject)else:select.add_eid_restriction(xvar,eids)update_rql='SET X %s%%(value)s WHERE X eid %%(x)s'%attrforeid,valueinself.cnx.execute(select.as_string()):self.cnx.execute(update_rql,{'value':value,'x':eid})classEntityWithCACreatedHook(hook.Hook):"""When creating an entity that has some computed attribute, those attributes have to be computed. Concret class of this hook are generated at registration time by introspecting the schema. """__abstract__=Trueevents=('after_add_entity',)# list of computed attribute rdefs that have to be recomputedcomputed_attributes=Nonedef__call__(self):forrdefinself.computed_attributes:RecomputeAttributeOperation.get_instance(self._cw).add_data(rdef,self.entity.eid)classRelationInvolvedInCAModifiedHook(hook.Hook):"""When some relation used in a computed attribute is updated, those attributes have to be recomputed. Concret class of this hook are generated at registration time by introspecting the schema. """__abstract__=Trueevents=('after_add_relation','before_delete_relation')# list of (computed attribute rdef, optimize_on) that have to be recomputedoptimized_computed_attributes=Nonedef__call__(self):forrdef,optimize_oninself.optimized_computed_attributes:ifoptimize_onisNone:eid=Noneelse:eid=getattr(self,optimize_on)RecomputeAttributeOperation.get_instance(self._cw).add_data(rdef,eid)classAttributeInvolvedInCAModifiedHook(hook.Hook):"""When some attribute used in a computed attribute is updated, those attributes have to be recomputed. Concret class of this hook are generated at registration time by introspecting the schema. """__abstract__=Trueevents=('after_update_entity',)# list of (computed attribute rdef, attributes of this entity type involved)# that may have to be recomputedattributes_computed_attributes=Nonedef__call__(self):edited_attributes=frozenset(self.entity.cw_edited)forrdef,used_attributesinself.attributes_computed_attributes.items():ifedited_attributes.intersection(used_attributes):# XXX optimize if the modified attributes belong to the same# entity as the computed attributeRecomputeAttributeOperation.get_instance(self._cw).add_data(rdef)# code generation at registration time #########################################def_optimize_on(formula_select,rtype):"""Given a formula and some rtype, tells whether on update of the given relation, formula may be recomputed only for rhe relation's subject ('eidfrom' returned), object ('eidto' returned) or None. Optimizing is only possible when X is used as direct subject/object of this relation, else we may miss some necessary update. """forrelinformula_select.get_nodes(nodes.Relation):ifrel.r_type==rtype:sub=rel.get_variable_parts()[0]obj=rel.get_variable_parts()[1]ifsub.name=='X':return'eidfrom'elifobj.name=='X':return'eidto'else:returnNoneclass_FormulaDependenciesMatrix(object):"""This class computes and represents the dependencies of computed attributes towards relations and attributes """def__init__(self,schema):"""Analyzes the schema to compute the dependencies"""# entity types holding some computed attribute {etype: [computed rdefs]}self.computed_attribute_by_etype=defaultdict(list)# depending entity types {dep. etype: {computed rdef: dep. etype attributes}}self.computed_attribute_by_etype_attrs=defaultdict(lambda:defaultdict(set))# depending relations def {dep. rdef: [computed rdefs]self.computed_attribute_by_relation=defaultdict(list)# by rdef# Walk through all attributes definitionsforrdefinschema.iter_computed_attributes():self.computed_attribute_by_etype[rdef.subject.type].append(rdef)# extract the relations it depends upon - `rdef.formula_select` is# expected to have been set by finalize_computed_attributesselect=rdef.formula_selectforrel_nodeinselect.get_nodes(nodes.Relation):ifrel_node.is_types_restriction():continuerschema=schema.rschema(rel_node.r_type)lhs,rhs=rel_node.get_variable_parts()forsolinselect.solutions:subject_etype=sol[lhs.name]ifisinstance(rhs,nodes.VariableRef):object_etypes=set(sol[rhs.name]forsolinselect.solutions)else:object_etypes=rschema.objects(subject_etype)forobject_etypeinobject_etypes:ifrschema.final:attr_for_computations=self.computed_attribute_by_etype_attrs[subject_etype]attr_for_computations[rdef].add(rschema.type)else:depend_on_rdef=rschema.rdefs[subject_etype,object_etype]self.computed_attribute_by_relation[depend_on_rdef].append(rdef)defgenerate_entity_creation_hooks(self):foretype,computed_attributesinself.computed_attribute_by_etype.items():regid='computed_attribute.%s_created'%etypeselector=hook.is_instance(etype)yieldtype('%sCreatedHook'%etype,(EntityWithCACreatedHook,),{'__regid__':regid,'__select__':hook.Hook.__select__&selector,'computed_attributes':computed_attributes})defgenerate_relation_change_hooks(self):forrdef,computed_attributesinself.computed_attribute_by_relation.items():regid='computed_attribute.%s_modified'%rdef.rtypeselector=hook.match_rtype(rdef.rtype.type,frometypes=(rdef.subject.type,),toetypes=(rdef.object.type,))optimized_computed_attributes=[]forcomputed_rdefincomputed_attributes:optimized_computed_attributes.append((computed_rdef,_optimize_on(computed_rdef.formula_select,rdef.rtype)))yieldtype('%sModifiedHook'%rdef.rtype,(RelationInvolvedInCAModifiedHook,),{'__regid__':regid,'__select__':hook.Hook.__select__&selector,'optimized_computed_attributes':optimized_computed_attributes})defgenerate_entity_update_hooks(self):foretype,attributes_computed_attributesinself.computed_attribute_by_etype_attrs.items():regid='computed_attribute.%s_updated'%etypeselector=hook.is_instance(etype)yieldtype('%sModifiedHook'%etype,(AttributeInvolvedInCAModifiedHook,),{'__regid__':regid,'__select__':hook.Hook.__select__&selector,'attributes_computed_attributes':attributes_computed_attributes})defregistration_callback(vreg):vreg.register_all(globals().values(),__name__)dependencies=_FormulaDependenciesMatrix(vreg.schema)forhook_classindependencies.generate_entity_creation_hooks():vreg.register(hook_class)forhook_classindependencies.generate_relation_change_hooks():vreg.register(hook_class)forhook_classindependencies.generate_entity_update_hooks():vreg.register(hook_class)