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.

270 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.tests.common import TransactionCase
  4. from odoo.exceptions import ValidationError
  5. class TestBaseLocation(TransactionCase):
  6. def test_onchange_better_zip_state_id(self):
  7. """ Test onchange on res.better.zip """
  8. usa_MA = self.env.ref('base.state_us_34')
  9. better_zip1 = self.env['res.better.zip'].new(self.values_better_zip1())
  10. better_zip1.state_id = usa_MA
  11. better_zip1._onchange_state_id()
  12. self.assertEqual(better_zip1.country_id, usa_MA.country_id)
  13. def test_onchange_better_zip_city_id(self):
  14. better_zip2 = self.env['res.better.zip'].new(self.values_better_zip2())
  15. better_zip2.city_id = self.city_madrid
  16. better_zip2._onchange_city_id()
  17. self.assertEqual(better_zip2.city, self.city_madrid.name)
  18. def test_onchange_better_zip_country_id(self):
  19. better_zip1 = self.env['res.better.zip'].new(self.values_better_zip1())
  20. better_zip1.country_id = self.env.ref('base.es')
  21. better_zip1._onchange_country_id()
  22. self.assertFalse(better_zip1.state_id)
  23. def test_onchange_better_zip_none(self):
  24. better_zip1 = self.env['res.better.zip'].new(self.values_better_zip1())
  25. better_zip1.country_id = False
  26. better_zip1._onchange_country_id()
  27. self.assertFalse(better_zip1.state_id)
  28. def test_onchange_partner_city_completion(self):
  29. partner1 = self.env['res.partner'].new({
  30. 'name': 'Camptocamp',
  31. })
  32. better_zip2 = self.env['res.better.zip'].create(
  33. self.values_better_zip2())
  34. better_zip2.country_id.enforce_cities = True
  35. partner1.zip_id = better_zip2
  36. partner1._onchange_zip_id()
  37. self.assertEqual(partner1.zip, better_zip2.name)
  38. self.assertEqual(partner1.city, better_zip2.city)
  39. self.assertEqual(partner1.state_id, better_zip2.state_id)
  40. self.assertEqual(partner1.country_id, better_zip2.country_id)
  41. def test_onchange_company_city_completion(self):
  42. company = self.env['res.company'].new({'name': 'Test'})
  43. better_zip1 = self.env['res.better.zip'].create(
  44. self.values_better_zip1())
  45. company.zip_id = better_zip1
  46. company._onchange_zip_id()
  47. self.assertEqual(company.zip, better_zip1.name)
  48. self.assertEqual(company.city, better_zip1.city)
  49. self.assertEqual(company.state_id, better_zip1.state_id)
  50. self.assertEqual(company.country_id, better_zip1.country_id)
  51. def test_company_address_fields(self):
  52. better_zip1 = self.env['res.better.zip'].create(
  53. self.values_better_zip1()
  54. )
  55. company = self.env['res.company'].create({
  56. 'name': 'Test',
  57. })
  58. self.assertTrue(company.partner_id)
  59. company.partner_id.write({
  60. 'zip_id': better_zip1.id,
  61. 'state_id': better_zip1.state_id.id,
  62. 'country_id': better_zip1.country_id.id,
  63. 'city_id': better_zip1.city_id.id,
  64. 'city': better_zip1.city,
  65. 'zip': better_zip1.name,
  66. })
  67. company._compute_address()
  68. self.assertEqual(company.zip_id, company.partner_id.zip_id)
  69. self.assertEqual(company.city_id, company.partner_id.city_id)
  70. def test_company_address_fields_inverse(self):
  71. better_zip2 = self.env['res.better.zip'].create(
  72. self.values_better_zip2()
  73. )
  74. company = self.env['res.company'].new({
  75. 'name': 'Test',
  76. 'partner_id': self.env['res.partner'].new({}).id
  77. # Partner must be initiated in order to be filled
  78. })
  79. company.update({
  80. 'zip_id': better_zip2.id,
  81. })
  82. company._inverse_city_id()
  83. company._inverse_zip_id()
  84. self.assertEqual(company.zip_id, company.partner_id.zip_id)
  85. self.assertEqual(company.city_id, company.partner_id.city_id)
  86. def test_onchange_company_city_id_completion(self):
  87. company = self.env['res.company'].new({'name': 'Test'})
  88. better_zip2 = self.env['res.better.zip'].create(
  89. self.values_better_zip2())
  90. company.zip_id = better_zip2
  91. company._onchange_zip_id()
  92. self.assertEqual(company.city_id, better_zip2.city_id)
  93. def test_constrains_better_zip_01(self):
  94. better_zip1 = self.env['res.better.zip'].create(
  95. self.values_better_zip1())
  96. better_zip2 = self.env['res.better.zip'].create(
  97. self.values_better_zip2())
  98. better_zip1.city_id = self.city_lausanne
  99. with self.assertRaises(ValidationError):
  100. better_zip2.city_id = better_zip1.city_id
  101. def test_constrains_better_zip_02(self):
  102. better_zip1 = self.env['res.better.zip'].create(
  103. self.values_better_zip1())
  104. better_zip2 = self.env['res.better.zip'].create(
  105. self.values_better_zip2())
  106. with self.assertRaises(ValidationError):
  107. better_zip2.country_id = better_zip1.country_id
  108. def test_constrains_better_zip_03(self):
  109. better_zip1 = self.env['res.better.zip'].create(
  110. self.values_better_zip1())
  111. better_zip2 = self.env['res.better.zip'].create(
  112. self.values_better_zip2())
  113. with self.assertRaises(ValidationError):
  114. better_zip2.state_id = better_zip1.state_id
  115. def test_constrains_better_zip_04(self):
  116. better_zip2 = self.env['res.better.zip'].create(
  117. self.values_better_zip2())
  118. with self.assertRaises(ValidationError):
  119. better_zip2.city_id = self.city_madrid
  120. def test_constrains_partner_01(self):
  121. better_zip2 = self.env['res.better.zip'].create(
  122. self.values_better_zip2())
  123. with self.assertRaises(ValidationError):
  124. self.env['res.partner'].create({
  125. 'name': 'P1',
  126. 'zip_id': better_zip2.id,
  127. })
  128. def test_constrains_partner_02(self):
  129. better_zip2 = self.env['res.better.zip'].create(
  130. self.values_better_zip2())
  131. partner = self.env['res.partner'].create({
  132. 'name': 'P1',
  133. 'zip_id': better_zip2.id,
  134. 'country_id': better_zip2.country_id.id,
  135. 'state_id': better_zip2.state_id.id,
  136. 'city_id': better_zip2.city_id.id,
  137. })
  138. with self.assertRaises(ValidationError):
  139. partner.country_id = self.ref('base.ch')
  140. with self.assertRaises(ValidationError):
  141. partner.state_id = self.state_vd.id,
  142. with self.assertRaises(ValidationError):
  143. partner.city_id = self.city_lausanne
  144. def values_better_zip1(self):
  145. return {
  146. 'name': 1000,
  147. 'city': 'Lausanne',
  148. 'state_id': self.state_vd.id,
  149. 'country_id': self.ref('base.ch'),
  150. }
  151. def values_better_zip2(self):
  152. return {
  153. 'city_id': self.city_bcn.id,
  154. 'city': self.city_bcn.name,
  155. 'state_id': self.state_bcn.id,
  156. 'country_id': self.ref('base.es'),
  157. }
  158. def test_partner_onchange_country(self):
  159. country_es = self.browse_ref('base.es')
  160. country_es.enforce_cities = True
  161. better_zip1 = self.env['res.better.zip'].create(
  162. self.values_better_zip1())
  163. partner = self.env['res.partner'].new({
  164. 'name': 'TEST',
  165. 'zip_id': better_zip1.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. better_zip1 = self.env['res.better.zip'].create(
  172. self.values_better_zip1())
  173. partner = self.env['res.partner'].new({
  174. 'name': 'TEST',
  175. 'zip_id': better_zip1.id
  176. })
  177. self.city_bcn.country_id.enforce_cities = False
  178. partner.city_id = self.city_bcn
  179. partner._onchange_city_id()
  180. self.assertFalse(partner.zip_id)
  181. partner.city_id = False
  182. res = partner._onchange_city_id()
  183. self.assertFalse(res['domain']['zip_id'])
  184. def test_partner_onchange_state(self):
  185. better_zip1 = self.env['res.better.zip'].create(
  186. self.values_better_zip1())
  187. partner = self.env['res.partner'].new({
  188. 'name': 'TEST',
  189. 'zip_id': better_zip1.id
  190. })
  191. partner.state_id = self.state_bcn
  192. partner._onchange_state_id()
  193. self.assertFalse(partner.zip_id)
  194. def test_display_name(self):
  195. better_zip1 = self.env['res.better.zip'].create(
  196. self.values_better_zip1())
  197. self.assertEqual(
  198. better_zip1.display_name, '1000, Lausanne, Vaud, '+self.browse_ref(
  199. 'base.ch'
  200. ).name
  201. )
  202. def setUp(self):
  203. super(TestBaseLocation, self).setUp()
  204. self.state_vd = self.env['res.country.state'].create({
  205. 'name': 'Vaud',
  206. 'code': 'VD',
  207. 'country_id': self.ref('base.ch'),
  208. })
  209. self.env['res.country'].browse(self.ref('base.es')).write({
  210. 'enforce_cities': True
  211. })
  212. self.company = self.env.ref('base.main_company')
  213. self.state_bcn = self.env['res.country.state'].create({
  214. 'name': 'Barcelona',
  215. 'code': '08',
  216. 'country_id': self.ref('base.es'),
  217. })
  218. self.state_madrid = self.env['res.country.state'].create({
  219. 'name': 'Madrid',
  220. 'code': '28',
  221. 'country_id': self.ref('base.es'),
  222. })
  223. self.city_bcn = self.env['res.city'].create({
  224. 'name': 'Barcelona',
  225. 'state_id': self.state_bcn.id,
  226. 'country_id': self.ref('base.es'),
  227. })
  228. self.city_madrid = self.env['res.city'].create({
  229. 'name': 'Madrid',
  230. 'state_id': self.state_madrid.id,
  231. 'country_id': self.ref('base.es'),
  232. })
  233. self.city_lausanne = self.env['res.city'].create({
  234. 'name': 'Lausanne',
  235. 'state_id': self.state_vd.id,
  236. 'country_id': self.ref('base.ch'),
  237. })