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.

281 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_writing_company(self):
  127. self.company.zip_id = self.barcelona
  128. def test_constrains_partner_country(self):
  129. """Test partner country constraints"""
  130. partner = self.partner_obj.create({
  131. 'name': 'P1',
  132. 'zip_id': self.barcelona.id,
  133. 'country_id': self.barcelona.city_id.country_id.id,
  134. 'state_id': self.barcelona.city_id.state_id.id,
  135. 'city_id': self.barcelona.city_id.id,
  136. })
  137. with self.assertRaises(ValidationError):
  138. partner.country_id = self.ref('base.ch')
  139. def test_constrains_partner_state(self):
  140. """Test partner state constraints"""
  141. partner = self.partner_obj.create({
  142. 'name': 'P1',
  143. 'zip_id': self.barcelona.id,
  144. 'country_id': self.barcelona.city_id.country_id.id,
  145. 'state_id': self.barcelona.city_id.state_id.id,
  146. 'city_id': self.barcelona.city_id.id,
  147. })
  148. with self.assertRaises(ValidationError):
  149. partner.state_id = self.state_vd.id
  150. def test_constrains_partner_city(self):
  151. """Test partner city constraints"""
  152. partner = self.partner_obj.create({
  153. 'name': 'P1',
  154. 'zip_id': self.barcelona.id,
  155. 'country_id': self.barcelona.city_id.country_id.id,
  156. 'state_id': self.barcelona.city_id.state_id.id,
  157. 'city_id': self.barcelona.city_id.id,
  158. })
  159. with self.assertRaises(ValidationError):
  160. partner.city_id = self.city_lausanne
  161. def test_partner_onchange_country(self):
  162. """Test partner onchange country_id"""
  163. country_es = self.env.ref('base.es')
  164. country_es.enforce_cities = True
  165. partner = self.partner_obj.new({
  166. 'name': 'TEST',
  167. 'zip_id': self.lausanne.id
  168. })
  169. partner.country_id = country_es
  170. partner._onchange_country_id()
  171. self.assertFalse(partner.zip_id)
  172. def test_partner_onchange_city(self):
  173. """Test partner onchange city_id"""
  174. partner = self.partner_obj.new({
  175. 'name': 'TEST',
  176. 'zip_id': self.lausanne.id
  177. })
  178. self.city_bcn.country_id.enforce_cities = False
  179. partner.city_id = self.city_bcn
  180. partner._onchange_city_id()
  181. self.assertFalse(partner.zip_id)
  182. partner.city_id = False
  183. res = partner._onchange_city_id()
  184. self.assertFalse(res['domain']['zip_id'])
  185. def test_partner_onchange_state(self):
  186. """Test partner onchange state_id"""
  187. partner = self.partner_obj.new({
  188. 'name': 'TEST',
  189. 'zip_id': self.lausanne.id
  190. })
  191. partner.state_id = self.state_bcn
  192. partner._onchange_state_id()
  193. self.assertFalse(partner.zip_id)
  194. self.assertEqual(partner.country_id, partner.state_id.country_id)
  195. def test_company_onchange_state(self):
  196. """Test company onchange state_id"""
  197. self.company.state_id = self.state_bcn
  198. self.company._onchange_state_id()
  199. self.assertEqual(self.company.country_id,
  200. self.company.state_id.country_id)
  201. def test_display_name(self):
  202. """Test if the display_name is stored and computed properly"""
  203. self.assertEqual(
  204. self.lausanne.display_name,
  205. '666, Lausanne, Vaud, ' + self.browse_ref(
  206. 'base.ch'
  207. ).name
  208. )
  209. def test_name_search(self):
  210. """Test that zips can be searched through both the name of the
  211. city or the zip code"""
  212. madrid_data = {
  213. 'city_id': self.city_madrid.id,
  214. 'name': '555',
  215. }
  216. madrid = self.env['res.city.zip'].create(madrid_data)
  217. found_recs = self.env['res.city.zip'].name_search(name='444')
  218. self.assertEqual(len(found_recs), 1)
  219. self.assertEqual(found_recs[0][0], self.barcelona.id)
  220. found_recs = self.env['res.city.zip'].name_search(name='Barcelona')
  221. self.assertEqual(len(found_recs), 1)
  222. self.assertEqual(found_recs[0][0], self.barcelona.id)
  223. found_recs = self.env['res.city.zip'].name_search(name='555')
  224. self.assertEqual(len(found_recs), 1)
  225. self.assertEqual(found_recs[0][0], madrid.id)
  226. found_recs = self.env['res.city.zip'].name_search(name='Madrid')
  227. self.assertEqual(len(found_recs), 1)
  228. self.assertEqual(found_recs[0][0], madrid.id)
  229. found_recs = self.env['res.city.zip'].name_search(name='666')
  230. self.assertEqual(len(found_recs), 1)
  231. self.assertEqual(found_recs[0][0], self.lausanne.id)
  232. found_recs = self.env['res.city.zip'].name_search(name='Lausanne')
  233. self.assertEqual(len(found_recs), 1)
  234. self.assertEqual(found_recs[0][0], self.lausanne.id)
  235. def test_zip_ql_constraints(self):
  236. """Test UNIQUE name within it's area for zips"""
  237. with self.assertRaises(
  238. psycopg2.IntegrityError), mute_logger('odoo.sql_db'):
  239. self.env['res.city.zip'].create({
  240. 'name': '666',
  241. 'city_id': self.city_lausanne.id,
  242. })
  243. def test_city_sql_contraint(self):
  244. """Test UNIQUE name within it's area for cities"""
  245. with self.assertRaises(
  246. psycopg2.IntegrityError), mute_logger('odoo.sql_db'):
  247. self.env['res.city'].create({
  248. 'name': 'Barcelona',
  249. 'state_id': self.state_bcn.id,
  250. 'country_id': self.ref('base.es'),
  251. })