[IMP] mail_activity_team: Improve test using standard methods

This commit is contained in:
Enric Tobella 2022-05-26 21:14:04 +02:00
parent 5edac89b4f
commit c3c52d7384
3 changed files with 112 additions and 28 deletions

View File

@ -38,7 +38,7 @@ class MailActivity(models.Model):
if self.team_id and self.user_id in self.team_id.member_ids:
return res
self.team_id = self.with_context(
default_res_model=self.res_model_id.id
default_res_model=self.res_model_id.model
)._get_default_team_id(user_id=self.user_id.id)
return res

View File

@ -64,6 +64,9 @@ class ResUsers(models.Model):
user_activities[activity["model"]][
"%s_count" % activity["states"]
] += activity["count"]
if activity["states"] in ("today", "overdue"):
if (
activity["states"] in ("today", "overdue")
and activity["user_id"] != user
):
user_activities[activity["model"]]["total_count"] += activity["count"]
return list(user_activities.values())

View File

@ -1,13 +1,14 @@
# Copyright 2018-22 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase
from odoo.tests.common import Form, SavepointCase
class TestMailActivityTeam(TransactionCase):
def setUp(self):
super(TestMailActivityTeam, self).setUp()
class TestMailActivityTeam(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
self = cls
self.env["mail.activity.team"].search([]).unlink()
self.employee = self.env["res.users"].create(
@ -125,6 +126,18 @@ class TestMailActivityTeam(TransactionCase):
}
)
def test_activity_members(self):
self.team1.member_ids |= self.employee2
self.partner_client.refresh()
self.assertIn(self.employee2, self.partner_client.activity_team_user_ids)
self.assertIn(self.employee, self.partner_client.activity_team_user_ids)
self.assertEqual(
self.partner_client,
self.env["res.partner"].search(
[("activity_team_user_ids", "=", self.employee.id)]
),
)
def test_team_and_user_onchange(self):
with self.assertRaises(ValidationError):
self.team1.member_ids = [(3, self.employee.id)]
@ -144,32 +157,83 @@ class TestMailActivityTeam(TransactionCase):
self.team2._onchange_user_id()
self.assertTrue(self.employee3 in self.team2.member_ids)
def test_activity_onchanges(self):
def test_activity_onchanges_keep_user(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.act2.team_id = False
self.act2._onchange_team_id()
self.assertEqual(self.act2.user_id, self.employee)
self.act2.team_id = self.team2
self.act2._onchange_team_id()
self.assertEqual(self.act2.user_id, self.employee)
self.act2.user_id = self.employee2
self.act2._onchange_user_id()
self.assertEqual(self.act2.team_id, self.team2)
with Form(self.act2) as form:
form.team_id = self.env["mail.activity.team"]
self.assertEqual(form.user_id, self.employee)
def test_activity_onchanges_user_no_member_team(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
with Form(self.act2) as form:
form.user_id = self.employee2
self.assertEqual(form.team_id, self.team2)
def test_activity_onchanges_user_no_team(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
with Form(self.act2) as form:
form.team_id = self.env["mail.activity.team"]
form.user_id = self.employee2
self.assertEqual(form.team_id, self.team2)
def test_activity_onchanges_team_no_member(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.team2.user_id = False
self.team2.member_ids = False
with Form(self.act2) as form:
form.team_id = self.team2
self.assertFalse(form.user_id)
def test_activity_onchanges_team_different_member(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.team2.user_id = self.employee2
self.team2.member_ids = self.employee2
with Form(self.act2) as form:
form.team_id = self.team2
self.assertEqual(form.user_id, self.employee2)
def test_activity_onchanges_team_different_member_no_leader(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.team2.user_id = False
self.team2.member_ids = self.employee2
with Form(self.act2) as form:
form.team_id = self.team2
self.assertEqual(form.user_id, self.employee2)
def test_activity_onchanges_activity_type_set_team(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.activity1.default_team_id = self.team2
self.assertEqual(self.act2.activity_type_id, self.activity2)
with Form(self.act2) as form:
form.activity_type_id = self.activity1
self.assertEqual(form.team_id, self.team2)
def test_activity_onchanges_activity_type_no_team(self):
self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.assertEqual(self.act2.activity_type_id, self.activity2)
with Form(self.act2) as form:
form.activity_type_id = self.activity1
self.assertEqual(form.team_id, self.team1)
def test_activity_constrain(self):
with self.assertRaises(ValidationError):
self.act2.write({"user_id": self.employee2.id, "team_id": self.team1.id})
self.team1.user_id = False
self.act2.user_id = False
self.act2._onchange_user_id()
self.team2.member_ids = [(4, self.employee3.id)]
self.act2.team_id = self.team1
self.act2.team_id = False
self.act2.user_id = self.employee3
self.act2._onchange_user_id()
self.act2.team_id = self.team2
self.team2.member_ids = [(3, self.act2.user_id.id)]
self.act2._onchange_team_id()
def test_schedule_activity(self):
"""Correctly assign teams to auto scheduled activities. Those won't
@ -210,6 +274,23 @@ class TestMailActivityTeam(TransactionCase):
.systray_get_activities()
)
self.assertEqual(res[0]["total_count"], 0)
self.assertEqual(res[0]["today_count"], 1)
partner_record = self.employee.partner_id.with_user(self.employee.id)
self.activity2.default_team_id = self.team2
activity = partner_record.activity_schedule(
activity_type_id=self.activity2.id, user_id=self.employee2.id
)
activity.flush()
res = (
self.env["res.users"]
.with_user(self.employee.id)
.with_context({"team_activities": True})
.systray_get_activities()
)
self.assertEqual(res[0]["total_count"], 1)
self.assertEqual(res[0]["today_count"], 2)
res = self.env["res.users"].with_user(self.employee.id).systray_get_activities()
self.assertEqual(res[0]["total_count"], 2)
def test_activity_schedule_next(self):
self.activity1.write(