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.

213 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. # © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U.
  4. """These tests try to mimic the behavior of the UI form.
  5. The form operates in onchange mode, with its limitations.
  6. """
  7. from openerp.tests.common import TransactionCase
  8. class OnChangeCase(TransactionCase):
  9. is_company = False
  10. def new_partner(self):
  11. """Create an empty partner. Ensure it is (or not) a company."""
  12. new = self.env["res.partner"].new()
  13. new.is_company = self.is_company
  14. return new
  15. class PartnerCompanyCase(OnChangeCase):
  16. is_company = True
  17. def tearDown(self):
  18. """Companies never have ``firstname`` nor ``lastname2``."""
  19. self.assertEqual(self.partner.firstname, False)
  20. self.assertEqual(self.partner.lastname2, False)
  21. def set_name(self, value):
  22. self.partner.name = value
  23. # It triggers onchange
  24. self.partner._onchange_name()
  25. # Ensure it's properly set
  26. self.assertEqual(self.partner.name, value)
  27. def test_create_from_form(self):
  28. """A user creates a company from the form."""
  29. name = u"Sôme company"
  30. with self.env.do_in_onchange():
  31. # User presses ``new``
  32. self.partner = self.new_partner()
  33. # User changes fields
  34. self.set_name(name)
  35. self.assertEqual(self.partner.lastname, name)
  36. def test_empty_name_and_subnames(self):
  37. """If the user empties ``name``, subnames must be ``False``.
  38. Otherwise, the ``required`` attr will not work as expected.
  39. """
  40. with self.env.do_in_onchange():
  41. # User presses ``new``
  42. self.partner = self.new_partner()
  43. # User changes fields
  44. self.set_name(u"Fóo")
  45. self.set_name(u"")
  46. self.assertEqual(self.partner.lastname, False)
  47. class PartnerContactCase(OnChangeCase):
  48. def set_field(self, field, value):
  49. # Changes the field
  50. setattr(self.partner, field, value)
  51. if field in ("firstname", "lastname", "lastname2"):
  52. # Trigger onchanges
  53. self.partner._onchange_subnames()
  54. self.partner._onchange_name()
  55. # Check it's set OK
  56. self.assertEqual(getattr(self.partner, field), value)
  57. def test_create_from_form_empty(self):
  58. """A user creates a contact from the form.
  59. All subfields must be false, or the ``required`` attr will not work as
  60. expected.
  61. """
  62. with self.env.do_in_onchange():
  63. # User presses ``new``
  64. self.partner = self.new_partner()
  65. # Odoo tries to compute the name
  66. self.partner._compute_name()
  67. # This is then triggered
  68. self.partner._onchange_name()
  69. # Subnames must start as False to make the UI work fine
  70. self.assertEqual(self.partner.firstname, False)
  71. self.assertEqual(self.partner.lastname, False)
  72. self.assertEqual(self.partner.lastname2, False)
  73. # ``name`` cannot be False, or upstream Odoo will fail
  74. self.assertEqual(self.partner.name, u"")
  75. def test_create_from_form_only_firstname(self):
  76. """A user creates a contact with only the firstname from the form."""
  77. firstname = u"Fïrst"
  78. with self.env.do_in_onchange():
  79. # User presses ``new``
  80. self.partner = self.new_partner()
  81. # User changes fields
  82. self.set_field("firstname", firstname)
  83. self.assertEqual(self.partner.lastname, False)
  84. self.assertEqual(self.partner.lastname2, False)
  85. self.assertEqual(self.partner.name, firstname)
  86. def test_create_from_form_only_lastname(self):
  87. """A user creates a contact with only the lastname from the form."""
  88. lastname = u"Läst"
  89. with self.env.do_in_onchange():
  90. # User presses ``new``
  91. self.partner = self.new_partner()
  92. # User changes fields
  93. self.set_field("lastname", lastname)
  94. self.assertEqual(self.partner.firstname, False)
  95. self.assertEqual(self.partner.lastname2, False)
  96. self.assertEqual(self.partner.name, lastname)
  97. def test_create_from_form_only_lastname2(self):
  98. """A user creates a contact with only the lastname2 from the form."""
  99. lastname2 = u"Läst2"
  100. with self.env.do_in_onchange():
  101. # User presses ``new``
  102. self.partner = self.new_partner()
  103. # User changes fields
  104. self.set_field("lastname2", lastname2)
  105. self.assertEqual(self.partner.firstname, False)
  106. self.assertEqual(self.partner.lastname, False)
  107. self.assertEqual(self.partner.name, lastname2)
  108. def test_create_from_without_firstname(self):
  109. """A user creates a contact without firstname from the form."""
  110. lastname = u"Läst"
  111. lastname2 = u"Läst2"
  112. with self.env.do_in_onchange():
  113. # User presses ``new``
  114. self.partner = self.new_partner()
  115. # User changes fields
  116. self.set_field("lastname", lastname)
  117. self.set_field("lastname2", lastname2)
  118. self.assertEqual(self.partner.firstname, False)
  119. self.assertEqual(
  120. self.partner.name,
  121. u"%s %s" % (lastname, lastname2))
  122. def test_create_from_without_lastname(self):
  123. """A user creates a contact without lastname from the form."""
  124. firstname = u"Fïrst"
  125. lastname2 = u"Läst2"
  126. with self.env.do_in_onchange():
  127. # User presses ``new``
  128. self.partner = self.new_partner()
  129. # User changes fields
  130. self.set_field("firstname", firstname)
  131. self.set_field("lastname2", lastname2)
  132. self.assertEqual(self.partner.lastname, False)
  133. self.assertEqual(
  134. self.partner.name,
  135. u"%s, %s" % (lastname2, firstname))
  136. def test_create_from_without_lastname2(self):
  137. """A user creates a contact without lastname2 from the form."""
  138. firstname = u"Fïrst"
  139. lastname = u"Läst"
  140. with self.env.do_in_onchange():
  141. # User presses ``new``
  142. self.partner = self.new_partner()
  143. # User changes fields
  144. self.set_field("firstname", firstname)
  145. self.set_field("lastname", lastname)
  146. self.assertEqual(self.partner.lastname2, False)
  147. self.assertEqual(
  148. self.partner.name,
  149. u"%s, %s" % (lastname, firstname))
  150. def test_create_from_form_all(self):
  151. """A user creates a contact with all names from the form."""
  152. firstname = u"Fïrst"
  153. lastname = u"Läst"
  154. lastname2 = u"Läst2"
  155. with self.env.do_in_onchange():
  156. # User presses ``new``
  157. self.partner = self.new_partner()
  158. # User changes fields
  159. self.set_field("firstname", firstname)
  160. self.set_field("lastname", lastname)
  161. self.set_field("lastname2", lastname2)
  162. self.assertEqual(
  163. self.partner.name,
  164. u"%s %s, %s" % (lastname, lastname2, firstname))