Skip to content

Commit e260401

Browse files
committed
Add test for _cancel_volunteer_leave_participation().
1 parent 5e83781 commit e260401

File tree

3 files changed

+167
-7
lines changed

3 files changed

+167
-7
lines changed

volunteer_holiday/tests/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
#
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44

5+
from . import test_volunteer_leave
56
from . import test_company_holiday
6-
7-
# from . import test_volunteer_leave

volunteer_holiday/tests/test_company_holiday.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@freeze_time("2026-01-01 10:00:00")
13-
class TestCronHoliday(TransactionCase):
13+
class TestCompanyHoliday(TransactionCase):
1414
def setUp(self, *args, **kwargs):
1515
super().setUp(*args, **kwargs)
1616

@@ -33,14 +33,14 @@ def setUp(self, *args, **kwargs):
3333

3434
self.Company = self.env["res.company"]
3535
self.Holiday = self.env["volunteer.company.holiday"]
36-
self.Shift = self.env["volunteer.shift"]
36+
# self.Shift = self.env["volunteer.shift"]
3737
self.Type = self.env["volunteer.shift.type"]
3838
self.Generator = self.env["volunteer.shift.recurrent.generator"]
3939

4040
# Stages
4141

42-
self.stage_confirmed = self.env.ref("volunteer.volunteer_shift_stage_confirmed")
43-
self.stage_canceled = self.env.ref("volunteer.volunteer_shift_stage_canceled")
42+
# self.stage_confirmed = self.env.ref("volunteer.volunteer_shift_stage_confirmed")
43+
# self.stage_canceled = self.env.ref("volunteer.volunteer_shift_stage_canceled")
4444

