goa/test/unittest_rql.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1398 5fe84a5f7035
child 1977 606923dff11b
--- a/goa/test/unittest_rql.py	Thu May 14 10:24:56 2009 +0200
+++ b/goa/test/unittest_rql.py	Thu May 14 11:38:40 2009 +0200
@@ -32,7 +32,7 @@
 
 # end stored procedure definition #############################################
 
-class Article(db.Model):        
+class Article(db.Model):
     content = db.TextProperty()
     synopsis = db.StringProperty(default=u'hello')
 
@@ -40,14 +40,14 @@
     diem = db.DateProperty(required=True, auto_now_add=True)
     content = db.TextProperty()
     itemtype = db.StringProperty(required=True, choices=(u'personal', u'business'))
-    talks_about = db.ReferenceProperty(Article) 
-    cites = db.SelfReferenceProperty() 
+    talks_about = db.ReferenceProperty(Article)
+    cites = db.SelfReferenceProperty()
     data = db.BlobProperty()
 
-    
+
 class RQLTest(GAEBasedTC):
     MODEL_CLASSES = (Article, Blog)
-    
+
     def setUp(self):
         GAEBasedTC.setUp(self)
         # hack to make talks_about cardinality to ** instead of ?*
@@ -59,13 +59,13 @@
         self.execute('SET X talks_about Y WHERE X eid %(x)s, Y eid %(y)s',
                      {'x': self.blog.eid, 'y': self.article.eid})
         self.commit()
-        
+
     def _check_rset_size(self, rset, row, col):
         self.assertEquals(len(rset), row)
         self.assertEquals(len(rset[0]), col)
         self.assertEquals(len(rset.description), row)
         self.assertEquals(len(rset.description[0]), col)
-        
+
     def _check_blog_rset(self, rset):
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset.description[0][0], 'Blog')
@@ -121,20 +121,20 @@
         self.assertEquals(len(rset), 2)
         self.assertEquals(rset.description, [('Blog',), ('Blog',)])
 
-        
+
     def test_2_attribute_selection_1(self):
         rset = self.req.execute('Any X,D,C WHERE X is Blog, X diem D, X content C')
         self._check_rset_size(rset, 1, 3)
         self.assertEquals(rset[0], [self.blog.eid, today(), u'hop'])
         self.assertEquals(rset.description[0], ('Blog', 'Date', 'String'))
         self.assertIsInstance(rset[0][1], DateTimeType)
-        
+
     def test_2_attribute_selection_2(self):
         rset = self.req.execute('Any D,C WHERE X is Blog, X diem D, X content C')
         self._check_rset_size(rset, 1, 2)
         self.assertEquals(rset[0], [today(), u'hop'])
         self.assertEquals(rset.description[0], ('Date', 'String'))
-        
+
     def test_2_attribute_selection_binary(self):
         rset = self.req.execute('Any D WHERE X is Blog, X data D')
         self._check_rset_size(rset, 1, 1)
@@ -147,19 +147,19 @@
         self.assertIsInstance(rset[0][0], Binary)
         value = rset[0][0].getvalue()
         self.assertIsInstance(value, str)
-        self.failIf(isinstance(value, Blob)) 
+        self.failIf(isinstance(value, Blob))
         self.assertEquals(value, 'raw data')
         self.assertEquals(rset.description[0], ('Bytes',))
-        
+
     def test_2_attribute_selection_long_text(self):
         self.blog['content'] = text = 'a'*501
         self.blog.put()
         rset = self.req.execute('Any C WHERE X is Blog, X content C')
         self._check_rset_size(rset, 1, 1)
         self.assertIsInstance(rset[0][0], unicode)
-        self.failIf(isinstance(rset[0][0], Text)) 
+        self.failIf(isinstance(rset[0][0], Text))
         self.assertEquals(rset[0][0], text)
-        
+
     def test_2_attribute_selection_transformation(self):
         rset = self.req.execute('Any X,UPPER(C) WHERE X is Blog, X content C')
         self._check_rset_size(rset, 1, 2)
@@ -172,15 +172,15 @@
         self._check_blog_rset(rset)
         rset = self.req.execute('Any X WHERE X itemtype "business"')
         self.assertEquals(len(rset), 0)
-        
+
     def test_3_ambigous_attribute_restriction_1(self):
         rset = self.req.execute('Any X WHERE X content "hello"')
         self.assertEquals(len(rset), 0)
