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.

159 lines
6.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Odoo, Open Source Management Solution
  5. #
  6. # Copyright (c) All rights reserved:
  7. # (c) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
  8. # (c) 2012 Domsense srl (<http://www.domsense.com>)
  9. # (c) 2015 Anubía, soluciones en la nube,SL (http://www.anubia.es)
  10. # Alejandro Santana <alejandrosantana@anubia.es>
  11. # (c) 2015 Savoir-faire Linux <http://www.savoirfairelinux.com>)
  12. # Agathe Mollé <agathe.molle@savoirfairelinux.com>
  13. #
  14. # This program is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Affero General Public License as
  16. # published by the Free Software Foundation, either version 3 of the
  17. # License, or (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU Affero General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU Affero General Public License
  25. # along with this program. If not, see http://www.gnu.org/licenses
  26. #
  27. ##############################################################################
  28. from openerp.tests import TransactionCase
  29. from datetime import datetime
  30. from dateutil.relativedelta import relativedelta
  31. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, \
  32. DEFAULT_SERVER_DATE_FORMAT
  33. class TestSuperCalendar(TransactionCase):
  34. def setUp(self):
  35. super(TestSuperCalendar, self).setUp()
  36. self.PartnerObj = self.env['res.partner']
  37. self.SuperCalendarObj = self.env['super.calendar']
  38. self.SuperCalendarConfiguratorObj = self.env[
  39. 'super.calendar.configurator']
  40. self.SuperCalendarConfiguratorLineObj = self.env[
  41. 'super.calendar.configurator.line']
  42. self.ModelFieldsObj = self.env['ir.model.fields']
  43. self.ModelObj = self.env['ir.model']
  44. self.partner_A = self.env.ref("base.main_partner")
  45. self.partner_A.write({
  46. 'date': (datetime.today() + relativedelta(days=3)),
  47. })
  48. self.super_calendar_configurator = \
  49. self.SuperCalendarConfiguratorObj.create({
  50. 'name': 'Partners',
  51. })
  52. self.partner_model = self.ModelObj.search([
  53. ('model', '=', 'res.partner')
  54. ])
  55. self.date_start_field = self.ModelFieldsObj.search([
  56. ('name', '=', 'write_date'),
  57. ('model', '=', 'res.partner'),
  58. ])
  59. self.description_field = self.ModelFieldsObj.search([
  60. ('name', '=', 'name'),
  61. ('model', '=', 'res.partner'),
  62. ])
  63. self.super_calendar_configurator_line = \
  64. self.SuperCalendarConfiguratorLineObj.create({
  65. 'name': self.partner_model.id,
  66. 'date_start_field_id': self.date_start_field.id,
  67. 'description_field_id': self.description_field.id,
  68. 'configurator_id': self.super_calendar_configurator.id,
  69. 'domain': [('name', '=', self.partner_A.name)]
  70. })
  71. def test_get_record_values_from_line(self):
  72. """
  73. Test if record values are correctly computed
  74. """
  75. # Test without any date_stop or duration
  76. values_partner_a = {
  77. 'configurator_id': self.super_calendar_configurator.id,
  78. 'date_start': self.partner_A.write_date,
  79. 'duration': False,
  80. 'model_id': self.partner_model.id,
  81. 'name': self.partner_A.name,
  82. 'res_id': self.partner_model.model+','+str(self.partner_A.id),
  83. 'user_id': False
  84. }
  85. self.assertEqual(
  86. self.super_calendar_configurator._get_record_values_from_line(
  87. self.super_calendar_configurator.line_ids[0]
  88. )[self.partner_A],
  89. values_partner_a
  90. )
  91. # Add a date_stop
  92. self.date_stop_field = self.ModelFieldsObj.search([
  93. ('name', '=', 'date'),
  94. ('model', '=', 'res.partner'),
  95. ])
  96. start_date = datetime.strptime(self.partner_A.write_date,
  97. DEFAULT_SERVER_DATETIME_FORMAT)
  98. stop_date = datetime.strptime(self.partner_A.date,
  99. DEFAULT_SERVER_DATE_FORMAT)
  100. date_diff = (stop_date - start_date)
  101. self.super_calendar_configurator_line.write({
  102. 'date_stop_field_id': self.date_stop_field.id,
  103. })
  104. values_partner_a['duration'] = date_diff.total_seconds() / 3600
  105. self.assertEqual(
  106. self.super_calendar_configurator._get_record_values_from_line(
  107. self.super_calendar_configurator.line_ids[0]
  108. )[self.partner_A],
  109. values_partner_a
  110. )
  111. # Test description code
  112. self.super_calendar_configurator2 = \
  113. self.SuperCalendarConfiguratorObj.create({
  114. 'name': 'Partners 2',
  115. })
  116. self.super_calendar_configurator_line2 = \
  117. self.SuperCalendarConfiguratorLineObj.create({
  118. 'name': self.partner_model.id,
  119. 'date_start_field_id': self.date_start_field.id,
  120. 'description_type': 'code',
  121. 'description_code': '${o.email}',
  122. 'configurator_id': self.super_calendar_configurator2.id,
  123. 'domain': [('name', '=', self.partner_A.name)]
  124. })
  125. values_partner_a['name'] = self.partner_A.email
  126. values_partner_a['duration'] = False
  127. values_partner_a['configurator_id'] = \
  128. self.super_calendar_configurator2.id
  129. self.assertEqual(
  130. self.super_calendar_configurator2._get_record_values_from_line(
  131. self.super_calendar_configurator2.line_ids[0]
  132. )[self.partner_A],
  133. values_partner_a
  134. )
  135. def test_generate_calendar_records(self):
  136. """
  137. Test if calendar records are effectively created
  138. """
  139. self.super_calendar_configurator.generate_calendar_records()
  140. super_calendar_record = self.SuperCalendarObj.search([
  141. ('name', '=', self.partner_A.name)
  142. ])
  143. self.assertEqual(
  144. super_calendar_record.date_start,
  145. self.partner_A.write_date
  146. )