web/test/unittest_magicsearch.py
changeset 8505 dcd9bc1d1bca
parent 6340 470d8e828fda
child 8510 e2913c9880a0
equal deleted inserted replaced
8496:e4d71fc0b701 8505:dcd9bc1d1bca
   228                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"')
   228                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"')
   229         rset = self.proc.process_query(u'text: utilisateur Smith')
   229         rset = self.proc.process_query(u'text: utilisateur Smith')
   230         self.assertEqual(rset.rql, 'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s')
   230         self.assertEqual(rset.rql, 'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s')
   231         self.assertEqual(rset.args, {'text': u'utilisateur Smith'})
   231         self.assertEqual(rset.args, {'text': u'utilisateur Smith'})
   232 
   232 
       
   233 
       
   234 class RQLSuggestionsBuilderTC(CubicWebTC):
       
   235     def suggestions(self, rql):
       
   236         req = self.request()
       
   237         rbs = self.vreg['components'].select('rql.suggestions', req)
       
   238         rbs.attr_value_limit = 10 # limit to 10 to ease vocabulry tests
       
   239         return rbs.build_suggestions(rql)
       
   240 
       
   241     def test_no_restrictions_rql(self):
       
   242         self.assertListEqual([], self.suggestions(''))
       
   243         self.assertListEqual([], self.suggestions('An'))
       
   244         self.assertListEqual([], self.suggestions('Any X'))
       
   245         self.assertListEqual([], self.suggestions('Any X, Y'))
       
   246 
       
   247     def test_invalid_rql(self):
       
   248         self.assertListEqual([], self.suggestions('blabla'))
       
   249         self.assertListEqual([], self.suggestions('Any X WHERE foo, bar'))
       
   250 
       
   251     def test_is_rql(self):
       
   252         self.assertListEqual(['Any X WHERE X is %s' % eschema
       
   253                               for eschema in sorted(self.vreg.schema.entities())
       
   254                               if not eschema.final],
       
   255                              self.suggestions('Any X WHERE X is'))
       
   256 
       
   257         self.assertListEqual(['Any X WHERE X is Personne', 'Any X WHERE X is Project'],
       
   258                              self.suggestions('Any X WHERE X is P'))
       
   259 
       
   260         self.assertListEqual(['Any X WHERE X is Personne, Y is Personne',
       
   261                               'Any X WHERE X is Personne, Y is Project'],
       
   262                              self.suggestions('Any X WHERE X is Personne, Y is P'))
       
   263 
       
   264 
       
   265     def test_relations_rql(self):
       
   266         self.assertListEqual(['Any X WHERE X is Personne, X ass A',
       
   267                               'Any X WHERE X is Personne, X datenaiss A',
       
   268                               'Any X WHERE X is Personne, X description A',
       
   269                               'Any X WHERE X is Personne, X fax A',
       
   270                               'Any X WHERE X is Personne, X nom A',
       
   271                               'Any X WHERE X is Personne, X prenom A',
       
   272                               'Any X WHERE X is Personne, X promo A',
       
   273                               'Any X WHERE X is Personne, X salary A',
       
   274                               'Any X WHERE X is Personne, X sexe A',
       
   275                               'Any X WHERE X is Personne, X tel A',
       
   276                               'Any X WHERE X is Personne, X test A',
       
   277                               'Any X WHERE X is Personne, X titre A',
       
   278                               'Any X WHERE X is Personne, X travaille A',
       
   279                               'Any X WHERE X is Personne, X web A',
       
   280                               ],
       
   281                              self.suggestions('Any X WHERE X is Personne, X '))
       
   282         self.assertListEqual(['Any X WHERE X is Personne, X tel A',
       
   283                               'Any X WHERE X is Personne, X test A',
       
   284                               'Any X WHERE X is Personne, X titre A',
       
   285                               'Any X WHERE X is Personne, X travaille A',
       
   286                               ],
       
   287                              self.suggestions('Any X WHERE X is Personne, X t'))
       
   288         # try completion on selected
       
   289         self.assertListEqual(['Any X WHERE X is Personne, Y is Societe, X tel A',
       
   290                               'Any X WHERE X is Personne, Y is Societe, X test A',
       
   291                               'Any X WHERE X is Personne, Y is Societe, X titre A',
       
   292                               'Any X WHERE X is Personne, Y is Societe, X travaille Y',
       
   293                               ],
       
   294                              self.suggestions('Any X WHERE X is Personne, Y is Societe, X t'))
       
   295         # invalid relation should not break
       
   296         self.assertListEqual([],
       
   297                              self.suggestions('Any X WHERE X is Personne, X asdasd'))
       
   298 
       
   299     def test_attribute_vocabulary_rql(self):
       
   300         self.assertListEqual(['Any X WHERE X is Personne, X promo "bon"',
       
   301                               'Any X WHERE X is Personne, X promo "pasbon"',
       
   302                               ],
       
   303                              self.suggestions('Any X WHERE X is Personne, X promo "'))
       
   304         self.assertListEqual(['Any X WHERE X is Personne, X promo "pasbon"',
       
   305                               ],
       
   306                              self.suggestions('Any X WHERE X is Personne, X promo "p'))
       
   307         # "bon" should be considered complete, hence no suggestion
       
   308         self.assertListEqual([],
       
   309                              self.suggestions('Any X WHERE X is Personne, X promo "bon"'))
       
   310         # no valid vocabulary starts with "po"
       
   311         self.assertListEqual([],
       
   312                              self.suggestions('Any X WHERE X is Personne, X promo "po'))
       
   313 
       
   314     def test_attribute_value_rql(self):
       
   315         # suggestions should contain any possible value for
       
   316         # a given attribute (limited to 10)
       
   317         req = self.request()
       
   318         for i in xrange(15):
       
   319             req.create_entity('Personne', nom=u'n%s' % i, prenom=u'p%s' % i)
       
   320         self.assertListEqual(['Any X WHERE X is Personne, X nom "n0"',
       
   321                               'Any X WHERE X is Personne, X nom "n1"',
       
   322                               'Any X WHERE X is Personne, X nom "n2"',
       
   323                               'Any X WHERE X is Personne, X nom "n3"',
       
   324                               'Any X WHERE X is Personne, X nom "n4"',
       
   325                               'Any X WHERE X is Personne, X nom "n5"',
       
   326                               'Any X WHERE X is Personne, X nom "n6"',
       
   327                               'Any X WHERE X is Personne, X nom "n7"',
       
   328                               'Any X WHERE X is Personne, X nom "n8"',
       
   329                               'Any X WHERE X is Personne, X nom "n9"',
       
   330                               ],
       
   331                              self.suggestions('Any X WHERE X is Personne, X nom "'))
       
   332         self.assertListEqual(['Any X WHERE X is Personne, X nom "n1"',
       
   333                               'Any X WHERE X is Personne, X nom "n10"',
       
   334                               'Any X WHERE X is Personne, X nom "n11"',
       
   335                               'Any X WHERE X is Personne, X nom "n12"',
       
   336                               'Any X WHERE X is Personne, X nom "n13"',
       
   337                               'Any X WHERE X is Personne, X nom "n14"',
       
   338                               ],
       
   339                              self.suggestions('Any X WHERE X is Personne, X nom "n1'))
       
   340 
       
   341 
   233 if __name__ == '__main__':
   342 if __name__ == '__main__':
   234     unittest_main()
   343     unittest_main()