test_crm_lead_duplicates.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.crm.tests.common import TestCrmCommon
  4. from odoo.tests.common import tagged, users
  5. @tagged('lead_manage')
  6. class TestLeadConvert(TestCrmCommon):
  7. @users('user_sales_manager')
  8. def test_potential_duplicates(self):
  9. company = self.env['res.partner'].create({
  10. 'name': 'My company',
  11. 'email': 'mycompany@company.com',
  12. 'is_company': True,
  13. 'street': '57th Street',
  14. 'city': 'New New York',
  15. 'country_id': self.env.ref('base.us').id,
  16. 'zip': '12345',
  17. })
  18. partner_1 = self.env['res.partner'].create({
  19. 'name': 'Dave',
  20. 'email': 'dave@odoo.com',
  21. 'mobile': '+1 202 555 0123',
  22. 'phone': False,
  23. 'parent_id': company.id,
  24. 'is_company': False,
  25. 'street': 'Pearl street',
  26. 'city': 'California',
  27. 'country_id': self.env.ref('base.us').id,
  28. 'zip': '95826',
  29. })
  30. partner_2 = self.env['res.partner'].create({
  31. 'name': 'Eve',
  32. 'email': 'eve@odoo.com',
  33. 'mobile': '+1 202 555 3210',
  34. 'phone': False,
  35. 'parent_id': company.id,
  36. 'is_company': False,
  37. 'street': 'Wall street',
  38. 'city': 'New York',
  39. 'country_id': self.env.ref('base.us').id,
  40. 'zip': '54321',
  41. })
  42. lead_1 = self.env['crm.lead'].create({
  43. 'name': 'Lead 1',
  44. 'type': 'lead',
  45. 'partner_name': 'Alice',
  46. 'email_from': 'alice@odoo.com',
  47. })
  48. lead_2 = self.env['crm.lead'].create({
  49. 'name': 'Opportunity 1',
  50. 'type': 'opportunity',
  51. 'email_from': 'alice@odoo.com',
  52. })
  53. lead_3 = self.env['crm.lead'].create({
  54. 'name': 'Opportunity 2',
  55. 'type': 'opportunity',
  56. 'email_from': 'alice@odoo.com',
  57. })
  58. lead_4 = self.env['crm.lead'].create({
  59. 'name': 'Lead 2',
  60. 'type': 'lead',
  61. 'partner_name': 'Alice Doe'
  62. })
  63. lead_5 = self.env['crm.lead'].create({
  64. 'name': 'Opportunity 3',
  65. 'type': 'opportunity',
  66. 'partner_name': 'Alice Doe'
  67. })
  68. lead_6 = self.env['crm.lead'].create({
  69. 'name': 'Opportunity 4',
  70. 'type': 'opportunity',
  71. 'partner_name': 'Bob Doe'
  72. })
  73. lead_7 = self.env['crm.lead'].create({
  74. 'name': 'Opportunity 5',
  75. 'type': 'opportunity',
  76. 'partner_name': 'Bob Doe',
  77. 'email_from': 'bob@odoo.com',
  78. })
  79. lead_8 = self.env['crm.lead'].create({
  80. 'name': 'Opportunity 6',
  81. 'type': 'opportunity',
  82. 'email_from': 'bob@mymail.com',
  83. })
  84. lead_9 = self.env['crm.lead'].create({
  85. 'name': 'Opportunity 7',
  86. 'type': 'opportunity',
  87. 'email_from': 'alice@mymail.com',
  88. })
  89. lead_10 = self.env['crm.lead'].create({
  90. 'name': 'Opportunity 8',
  91. 'type': 'opportunity',
  92. 'probability': 0,
  93. 'active': False,
  94. 'email_from': 'alice@mymail.com',
  95. })
  96. lead_11 = self.env['crm.lead'].create({
  97. 'name': 'Opportunity 9',
  98. 'type': 'opportunity',
  99. 'contact_name': 'charlie'
  100. })
  101. lead_12 = self.env['crm.lead'].create({
  102. 'name': 'Opportunity 10',
  103. 'type': 'opportunity',
  104. 'contact_name': 'Charlie Chapelin',
  105. })
  106. lead_13 = self.env['crm.lead'].create({
  107. 'name': 'Opportunity 8',
  108. 'type': 'opportunity',
  109. 'partner_id': partner_1.id
  110. })
  111. lead_14 = self.env['crm.lead'].create({
  112. 'name': 'Lead 3',
  113. 'type': 'lead',
  114. 'partner_id': partner_2.id
  115. })
  116. self.assertEqual(lead_1 + lead_2 + lead_3, lead_1.duplicate_lead_ids)
  117. self.assertEqual(lead_1 + lead_2 + lead_3, lead_2.duplicate_lead_ids)
  118. self.assertEqual(lead_1 + lead_2 + lead_3, lead_3.duplicate_lead_ids)
  119. self.assertEqual(lead_4 + lead_5, lead_4.duplicate_lead_ids)
  120. self.assertEqual(lead_4 + lead_5, lead_5.duplicate_lead_ids)
  121. self.assertEqual(lead_6 + lead_7, lead_6.duplicate_lead_ids)
  122. self.assertEqual(lead_6 + lead_7, lead_7.duplicate_lead_ids)
  123. self.assertEqual(lead_8 + lead_9 + lead_10, lead_8.duplicate_lead_ids)
  124. self.assertEqual(lead_8 + lead_9 + lead_10, lead_9.duplicate_lead_ids)
  125. self.assertEqual(lead_8 + lead_9 + lead_10, lead_10.duplicate_lead_ids)
  126. self.assertEqual(lead_11 + lead_12, lead_11.duplicate_lead_ids)
  127. self.assertEqual(lead_12, lead_12.duplicate_lead_ids)
  128. self.assertEqual(lead_13 + lead_14, lead_13.duplicate_lead_ids)
  129. self.assertEqual(lead_13 + lead_14, lead_14.duplicate_lead_ids)
  130. @users('user_sales_manager')
  131. def test_potential_duplicates_with_phone(self):
  132. customer = self.env['res.partner'].create({
  133. 'email': 'customer1@duplicate.example.com',
  134. 'mobile': '+32485001122',
  135. 'name': 'Customer1',
  136. 'phone': '(803)-456-6126',
  137. })
  138. base_lead = self.env['crm.lead'].create({
  139. 'name': 'Base Lead',
  140. 'partner_id': customer.id,
  141. 'type': 'lead',
  142. })
  143. self.assertEqual(base_lead.contact_name, customer.name)
  144. self.assertEqual(base_lead.mobile, customer.mobile)
  145. self.assertFalse(base_lead.partner_name)
  146. self.assertEqual(base_lead.phone, customer.phone)
  147. dup1_1 = self.env['crm.lead'].create({
  148. 'name': 'Base Lead Dup1',
  149. 'type': 'lead',
  150. 'phone': '456-6126', # shorter version of base_lead
  151. 'mobile': ' ', # empty string shouldn't crash Odoo
  152. 'partner_name': 'Partner Name 1',
  153. })
  154. dup1_2 = self.env['crm.lead'].create({
  155. 'name': 'Base Lead Dup2',
  156. 'mobile': '8034566126',
  157. 'partner_name': 'Partner Name 2',
  158. 'type': 'lead',
  159. })
  160. dup1_3 = self.env['crm.lead'].create({
  161. 'name': 'Base Lead Dup3',
  162. 'partner_name': 'Partner Name 3',
  163. 'phone': '(803)-456-6126',
  164. 'type': 'lead',
  165. })
  166. dup1_4 = self.env['crm.lead'].create({
  167. 'mobile': '0032485001122',
  168. # 'mobile': '0485001122', # note: does not work
  169. 'name': 'Base Lead Dup4',
  170. 'partner_name': 'Partner Name 4',
  171. 'phone': False,
  172. 'type': 'lead',
  173. })
  174. expected = base_lead + dup1_2 + dup1_3 + dup1_4 # dup1_1 is shorter than lead -> not a dupe
  175. self.assertEqual(
  176. base_lead.duplicate_lead_ids, expected,
  177. 'CRM: missing %s, extra %s' % ((expected - base_lead.duplicate_lead_ids).mapped('name'), (base_lead.duplicate_lead_ids - expected).mapped('name'))
  178. )
  179. expected = base_lead + dup1_1 + dup1_2 + dup1_3 # dup1_4 has mobile of customer, but no link with dup1_1
  180. self.assertEqual(
  181. dup1_1.duplicate_lead_ids, expected,
  182. 'CRM: missing %s, extra %s' % ((expected - dup1_1.duplicate_lead_ids).mapped('name'), (dup1_1.duplicate_lead_ids - expected).mapped('name'))
  183. )
  184. @users('user_sales_manager')
  185. def test_potential_duplicates_with_invalid_email(self):
  186. lead_1 = self.env['crm.lead'].create({
  187. 'name': 'Lead 1',
  188. 'type': 'lead',
  189. 'email_from': 'mail"1@mymail.com'
  190. })
  191. lead_2 = self.env['crm.lead'].create({
  192. 'name': 'Opportunity 1',
  193. 'type': 'opportunity',
  194. 'email_from': 'mail2@mymail.com'
  195. })
  196. lead_3 = self.env['crm.lead'].create({
  197. 'name': 'Opportunity 2',
  198. 'type': 'lead',
  199. 'email_from': 'odoo.com'
  200. })
  201. lead_4 = self.env['crm.lead'].create({
  202. 'name': 'Opportunity 3',
  203. 'type': 'opportunity',
  204. 'email_from': 'odoo.com'
  205. })
  206. lead_5 = self.env['crm.lead'].create({
  207. 'name': 'Opportunity 3',
  208. 'type': 'opportunity',
  209. 'email_from': 'myodoo.com'
  210. })
  211. self.assertEqual(lead_1 + lead_2, lead_1.duplicate_lead_ids)
  212. self.assertEqual(lead_1 + lead_2, lead_2.duplicate_lead_ids)
  213. self.assertEqual(lead_3 + lead_4 + lead_5, lead_3.duplicate_lead_ids)
  214. self.assertEqual(lead_3 + lead_4 + lead_5, lead_4.duplicate_lead_ids)
  215. self.assertEqual(lead_5, lead_5.duplicate_lead_ids)