You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from openerp.osv import expression
  4. from openerp.tests.common import TransactionCase, at_install, post_install
  5. @at_install(False)
  6. @post_install(True)
  7. class QueryGenerationCase(TransactionCase):
  8. def setUp(self):
  9. super(QueryGenerationCase, self).setUp()
  10. self.ResPartner = self.env['res.partner']
  11. self.TrgmIndex = self.env['trgm.index']
  12. self.ResPartnerCategory = self.env['res.partner.category']
  13. def test_fuzzy_where_generation(self):
  14. """Check the generation of the where clause."""
  15. # the added fuzzy search operator should be available in the allowed
  16. # operators
  17. self.assertIn('%', expression.TERM_OPERATORS)
  18. # create new query with fuzzy search operator
  19. query = self.ResPartner._where_calc(
  20. [('name', '%', 'test')], active_test=False)
  21. from_clause, where_clause, where_clause_params = query.get_sql()
  22. # the % parameter has to be escaped (%%) for the string replation
  23. self.assertEqual(where_clause, """("res_partner"."name" %% %s)""")
  24. # test the right sql query statement creation
  25. # now there should be only one '%'
  26. complete_where = self.env.cr.mogrify(
  27. "SELECT FROM %s WHERE %s" % (from_clause, where_clause),
  28. where_clause_params)
  29. self.assertEqual(
  30. complete_where,
  31. 'SELECT FROM "res_partner" WHERE '
  32. '("res_partner"."name" % \'test\')')
  33. def test_fuzzy_where_generation_translatable(self):
  34. """Check the generation of the where clause for translatable fields."""
  35. ctx = {'lang': 'de_DE'}
  36. # create new query with fuzzy search operator
  37. query = self.ResPartnerCategory.with_context(ctx)\
  38. ._where_calc([('name', '%', 'Goschaeftlic')], active_test=False)
  39. from_clause, where_clause, where_clause_params = query.get_sql()
  40. # the % parameter has to be escaped (%%) for the string replation
  41. self.assertIn("""SELECT id FROM temp_irt_current WHERE name %% %s""",
  42. where_clause)
  43. complete_where = self.env.cr.mogrify(
  44. "SELECT FROM %s WHERE %s" % (from_clause, where_clause),
  45. where_clause_params)
  46. self.assertIn(
  47. """SELECT id FROM temp_irt_current WHERE name % 'Goschaeftlic'""",
  48. complete_where)
  49. def test_fuzzy_order_generation(self):
  50. """Check the generation of the where clause."""
  51. order = "similarity(%s.name, 'test') DESC" % self.ResPartner._table
  52. query = self.ResPartner._where_calc(
  53. [('name', '%', 'test')], active_test=False)
  54. order_by = self.ResPartner._generate_order_by(order, query)
  55. self.assertEqual(' ORDER BY %s' % order, order_by)
  56. def test_fuzzy_search(self):
  57. """Test the fuzzy search itself."""
  58. if self.TrgmIndex._trgm_extension_exists() != 'installed':
  59. return
  60. if not self.TrgmIndex.index_exists('res.partner', 'name'):
  61. field_partner_name = self.env.ref('base.field_res_partner_name')
  62. self.TrgmIndex.create({
  63. 'field_id': field_partner_name.id,
  64. 'index_type': 'gin',
  65. })
  66. partner1 = self.ResPartner.create({
  67. 'name': 'John Smith'
  68. })
  69. partner2 = self.ResPartner.create(
  70. {'name': 'John Smizz'}
  71. )
  72. partner3 = self.ResPartner.create({
  73. 'name': 'Linus Torvalds'
  74. })
  75. res = self.ResPartner.search([('name', '%', 'Jon Smith')])
  76. self.assertIn(partner1.id, res.ids)
  77. self.assertIn(partner2.id, res.ids)
  78. self.assertNotIn(partner3.id, res.ids)
  79. res = self.ResPartner.search([('name', '%', 'Smith John')])
  80. self.assertIn(partner1.id, res.ids)
  81. self.assertIn(partner2.id, res.ids)
  82. self.assertNotIn(partner3.id, res.ids)