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.

258 lines
10 KiB

  1. # Copyright 2015 Yannick Vaucher, Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import psycopg2
  4. from odoo.exceptions import ValidationError
  5. from odoo.tests import Form, common, tagged
  6. from odoo.tools.misc import mute_logger
  7. @tagged("post_install", "-at_install")
  8. class TestBaseLocation(common.SavepointCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. super().setUpClass()
  12. state_obj = cls.env["res.country.state"]
  13. city_obj = cls.env["res.city"]
  14. zip_obj = cls.env["res.city.zip"]
  15. cls.partner_obj = cls.env["res.partner"]
  16. cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
  17. cls.state_vd = state_obj.create(
  18. {"name": "Vaud", "code": "VD", "country_id": cls.env.ref("base.ch").id}
  19. )
  20. cls.env.ref("base.es").write({"enforce_cities": True})
  21. cls.company = cls.env.ref("base.main_company")
  22. cls.country_es = cls.env.ref("base.es")
  23. cls.state_bcn = state_obj.create(
  24. {"name": "Barcelona", "code": "08", "country_id": cls.country_es.id}
  25. )
  26. cls.state_madrid = state_obj.create(
  27. {"name": "Madrid", "code": "28", "country_id": cls.env.ref("base.es").id}
  28. )
  29. cls.city_bcn = city_obj.create(
  30. {
  31. "name": "Barcelona",
  32. "state_id": cls.state_bcn.id,
  33. "country_id": cls.env.ref("base.es").id,
  34. }
  35. )
  36. cls.city_madrid = city_obj.create(
  37. {
  38. "name": "Madrid",
  39. "state_id": cls.state_madrid.id,
  40. "country_id": cls.env.ref("base.es").id,
  41. }
  42. )
  43. cls.city_lausanne = city_obj.create(
  44. {
  45. "name": "Lausanne",
  46. "state_id": cls.state_vd.id,
  47. "country_id": cls.env.ref("base.ch").id,
  48. }
  49. )
  50. cls.lausanne = zip_obj.create({"name": "666", "city_id": cls.city_lausanne.id})
  51. cls.barcelona = zip_obj.create({"name": "444", "city_id": cls.city_bcn.id})
  52. def test_onchange_partner_city_completion(self):
  53. """Test that partner data is filled accodingly"""
  54. partner1 = Form(self.env["res.partner"])
  55. partner1.zip_id = self.barcelona
  56. self.assertEqual(partner1.zip, self.barcelona.name)
  57. self.assertEqual(partner1.city, self.barcelona.city_id.name)
  58. self.assertEqual(partner1.state_id, self.barcelona.city_id.state_id)
  59. self.assertEqual(partner1.country_id, self.barcelona.city_id.country_id)
  60. def test_onchange_company_city_completion(self):
  61. """Test that company data is filled accodingly"""
  62. company = self.env["res.company"].new({"name": "Test"})
  63. company.zip_id = self.lausanne
  64. company._onchange_zip_id()
  65. self.assertEqual(company.zip, self.lausanne.name)
  66. self.assertEqual(company.city, self.lausanne.city_id.name)
  67. self.assertEqual(company.state_id, self.lausanne.city_id.state_id)
  68. self.assertEqual(company.country_id, self.lausanne.city_id.country_id)
  69. def test_company_address_fields(self):
  70. """Test if the partner address fields changes when
  71. changing the ones from the company"""
  72. company = self.env["res.company"].create({"name": "Test"})
  73. self.assertTrue(company.partner_id)
  74. company.partner_id.write(
  75. {
  76. "zip_id": self.lausanne.id,
  77. "state_id": self.lausanne.city_id.state_id.id,
  78. "country_id": self.lausanne.city_id.country_id.id,
  79. "city_id": self.lausanne.city_id.id,
  80. "city": self.lausanne.city_id.name,
  81. "zip": self.lausanne.name,
  82. }
  83. )
  84. company._compute_address()
  85. self.assertEqual(company.zip_id, company.partner_id.zip_id)
  86. self.assertEqual(company.city_id, company.partner_id.city_id)
  87. def test_company_address_fields_inverse(self):
  88. """Test inverse fields from res.company"""
  89. company = self.env["res.company"].create({"name": "Test"})
  90. company.zip_id = self.barcelona.id
  91. company._inverse_city_id()
  92. company._inverse_zip_id()
  93. self.assertEqual(company.zip_id, company.partner_id.zip_id)
  94. self.assertEqual(company.city_id, company.partner_id.city_id)
  95. def test_onchange_company_city_id_completion(self):
  96. """Test city auto-completion when changing zip in a company"""
  97. company = self.env["res.company"].new({"name": "Test"})
  98. company.zip_id = self.barcelona
  99. company._onchange_zip_id()
  100. self.assertEqual(company.city_id, self.barcelona.city_id)
  101. def test_constrains_partner_01(self):
  102. """Test zip_id constraints"""
  103. with self.assertRaises(ValidationError):
  104. self.partner_obj.create(
  105. {"name": "P1", "zip_id": self.barcelona.id, "state_id": False}
  106. )
  107. with self.assertRaises(ValidationError):
  108. self.partner_obj.create(
  109. {"name": "P1", "zip_id": self.barcelona.id, "country_id": False}
  110. )
  111. with self.assertRaises(ValidationError):
  112. self.partner_obj.create(
  113. {"name": "P1", "zip_id": self.barcelona.id, "city_id": False}
  114. )
  115. def test_writing_company(self):
  116. self.company.zip_id = self.barcelona
  117. def test_constrains_partner_country(self):
  118. """Test partner country constraints"""
  119. partner = self.partner_obj.create(
  120. {
  121. "name": "P1",
  122. "zip_id": self.barcelona.id,
  123. "country_id": self.barcelona.city_id.country_id.id,
  124. "state_id": self.barcelona.city_id.state_id.id,
  125. "city_id": self.barcelona.city_id.id,
  126. }
  127. )
  128. with self.assertRaises(ValidationError):
  129. partner.country_id = self.ref("base.ch")
  130. def test_constrains_partner_state(self):
  131. """Test partner state constraints"""
  132. partner = self.partner_obj.create(
  133. {
  134. "name": "P1",
  135. "zip_id": self.barcelona.id,
  136. "country_id": self.barcelona.city_id.country_id.id,
  137. "state_id": self.barcelona.city_id.state_id.id,
  138. "city_id": self.barcelona.city_id.id,
  139. }
  140. )
  141. with self.assertRaises(ValidationError):
  142. partner.state_id = self.state_vd.id
  143. def test_constrains_partner_city(self):
  144. """Test partner city constraints"""
  145. partner = self.partner_obj.create(
  146. {
  147. "name": "P1",
  148. "zip_id": self.barcelona.id,
  149. "country_id": self.barcelona.city_id.country_id.id,
  150. "state_id": self.barcelona.city_id.state_id.id,
  151. "city_id": self.barcelona.city_id.id,
  152. }
  153. )
  154. with self.assertRaises(ValidationError):
  155. partner.city_id = self.city_lausanne
  156. def test_partner_onchange_country(self):
  157. """Test partner onchange country_id"""
  158. partner = self.partner_obj.new({"name": "TEST", "zip_id": self.lausanne.id})
  159. partner.country_id = self.env.ref("base.es")
  160. partner._onchange_country_id()
  161. self.assertFalse(partner.zip_id)
  162. def test_partner_onchange_city(self):
  163. """Test partner onchange city_id"""
  164. partner = Form(self.env["res.partner"])
  165. partner.zip_id = self.lausanne
  166. self.city_bcn.country_id.enforce_cities = False
  167. partner.city_id = self.city_bcn
  168. self.assertFalse(partner.zip_id)
  169. partner.city_id = self.env["res.city"]
  170. self.assertEqual(
  171. len(partner.allowed_zip_ids), self.env["res.city.zip"].search_count([])
  172. )
  173. def test_partner_onchange_state(self):
  174. """Test partner onchange state_id"""
  175. partner = Form(self.env["res.partner"])
  176. partner.zip_id = self.lausanne
  177. partner.state_id = self.state_bcn
  178. self.assertFalse(partner.zip_id)
  179. self.assertEqual(partner.country_id, self.country_es)
  180. def test_company_onchange_state(self):
  181. """Test company onchange state_id"""
  182. self.company.state_id = self.state_bcn
  183. self.company._onchange_state_id()
  184. self.assertEqual(self.company.country_id, self.company.state_id.country_id)
  185. def test_display_name(self):
  186. """Test if the display_name is stored and computed properly"""
  187. self.assertEqual(
  188. self.lausanne.display_name,
  189. "666, Lausanne, Vaud, " + self.browse_ref("base.ch").name,
  190. )
  191. def test_name_search(self):
  192. """Test that zips can be searched through both the name of the
  193. city or the zip code"""
  194. madrid_data = {"city_id": self.city_madrid.id, "name": "555"}
  195. madrid = self.env["res.city.zip"].create(madrid_data)
  196. found_recs = self.env["res.city.zip"].name_search(name="444")
  197. self.assertEqual(len(found_recs), 1)
  198. self.assertEqual(found_recs[0][0], self.barcelona.id)
  199. found_recs = self.env["res.city.zip"].name_search(name="Barcelona")
  200. self.assertEqual(len(found_recs), 1)
  201. self.assertEqual(found_recs[0][0], self.barcelona.id)
  202. found_recs = self.env["res.city.zip"].name_search(name="555")
  203. self.assertEqual(len(found_recs), 1)
  204. self.assertEqual(found_recs[0][0], madrid.id)
  205. found_recs = self.env["res.city.zip"].name_search(name="Madrid")
  206. self.assertEqual(len(found_recs), 1)
  207. self.assertEqual(found_recs[0][0], madrid.id)
  208. found_recs = self.env["res.city.zip"].name_search(name="666")
  209. self.assertEqual(len(found_recs), 1)
  210. self.assertEqual(found_recs[0][0], self.lausanne.id)
  211. found_recs = self.env["res.city.zip"].name_search(name="Lausanne")
  212. self.assertEqual(len(found_recs), 1)
  213. self.assertEqual(found_recs[0][0], self.lausanne.id)
  214. def test_zip_ql_constraints(self):
  215. """Test UNIQUE name within it's area for zips"""
  216. with self.assertRaises(psycopg2.IntegrityError), mute_logger("odoo.sql_db"):
  217. self.env["res.city.zip"].create(
  218. {"name": "666", "city_id": self.city_lausanne.id}
  219. )
  220. def test_city_sql_contraint(self):
  221. """Test UNIQUE name within it's area for cities"""
  222. with self.assertRaises(psycopg2.IntegrityError), mute_logger("odoo.sql_db"):
  223. self.env["res.city"].create(
  224. {
  225. "name": "Barcelona",
  226. "state_id": self.state_bcn.id,
  227. "country_id": self.ref("base.es"),
  228. }
  229. )