test_utm.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.utm.tests.common import TestUTMCommon
  4. class TestUtm(TestUTMCommon):
  5. def test_find_or_create_record(self):
  6. source_1, source_2 = self.env['utm.source'].create([{
  7. 'name': 'Source 1',
  8. }, {
  9. 'name': 'Source 2',
  10. }])
  11. # Find the record based on the given name
  12. source = self.env['utm.mixin']._find_or_create_record('utm.source', 'Source 1')
  13. self.assertEqual(source, source_1)
  14. # Create a new record
  15. source_4 = self.env['utm.mixin']._find_or_create_record('utm.source', 'Source 3')
  16. self.assertNotIn(source_4, source_1 | source_2)
  17. self.assertEqual(source_4.name, 'Source 3')
  18. def test_name_generation(self):
  19. """Test that the name is always unique.
  20. A counter must be added at the end of the name if it's not the case.
  21. """
  22. for utm_model in ('utm.source', 'utm.medium', 'utm.campaign'):
  23. utm_0 = self.env[utm_model].create({'name': 'UTM dup'})
  24. utm_1, utm_2, utm_3, utm_4, utm_5 = self.env[utm_model].create([{
  25. 'name': 'UTM 1',
  26. }, {
  27. 'name': 'UTM 2',
  28. }, {
  29. 'name': 'UTM dup',
  30. }, {
  31. # UTM record 4 has the same name of the previous UTM record
  32. 'name': 'UTM dup',
  33. }, {
  34. # UTM record 5 has the same name of the previous UTM record
  35. # but with a wrong counter part, it will be removed and updated
  36. 'name': 'UTM dup [0]',
  37. }])
  38. self.assertEqual(utm_0.name, 'UTM dup', msg='The first "UTM dup" should be left unchanged since it is unique')
  39. self.assertEqual(utm_1.name, 'UTM 1', msg='This name is already unique')
  40. self.assertEqual(utm_2.name, 'UTM 2', msg='This name is already unique')
  41. self.assertEqual(utm_3.name, 'UTM dup [2]', msg='Must add a counter as suffix to ensure uniqueness')
  42. self.assertEqual(utm_4.name, 'UTM dup [3]', msg='Must add a counter as suffix to ensure uniqueness')
  43. self.assertEqual(utm_5.name, 'UTM dup [4]', msg='Must add a counter as suffix to ensure uniqueness')
  44. (utm_0 | utm_3 | utm_4).unlink()
  45. utm_6 = self.env[utm_model].create({'name': 'UTM dup'})
  46. self.assertEqual(utm_6.name, 'UTM dup [5]')
  47. utm_7 = self.env[utm_model].create({'name': 'UTM d'})
  48. self.assertEqual(utm_7.name, 'UTM d', msg='Even if this name has the same prefix as the other, it is still unique')
  49. utm_8 = utm_7.copy()
  50. self.assertEqual(utm_8.name, 'UTM d [2]', msg='Must add a counter as suffix to ensure uniqueness')
  51. # Test name uniqueness when creating a campaign using a title (quick creation)
  52. utm_9 = self.env['utm.campaign'].create({'title': 'UTM dup'})
  53. self.assertEqual(utm_9.name, 'UTM dup [6]', msg='Even if the record has been created using a title, the name must be unique')