4545
# Create required type
4646
self.type1 = self.Type.create(
@@ -146,7 +146,7 @@ def setUp(self, *args, **kwargs):
146146
def test_cancel_holiday_shift(self):
147147
"""Test that holidays do cancel confirmed generated shifts"""
148148

149-
# Check before test
149+
# Checks before test
150150
shifts_on_holiday = self.gen_with_holiday.volunteer_shift_ids
151151
for shift in shifts_on_holiday:
152152
self.assertEqual(shift.state, "confirmed")
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# SPDX-FileCopyrightText: 2025 Coop IT Easy SC
2+
#
3+
# SPDX-License-Identifier: AGPL-3.0-or-later
4+
5+
from datetime import date, datetime
6+
7+
from freezegun import freeze_time
8+
9+
from odoo.tests.common import TransactionCase
10+
11+
12+
@freeze_time("2026-03-01 10:00:00")
13+
class TestVolunteerLeave(TransactionCase):
14+
def setUp(self, *args, **kwargs):
15+
super().setUp(*args, **kwargs)
16+
17+
# Force all operations to run as admin
18+
self.env = self.env(user=self.env.ref("base.user_admin"))
19+
20+
# Set up the environment
21+
self.env = self.env(
22+
context=dict(
23+
self.env.context,
24+
mail_create_nolog=True,
25+
mail_create_nosubscribe=True,
26+
mail_notrack=True,
27+
no_reset_password=True,
28+
tracking_disable=True,
29+
)
30+
)
31+
32+
# Models
33+
34+
self.Volunteer = self.env["volunteer.volunteer"]
35+
self.Shift = self.env["volunteer.shift"]
36+
self.Type = self.env["volunteer.shift.type"]
37+
self.VolunteerLeave = self.env["volunteer.volunteer.leave"]
38+
self.VolunteerLeaveType = self.env["volunteer.volunteer.leave.type"]
39+
self.Participation = self.env["volunteer.shift.participation"]
40+
41+
# Create required stages
42+
self.stage_confirmed = self.env.ref("volunteer.volunteer_shift_stage_confirmed")
43+
self.stage_canceled = self.env.ref("volunteer.volunteer_shift_stage_canceled")
44+
45+
# Create required types
46+
self.type1 = self.Type.create(
47+
{
48+
"name": "TypeTest",
49+
"description": "Type for autotests",
50+
}
51+
)
52+
self.volunteer_leave_type1 = self.VolunteerLeaveType.create(
53+
{
54+
"name": "LeaveTypeTest",
55+
"description": "Type for autotests",
56+
}
57+
)
58+
59+
# Create shifts
60+
self.shift1 = self.Shift.create(
61+
{
62+
"name": "Shift1",
63+
"stage_id": self.stage_confirmed.id,
64+
"start_time": datetime(2026, 4, 1, 10, 5),
65+
"end_time": datetime(2026, 4, 1, 12, 5),
66+
"tz": "Europe/Brussels",
67+
"max_volunteer_nb": 2,
68+
"type_id": self.type1.id,
69+
}
70+
)
71+
self.shift2 = self.Shift.create(
72+
{
73+
"name": "Shift2",
74+
"stage_id": self.stage_confirmed.id,
75+
"start_time": datetime(2026, 4, 2, 10, 5),
76+
"end_time": datetime(2026, 4, 2, 12, 5),
77+
"tz": "Europe/Brussels",
78+
"max_volunteer_nb": 2,
79+
"type_id": self.type1.id,
80+
}
81+
)
82+
83+
# Create volunteers, one that has time off, one that doesn't
84+
self.leaving_volunteer = self.Volunteer.create({"name": "LeavingVolunteer"})
85+
self.staying_volunteer = self.Volunteer.create({"name": "StayingVolunteer"})
86+
87+
# Create confirmed participations
88+
self.leaving_volunteer_participation1 = self.Participation.create(
89+
{
90+
"volunteer_id": self.leaving_volunteer.id,
91+
"shift_id": self.shift1.id,
92+
"registration_state": "confirmed",
93+
}
94+
)
95+
self.leaving_volunteer_participation2 = self.Participation.create(
96+
{
97+
"volunteer_id": self.leaving_volunteer.id,
98+
"shift_id": self.shift2.id,
99+
"registration_state": "confirmed",
100+
}
101+
)
102+
self.staying_volunteer_participation1 = self.Participation.create(
103+
{
104+
"volunteer_id": self.staying_volunteer.id,
105+
"shift_id": self.shift1.id,
106+
"registration_state": "confirmed",
107+
}
108+
)
109+
self.staying_volunteer_participation2 = self.Participation.create(
110+
{
111+
"volunteer_id": self.staying_volunteer.id,
112+
"shift_id": self.shift2.id,
113+
"registration_state": "confirmed",
114+
}
115+
)
116+
117+
# Create leaves for leaving volunteer
118+
self.leaving_volunteer_leave = self.VolunteerLeave.create(
119+
{
120+
"volunteer_id": self.leaving_volunteer.id,
121+
"type_id": self.volunteer_leave_type1.id,
122+
"start_date": date(2026, 4, 1),
123+
"end_date": date(2026, 4, 2),
124+
}
125+
)
126+
127+
def test_cancel_volunteer_leave_participation(self):
128+
"""Test that volunteer participations are canceled
129+
if the associated shifts overlap with the volunteer's time off"""
130+
131+
# Checks before test
132+
self.assertEqual(
133+
self.leaving_volunteer_participation1.registration_state, "confirmed"
134+
)
135+
self.assertEqual(
136+
self.leaving_volunteer_participation2.registration_state, "confirmed"
137+
)
138+
self.assertEqual(
139+
self.staying_volunteer_participation1.registration_state, "confirmed"
140+
)
141+
self.assertEqual(
142+
self.staying_volunteer_participation2.registration_state, "confirmed"
143+
)
144+
145+
# Call function
146+
self.VolunteerLeave._cancel_volunteer_leave_participation()
147+
148+
# Participations of the leaving volunteer should be canceled
149+
self.assertEqual(
150+
self.leaving_volunteer_participation1.registration_state, "canceled"
151+
)
152+
self.assertEqual(
153+
self.leaving_volunteer_participation2.registration_state, "canceled"
154+
)
155+
# Participations of the staying volunteer should be confirmed
156+
self.assertEqual(
157+
self.staying_volunteer_participation1.registration_state, "confirmed"
158+
)
159+
self.assertEqual(
160+
self.staying_volunteer_participation2.registration_state, "confirmed"
161+
)

0 commit comments

Comments
 (0)