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.

284 lines
11 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. with self.assertRaises(ValidationError):
  116. self.partner_obj.create(
  117. {"name": "P1", "zip_id": self.barcelona.id, "zip": False}
  118. )
  119. def test_writing_company(self):
  120. self.company.zip_id = self.barcelona
  121. def test_constrains_partner_country(self):
  122. """Test partner country constraints"""
  123. partner = self.partner_obj.create(
  124. {
  125. "name": "P1",
  126. "zip_id": self.barcelona.id,
  127. "country_id": self.barcelona.city_id.country_id.id,
  128. "state_id": self.barcelona.city_id.state_id.id,
  129. "city_id": self.barcelona.city_id.id,
  130. }
  131. )
  132. with self.assertRaises(ValidationError):
  133. partner.country_id = self.ref("base.ch")
  134. def test_constrains_partner_state(self):
  135. """Test partner state constraints"""
  136. partner = self.partner_obj.create(
  137. {
  138. "name": "P1",
  139. "zip_id": self.barcelona.id,
  140. "country_id": self.barcelona.city_id.country_id.id,
  141. "state_id": self.barcelona.city_id.state_id.id,
  142. "city_id": self.barcelona.city_id.id,
  143. }
  144. )
  145. with self.assertRaises(ValidationError):
  146. partner.state_id = self.state_vd.id
  147. def test_constrains_partner_city(self):
  148. """Test partner city constraints"""
  149. partner = self.partner_obj.create(
  150. {
  151. "name": "P1",
  152. "zip_id": self.barcelona.id,
  153. "country_id": self.barcelona.city_id.country_id.id,
  154. "state_id": self.barcelona.city_id.state_id.id,
  155. "city_id": self.barcelona.city_id.id,
  156. }
  157. )
  158. with self.assertRaises(ValidationError):
  159. partner.city_id = self.city_lausanne
  160. def test_partner_onchange_country(self):
  161. """Test partner onchange country_id"""
  162. partner = self.partner_obj.new({"name": "TEST", "zip_id": self.lausanne.id})
  163. partner.country_id = self.env.ref("base.es")
  164. partner._onchange_country_id()
  165. self.assertFalse(partner.zip_id)
  166. def test_partner_onchange_city(self):
  167. """Test partner onchange city_id"""
  168. partner = Form(self.env["res.partner"])
  169. partner.zip_id = self.lausanne
  170. self.city_bcn.country_id.enforce_cities = False
  171. partner.city_id = self.city_bcn
  172. self.assertFalse(partner.zip_id)
  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_partner_address_field_sync(self):
  186. """Test that zip_id is correctly synced with parent of contact addresses"""
  187. parent = self.env["res.partner"].create(
  188. {
  189. "name": "ACME Inc.",
  190. "is_company": True,
  191. "street": "123 Fake St.",
  192. "city": "Springfield",
  193. "city_id": self.barcelona.city_id.id,
  194. "state_id": self.barcelona.state_id.id,
  195. "country_id": self.barcelona.country_id.id,
  196. "zip_id": self.barcelona.id,
  197. }
  198. )
  199. contact = self.env["res.partner"].create(
  200. {
  201. "name": "John Doe",
  202. "type": "contact",
  203. "parent_id": parent.id,
  204. }
  205. )
  206. parent = Form(parent)
  207. parent.zip_id = self.lausanne
  208. parent.save()
  209. self.assertEqual(contact.zip_id, self.lausanne, "Contact should be synced")
  210. def test_display_name(self):
  211. """Test if the display_name is stored and computed properly"""
  212. self.assertEqual(
  213. self.lausanne.display_name,
  214. "666, Lausanne, Vaud, " + self.browse_ref("base.ch").name,
  215. )
  216. def test_name_search(self):
  217. """Test that zips can be searched through both the name of the
  218. city or the zip code"""
  219. madrid_data = {"city_id": self.city_madrid.id, "name": "555"}
  220. madrid = self.env["res.city.zip"].create(madrid_data)
  221. found_recs = self.env["res.city.zip"].name_search(name="444")
  222. self.assertEqual(len(found_recs), 1)
  223. self.assertEqual(found_recs[0][0], self.barcelona.id)
  224. found_recs = self.env["res.city.zip"].name_search(name="Barcelona")
  225. self.assertEqual(len(found_recs), 1)
  226. self.assertEqual(found_recs[0][0], self.barcelona.id)
  227. found_recs = self.env["res.city.zip"].name_search(name="555")
  228. self.assertEqual(len(found_recs), 1)
  229. self.assertEqual(found_recs[0][0], madrid.id)
  230. found_recs = self.env["res.city.zip"].name_search(name="Madrid")
  231. self.assertEqual(len(found_recs), 1)
  232. self.assertEqual(found_recs[0][0], madrid.id)
  233. found_recs = self.env["res.city.zip"].name_search(name="666")
  234. self.assertEqual(len(found_recs), 1)
  235. self.assertEqual(found_recs[0][0], self.lausanne.id)
  236. found_recs = self.env["res.city.zip"].name_search(name="Lausanne")
  237. self.assertEqual(len(found_recs), 1)
  238. self.assertEqual(found_recs[0][0], self.lausanne.id)
  239. def test_zip_ql_constraints(self):
  240. """Test UNIQUE name within it's area for zips"""
  241. with self.assertRaises(psycopg2.IntegrityError), mute_logger("odoo.sql_db"):
  242. self.env["res.city.zip"].create(
  243. {"name": "666", "city_id": self.city_lausanne.id}
  244. )
  245. def test_city_sql_contraint(self):
  246. """Test UNIQUE name within it's area for cities"""
  247. with self.assertRaises(psycopg2.IntegrityError), mute_logger("odoo.sql_db"):
  248. self.env["res.city"].create(
  249. {
  250. "name": "Barcelona",
  251. "state_id": self.state_bcn.id,
  252. "country_id": self.ref("base.es"),
  253. }
  254. )