-        
+
     def test_3_ambigous_attribute_restriction_2(self):
         rset = self.req.execute('Any X WHERE X content "hop"')
         self._check_blog_rset(rset)
-        
+
     def test_3_ambigous_attribute_restriction_3(self):
         article = Article(content=u'hop')
         article.put()
@@ -193,11 +193,11 @@
         rset = self.req.execute('Any X WHERE X eid %(x)s, X content "hola"',
                                 {'x': self.blog.eid})
         self.assertEquals(len(rset), 0)
-        
+
     def test_3_multiple_attribute_restriction(self):
         rset = self.req.execute('Any X WHERE X content "hop", X itemtype "personal"')
         self._check_blog_rset(rset)
-        
+
     def test_3_incoherant_multiple_attribute_restriction(self):
         rset = self.req.execute('Any X WHERE X content "hip", X itemtype "personal"')
         self.assertEquals(len(rset), 0)
@@ -234,7 +234,7 @@
         repo = self.config.repository()
         versions = repo.get_versions()
         self.assertEquals(versions.keys(), ['cubicweb'])
-    
+
     def _setup_relation_description(self):
         self.article2 = self.add_entity('Article', content=u'hop')
         self.blog2 = self.add_entity('Blog', itemtype=u'personal', content=u'hip')
@@ -242,7 +242,7 @@
                      {'x': self.blog2.eid, 'y': self.article2.eid})
         self.blog3 = self.add_entity('Blog', itemtype=u'business', content=u'hep')
         self.commit()
-        
+
     def test_4_relation_restriction_1(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X WHERE X talks_about Y')
@@ -250,7 +250,7 @@
         self.assertUnorderedIterableEquals([r[0] for r in rset],
                              [self.blog.eid, self.blog2.eid])
         self.assertUnorderedIterableEquals([r[0] for r in rset.description], ['Blog', 'Blog'])
-        
+
     def test_4_relation_restriction_2(self):
         self._setup_relation_description()
         rset = self.req.execute('Any Y WHERE X talks_about Y')
@@ -259,7 +259,7 @@
                              [self.article.eid, self.article2.eid])
         self.assertUnorderedIterableEquals([r[0] for r in rset.description],
                              ['Article', 'Article'])
