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.

102 lines
4.0 KiB

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