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.

278 lines
10 KiB

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