-        
+
     def test_4_relation_restriction_3(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,Y WHERE X talks_about Y')
@@ -270,7 +270,7 @@
         self.assertUnorderedIterableEquals([tuple(r) for r in rset.description],
                              [('Blog', 'Article'),
                               ('Blog', 'Article')])
-        
+
     def test_4_relation_restriction_4(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,Y WHERE X talks_about Y, X eid %(x)s',
@@ -278,7 +278,7 @@
         self._check_rset_size(rset, 1, 2)
         self.assertEquals(rset[0], [self.blog.eid, self.article.eid])
         self.assertUnorderedIterableEquals(rset.description[0], ['Blog', 'Article'])
-        
+
     def test_4_relation_restriction_5(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,Y WHERE X talks_about Y, Y eid %(x)s',
@@ -286,7 +286,7 @@
         self._check_rset_size(rset, 1, 2)
         self.assertEquals(rset[0], [self.blog.eid, self.article.eid])
         self.assertUnorderedIterableEquals(rset.description[0], ['Blog', 'Article'])
-        
+
     def test_4_relation_subject_restriction(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,Y WHERE X talks_about Y, X content %(c)s',
@@ -294,7 +294,7 @@
         self._check_rset_size(rset, 1, 2)
         self.assertEquals(rset[0], [self.blog.eid, self.article.eid])
         self.assertUnorderedIterableEquals(rset.description[0], ['Blog', 'Article'])
-        
+
     def test_4_relation_object_restriction(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X WHERE X is Blog, X talks_about Y, Y content %(c)s',
@@ -302,7 +302,7 @@
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset[0], [self.blog.eid])
         self.assertUnorderedIterableEquals(rset.description[0], ['Blog'])
-        
+
     def test_4_relation_subject_object_restriction(self):
         article2 = self.add_entity('Article', content=u'very interesting')
         rset = self.req.execute('Any X,XC WHERE X is Blog, X content XC, X content %(xc)s, '
@@ -311,20 +311,20 @@
         self._check_rset_size(rset, 1, 2)
         self.assertEquals(rset[0], [self.blog.eid, self.blog.content])
         self.assertUnorderedIterableEquals(rset.description[0], ['Blog', 'String'])
-        
+
     def test_4_relation_subject_object_restriction_no_res(self):
         article2 = self.add_entity('Article', content=u'very interesting')
         rset = self.req.execute('Any X,XC WHERE X is Blog, X content XC, X content %(xc)s, '
                                 'X talks_about Y, Y content %(c)s',
                                 {'xc': 'hip', 'c': 'very interesting'})
         self.assertEquals(len(rset), 0)
-        
+
     def test_4_relation_subject_object_restriction_no_res_2(self):
         rset = self.req.execute('Any X,XC WHERE X is Blog, X content XC, X content %(xc)s, '
                                 'X talks_about Y, Y content %(c)s',
                                 {'xc': 'hop', 'c': 'not interesting'})
         self.assertEquals(len(rset), 0)
-        
+
     def test_4_relation_restriction_7(self):
         self._setup_relation_description()
         rset = self.req.execute('Any XC,XD,YC WHERE X talks_about Y, Y eid %(x)s,'
@@ -333,7 +333,7 @@
         self._check_rset_size(rset, 1, 3)
         self.assertEquals(rset[0], [self.blog.content, self.blog.diem, self.article.content])
         self.assertUnorderedIterableEquals(rset.description[0], ['String', 'Date', 'String'])
-        
+
     def test_4_relation_restriction_8(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,Y WHERE X cites Y, Y eid %(x)s', {'x': self.blog.eid})
@@ -346,7 +346,7 @@
         rset = self.req.execute('Any X,Y WHERE X talks_about Y, X eid %(x)s, Y eid %(y)s',
                                 {'x': self.blog.eid, 'y': article2.eid})
         self._check_rset_size(rset, 1, 2)
-        
+
     def test_4_ambiguous_subject_relation(self):
         ye = self.add_entity('YamsEntity')
         self.req.execute('SET X ambiguous_relation Y WHERE X eid %(x)s, Y eid %(y)s',
@@ -365,7 +365,7 @@
         self._check_rset_size(rset, 2, 1)
         self.assertUnorderedIterableEquals([r[0] for r in rset], [self.blog.eid, self.article.eid])
         self.assertUnorderedIterableEquals([r[0] for r in rset.description], ['Blog', 'Article'])
-        
+
     def test_4_relation_selection(self):
         req = self.request()
         rset = req.execute('Any N WHERE G content N, U talks_about G, U eid %(u)s', {'u': self.blog.eid})
@@ -381,7 +381,7 @@
                           [[self.blog3.eid, 'hep'],
                            [self.blog2.eid, 'hip'],
                            [self.blog.eid, 'hop']])
-                           
+
     def test_5_orderby_desc(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,XC ORDERBY XC DESC WHERE X is Blog, X content XC')
@@ -417,7 +417,7 @@
                           [[self.blog.eid, 'hop', 'personal'],
                            [self.blog2.eid, 'hip', 'personal'],
                            [self.blog3.eid, 'hep', 'business']])
-        
+
     def test_5_orderby_several_terms_mixed_order(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X,XC,XI ORDERBY XI ASC,XC DESC WHERE X is Blog, X content XC, X itemtype XI')
@@ -449,25 +449,25 @@
                                 'WHERE X is Blog, X itemtype XIT')
         self._check_rset_size(rset, 2, 1)
         self.assertEquals(rset.rows, [[self.blog.eid], [blog2.eid]])
-                          
-        
+
+
     def test_6_limit(self):
         self._setup_relation_description()
         rset = self.req.execute('Any X LIMIT 2 WHERE X is Blog')
         self._check_rset_size(rset, 2, 1)
-        
+
     def test_6_offset(self):
         self._setup_relation_description()
         rset = self.req.execute('Any XC ORDERBY XC DESC OFFSET 1 WHERE X is Blog, X content XC')
         self._check_rset_size(rset, 2, 1)
         self.assertEquals(rset.rows, [['hip'], ['hep']])
-        
+
     def test_6_limit_and_orderby(self):
         self._setup_relation_description()
         rset = self.req.execute('Any XC ORDERBY XC LIMIT 2 WHERE X is Blog, X content XC')
         self._check_rset_size(rset, 2, 1)
         self.assertEquals(rset.rows, [['hep'], ['hip']])
-        
+
     def test_6_limit_offset_and_orderby(self):
         self._setup_relation_description()
         rset = self.req.execute('Any XC ORDERBY XC LIMIT 2 OFFSET 0 WHERE X is Blog, X content XC')
@@ -481,7 +481,7 @@
         self.assertEquals(rset.rows, [['hop']])
         rset = self.req.execute('Any XC ORDERBY XC LIMIT 2 OFFSET 3 WHERE X is Blog, X content XC')
         self.failIf(rset)
-        
+
 
     def test_7_simple_datetimecast(self):
         self._setup_relation_description()
@@ -496,7 +496,7 @@
         rset = self.req.execute('Any X WHERE X is Blog, X creation_date <= "%s"'
                                 % _tomorrow.strftime('%Y-%m-%d'))
         self._check_rset_size(rset, 3, 1)
-        
+
     def test_7_identity_relation(self):
         rset = self.req.execute('Any X WHERE X identity Y, X eid %(x)s, Y eid %(y)s',
                                 {'x': self.user.eid, 'y': self.user.eid})
@@ -509,13 +509,13 @@
         rset = self.req.execute('Any X WHERE X identity Y, X eid %(x)s, Y eid %(y)s',
                                 {'x': self.blog.eid, 'y': blog2.eid})
         self.failIf(rset)
-        
+
     def test_8_not_relation_1(self):
         rset = self.req.execute('Any X WHERE X identity U, NOT U in_group G, '
                                 'G name "guests", X eid %(x)s, U eid %(u)s',
                                 {'x': self.user.eid, 'u': self.user.eid})
         self._check_rset_size(rset, 1, 1)
-        self.assertEquals(rset.rows, [[self.user.eid]])        
+        self.assertEquals(rset.rows, [[self.user.eid]])
 
     def test_8_not_relation_linked_subject(self):
         rset = self.req.execute('Any X WHERE NOT X talks_about Y, Y eid %(y)s',
@@ -524,7 +524,7 @@
         blog2 = self.add_entity('Blog', content=u'hop', itemtype=u'personal')
         self.commit()
         rset = self.req.execute('Any X WHERE NOT X talks_about Y, Y eid %(y)s',
-                                {'y': self.article.eid})        
+                                {'y': self.article.eid})
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset.rows, [[blog2.eid]])
 
@@ -541,7 +541,7 @@
 
     def test_8_not_relation_linked_attr(self):
         self.skip('not yet implemented')
-        # TODO: this should generated 
+        # TODO: this should generated
         # Query(X)[s_talks_about] > "hop" || Query(X)[s_talks_about] < "hop"
         article2 = self.add_entity('Article', content=u'hop')
         self.req.execute('SET X talks_about Y WHERE X eid %(x)s, Y eid %(y)s',
@@ -564,13 +564,13 @@
         rset = self.req.execute('Any Y WHERE NOT X talks_about Y')
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset.rows, [[article2.eid]])
-        
+
     def test_8_not_relation_final_1(self):
         rset = self.req.execute('Any G WHERE G is CWGroup, NOT G name "guests"')
         self._check_rset_size(rset, 2, 1)
         self.assertUnorderedIterableEquals([g.name for g in rset.entities()],
-                                           ['users', 'managers'])        
-        
+                                           ['users', 'managers'])
+
     def test_8_not_relation_final_2(self):
         rset = self.req.execute('Any GN WHERE G is CWGroup, NOT G name "guests", G name GN')
         self._check_rset_size(rset, 2, 1)
@@ -587,8 +587,8 @@
         rset = self.req.execute('Any X WHERE X is Blog, EXISTS(X talks_about Y)')
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset.rows, [[self.blog.eid]])
-        
-        
+
+
     def test_error_unknown_eid(self):
         rset = self.req.execute('Any X WHERE X eid %(x)s', {'x': '1234'})
         self.assertEquals(len(rset), 0)
@@ -603,6 +603,6 @@
         rset = self.execute('Any X WHERE Y inlined_relation X, Y eid %(y)s', {'y': eid})
         self._check_rset_size(rset, 1, 1)
         self.assertEquals(rset[0][0], self.blog.eid)
-        
+
 if __name__ == '__main__':
     unittest_main()