You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from datetime import datetime, timedelta
  5. from mock import patch
  6. from odoo import fields
  7. from odoo.tests.common import SavepointCase
  8. PATH = "odoo.addons.partner_phonecall_schedule.models.res_partner.datetime"
  9. class CanICallCase(SavepointCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. super(CanICallCase, cls).setUpClass()
  13. cls.Calendar = cls.env["resource.calendar"].with_context(tz="UTC")
  14. cls.Partner = cls.env["res.partner"].with_context(tz="UTC")
  15. cls.some_mornings = cls.Calendar.create({
  16. "name": "Some mornings",
  17. "attendance_ids": [
  18. (0, 0, {
  19. "name": "Friday morning",
  20. "dayofweek": "4",
  21. "hour_from": 8,
  22. "hour_to": 12,
  23. }),
  24. (0, 0, {
  25. "name": "Next monday morning",
  26. "dayofweek": "0",
  27. "hour_from": 8,
  28. "hour_to": 12,
  29. "date_from": "2017-09-18",
  30. "date_to": "2017-09-18",
  31. }),
  32. ],
  33. })
  34. cls.some_evenings = cls.Calendar.create({
  35. "name": "Some evenings",
  36. "attendance_ids": [
  37. (0, 0, {
  38. "name": "Friday evening",
  39. "dayofweek": "4",
  40. "hour_from": 15,
  41. "hour_to": 19,
  42. }),
  43. (0, 0, {
  44. "name": "Next monday evening",
  45. "dayofweek": "0",
  46. "hour_from": 15,
  47. "hour_to": 19,
  48. "date_from": "2017-09-18",
  49. "date_to": "2017-09-18",
  50. }),
  51. ],
  52. })
  53. cls.dude = cls.Partner.create({
  54. "name": "Dude",
  55. })
  56. cls.dude.phonecall_calendar_ids = cls.some_mornings
  57. def setUp(self):
  58. super(CanICallCase, self).setUp()
  59. # Now it is a friday morning
  60. self.datetime = datetime(2017, 9, 15, 10, 53, 30)
  61. def _allowed(self, now=None):
  62. dude, Partner = self.dude, self.Partner
  63. if now:
  64. dude = dude.with_context(now=now)
  65. Partner = Partner.with_context(now=now)
  66. self.assertTrue(dude.phonecall_available)
  67. self.assertTrue(Partner.search([
  68. ("id", "=", dude.id),
  69. ("phonecall_available", "=", True),
  70. ]))
  71. self.assertTrue(Partner.search([
  72. ("id", "=", dude.id),
  73. ("phonecall_available", "!=", False),
  74. ]))
  75. self.assertFalse(Partner.search([
  76. ("id", "=", dude.id),
  77. ("phonecall_available", "=", False),
  78. ]))
  79. self.assertFalse(Partner.search([
  80. ("id", "=", dude.id),
  81. ("phonecall_available", "!=", True),
  82. ]))
  83. def allowed(self):
  84. # Test mocking datetime.now()
  85. with patch(PATH) as mocked_dt:
  86. mocked_dt.now.return_value = self.datetime
  87. mocked_dt.date.return_value = self.datetime.date()
  88. self._allowed()
  89. # Test sending a datetime object in the context
  90. self._allowed(self.datetime)
  91. # Test sending a string in the context
  92. self._allowed(
  93. fields.Datetime.to_string(self.datetime))
  94. def _disallowed(self, now=None):
  95. dude, Partner = self.dude, self.Partner
  96. if now:
  97. dude = dude.with_context(now=now)
  98. Partner = Partner.with_context(now=now)
  99. self.assertFalse(dude.phonecall_available)
  100. self.assertFalse(Partner.search([
  101. ("id", "=", dude.id),
  102. ("phonecall_available", "=", True),
  103. ]))
  104. self.assertFalse(Partner.search([
  105. ("id", "=", dude.id),
  106. ("phonecall_available", "!=", False),
  107. ]))
  108. self.assertTrue(Partner.search([
  109. ("id", "=", dude.id),
  110. ("phonecall_available", "=", False),
  111. ]))
  112. self.assertTrue(Partner.search([
  113. ("id", "=", dude.id),
  114. ("phonecall_available", "!=", True),
  115. ]))
  116. def disallowed(self):
  117. # Test mocking datetime.now()
  118. with patch(PATH) as mocked_dt:
  119. mocked_dt.now.return_value = self.datetime
  120. mocked_dt.date.return_value = self.datetime.date()
  121. self._disallowed()
  122. # Test sending a datetime object in the context
  123. self._disallowed(self.datetime)
  124. # Test sending a string in the context
  125. self._disallowed(
  126. fields.Datetime.to_string(self.datetime))
  127. def test_friday_morning(self):
  128. """I can call dude this morning"""
  129. self.allowed()
  130. def test_friday_evening(self):
  131. """I cannot call dude this evening"""
  132. self.datetime += timedelta(hours=4)
  133. self.disallowed()
  134. def test_saturday_morning(self):
  135. """I cannot call dude tomorrow morning"""
  136. self.datetime += timedelta(days=1)
  137. self.disallowed()
  138. def test_saturday_evening(self):
  139. """I cannot call dude tomorrow evening"""
  140. self.datetime += timedelta(days=1, hours=4)
  141. self.disallowed()
  142. def test_next_monday_morning(self):
  143. """I can call dude next monday morning"""
  144. self.datetime += timedelta(days=3)
  145. self.allowed()
  146. def test_second_next_monday_morning(self):
  147. """I cannot call dude second next monday morning"""
  148. self.datetime += timedelta(days=10, hours=4)
  149. self.disallowed()
  150. def test_aggregated_attendances(self):
  151. """I get aggregated schedules correctly."""
  152. self.dude.phonecall_calendar_ids |= self.some_evenings
  153. all_attendances = (self.some_mornings | self.some_evenings).mapped(
  154. "attendance_ids")
  155. self.assertEqual(
  156. self.dude.phonecall_calendar_attendance_ids, all_attendances)