Skip to content

Commit

Permalink
[FIX] crm: make pls test independant of demo data
Browse files Browse the repository at this point in the history
As all won / lost leads will have an impact on
the PLS frequency table, demo data will have one
on the probabilities of leads without team_id set,
as their probability is based on all leads, regardless
on their team. The demo data also creates entries in
the frequency table as some leads are lost / won.

Therefore, make sure we unlink all leads and frequencies
in the setUpClass, to avoid any interference with
demo data. One should create leads directly in their test.
The models are flushed too to account for the deletion.

Follow up of ecac497

Task-3700966

closes odoo#165959

X-original-commit: c87da27
Signed-off-by: Noé Antoine (nan) <nan@odoo.com>
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
  • Loading branch information
Odoonan committed May 21, 2024
1 parent 3d0d4f7 commit 37eef5c
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions addons/crm/tests/test_crm_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def setUpClass(cls):
'name': 'PLS Team',
})

# Ensure independance on demo data
cls.env['crm.lead'].with_context({'active_test': False}).search([]).unlink()
cls.env['crm.lead.scoring.frequency'].search([]).unlink()
cls.cr.flush()

def _get_lead_values(self, team_id, name_suffix, country_id, state_id, email_state, phone_state, source_id, stage_id):
return {
'name': 'lead_' + name_suffix,
Expand Down Expand Up @@ -165,9 +170,14 @@ def test_predictive_lead_scoring(self):

leads = Lead.create(leads_to_create)

# assign team 3 to all leads with no teams (also take data into account).
leads_with_no_team = self.env['crm.lead'].sudo().search([('team_id', '=', False)])
leads_with_no_team.write({'team_id': team_ids[2]})
# Assert lead data.
existing_leads = Lead.with_context({'active_filter': False}).search([])
self.assertEqual(existing_leads, leads)
self.assertEqual(existing_leads.filtered(lambda lead: not lead.team_id), leads[-4::])

# Assign leads without team to team 3 to compare probability
# as a separate team and the one with no team set. See below (*)
leads[-4::].team_id = team_ids[2]

# Set the PLS config
self.env['ir.config_parameter'].sudo().set_param("crm.pls_start_date", "2000-01-01")
Expand Down Expand Up @@ -201,17 +211,14 @@ def test_predictive_lead_scoring(self):

# Probability for Lead with no teams should be based on all the leads no matter their team.
# De-assign team 3 and rebuilt frequency table and recompute.
# Proba should be different as "no team" is not considered as a separated team.
leads_with_no_team.write({'team_id': False})
# Proba should be different as "no team" is not considered as a separated team. (*)
leads[-4::].write({'team_id': False})
leads[-4::].flush_recordset()

Lead._cron_update_automated_probabilities()
lead_13_no_team_proba = leads[13].automated_probability
self.assertTrue(lead_13_team_3_proba != leads[13].automated_probability, "Probability for leads with no team should be different than if they where in their own team.")
# Todo: Make this test fully independent from demo data
if not loaded_demo_data(self.env):
expected_proba = 35.19
else:
expected_proba = 36.65
self.assertAlmostEqual(lead_13_no_team_proba, expected_proba, places=2)
self.assertAlmostEqual(lead_13_no_team_proba, 35.19, places=2)

# Test frequencies
lead_4_stage_0_freq = LeadScoringFrequency.search([('team_id', '=', leads[4].team_id.id), ('variable', '=', 'stage_id'), ('value', '=', stage_ids[0])])
Expand Down

0 comments on commit 37eef5c

Please sign in to comment.