[CWEP002 migration] support sync_schema_props_perms for computed attribute
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 28 Aug 2014 17:41:34 +0200
changeset 9971 5e44dd9dde6b
parent 9970 671bbfed459b
child 9972 e210f0e082b0
[CWEP002 migration] support sync_schema_props_perms for computed attribute
hooks/syncschema.py
server/test/datacomputed/migratedapp/schema.py
server/test/datacomputed/schema.py
server/test/unittest_migractions.py
--- a/hooks/syncschema.py	Tue Jul 22 11:00:21 2014 +0200
+++ b/hooks/syncschema.py	Thu Aug 28 17:41:34 2014 +0200
@@ -647,6 +647,8 @@
             self.null_allowed_changed = True
         if 'fulltextindexed' in self.values:
             UpdateFTIndexOp.get_instance(cnx).add_data(rdef.subject)
+        if 'formula' in self.values:
+            RecomputeAttributeOperation.get_instance(cnx).add_data(rdef)
 
     def revertprecommit_event(self):
         if self.rdef is None:
--- a/server/test/datacomputed/migratedapp/schema.py	Tue Jul 22 11:00:21 2014 +0200
+++ b/server/test/datacomputed/migratedapp/schema.py	Thu Aug 28 17:41:34 2014 +0200
@@ -40,10 +40,12 @@
 
 class Company(EntityType):
     score = Float(formula='Any AVG(NN) WHERE X employees E, N concerns E, N note NN')
+    score100 = Float(formula='Any AVG(NN) WHERE X employees E, N concerns E, N note100 NN')
 
 
 class Note(EntityType):
     note = Int()
+    note100 = Int(formula='Any N*100 WHERE X note N')
 
 
 class concerns(RelationDefinition):
--- a/server/test/datacomputed/schema.py	Tue Jul 22 11:00:21 2014 +0200
+++ b/server/test/datacomputed/schema.py	Thu Aug 28 17:41:34 2014 +0200
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 
-from yams.buildobjs import EntityType, RelationDefinition, ComputedRelation, Int
+from yams.buildobjs import EntityType, RelationDefinition, ComputedRelation, Int, Float
 
 
 class Employee(EntityType):
@@ -34,11 +34,12 @@
 
 
 class Company(EntityType):
-    pass
+    score100 = Float(formula='Any AVG(NN) WHERE X employees E, N concerns E, N note100 NN')
 
 class Note(EntityType):
     note = Int()
     note20 = Int(formula='Any N*20 WHERE X note N')
+    note100 = Int(formula='Any N*20 WHERE X note N')
 
 class concerns(RelationDefinition):
     subject = 'Note'
--- a/server/test/unittest_migractions.py	Tue Jul 22 11:00:21 2014 +0200
+++ b/server/test/unittest_migractions.py	Thu Aug 28 17:41:34 2014 +0200
@@ -824,6 +824,22 @@
             mh.cmd_drop_attribute('Note', 'note20')
         self.assert_computed_attribute_dropped()
 
+    def test_computed_attribute_sync_schema_props_perms_rtype(self):
+        self.assertIn('note100', self.schema)
+        with self.mh() as (cnx, mh):
+            mh.cmd_sync_schema_props_perms('note100')
+        self.assertEqual(self.schema['note100'].rdefs['Note', 'Int'].formula,
+                         'Any N*100 WHERE X note N')
+
+    def test_computed_attribute_sync_schema_props_perms_rdef(self):
+        self.setup_add_score()
+        with self.mh() as (cnx, mh):
+            mh.cmd_sync_schema_props_perms(('Note', 'note100', 'Int'))
+            self.assertEqual([[200], [400]],
+                             cnx.execute('Any N ORDERBY N WHERE X note100 N').rows)
+            self.assertEqual([[300]],
+                             cnx.execute('Any CS WHERE C score100 CS, C is Company').rows)
+
 
 if __name__ == '__main__':
     unittest_main()