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.

101 lines
4.0 KiB

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