|
| 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