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.

330 lines
13 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 test_name_search___can_find_using_city_name_or_zip_or_code(self):
  203. barcelona_data = {
  204. 'city_id': self.city_bcn.id,
  205. 'city': self.city_bcn.name,
  206. 'name': '444',
  207. 'code': 'BA',
  208. 'state_id': self.state_bcn.id,
  209. 'country_id': self.ref('base.es'),
  210. }
  211. madrid_data = {
  212. 'city_id': self.city_madrid.id,
  213. 'city': self.city_madrid.name,
  214. 'name': '555',
  215. 'code': 'MD',
  216. 'state_id': self.state_madrid.id,
  217. 'country_id': self.ref('base.es'),
  218. }
  219. lausanne_data = {
  220. 'city_id': self.city_lausanne.id,
  221. 'city': self.city_lausanne.name,
  222. 'name': '666',
  223. 'code': 'LA',
  224. 'state_id': self.state_vd.id,
  225. 'country_id': self.ref('base.ch'),
  226. }
  227. barcelona = self.env['res.better.zip'].create(barcelona_data)
  228. madrid = self.env['res.better.zip'].create(madrid_data)
  229. lausanne = self.env['res.better.zip'].create(lausanne_data)
  230. found_recs = self.env['res.better.zip'].name_search(name='444')
  231. self.assertEqual(len(found_recs), 1)
  232. self.assertEqual(found_recs[0][0], barcelona.id)
  233. found_recs = self.env['res.better.zip'].name_search(name='Barcelona')
  234. self.assertEqual(len(found_recs), 1)
  235. self.assertEqual(found_recs[0][0], barcelona.id)
  236. found_recs = self.env['res.better.zip'].name_search(name='BA')
  237. self.assertEqual(len(found_recs), 1)
  238. self.assertEqual(found_recs[0][0], barcelona.id)
  239. found_recs = self.env['res.better.zip'].name_search(name='555')
  240. self.assertEqual(len(found_recs), 1)
  241. self.assertEqual(found_recs[0][0], madrid.id)
  242. found_recs = self.env['res.better.zip'].name_search(name='Madrid')
  243. self.assertEqual(len(found_recs), 1)
  244. self.assertEqual(found_recs[0][0], madrid.id)
  245. found_recs = self.env['res.better.zip'].name_search(name='MD')
  246. self.assertEqual(len(found_recs), 1)
  247. self.assertEqual(found_recs[0][0], madrid.id)
  248. found_recs = self.env['res.better.zip'].name_search(name='666')
  249. self.assertEqual(len(found_recs), 1)
  250. self.assertEqual(found_recs[0][0], lausanne.id)
  251. found_recs = self.env['res.better.zip'].name_search(name='Lausanne')
  252. self.assertEqual(len(found_recs), 1)
  253. self.assertEqual(found_recs[0][0], lausanne.id)
  254. found_recs = self.env['res.better.zip'].name_search(name='LA')
  255. self.assertEqual(len(found_recs), 1)
  256. self.assertEqual(found_recs[0][0], lausanne.id)
  257. def setUp(self):
  258. super(TestBaseLocation, self).setUp()
  259. self.state_vd = self.env['res.country.state'].create({
  260. 'name': 'Vaud',
  261. 'code': 'VD',
  262. 'country_id': self.ref('base.ch'),
  263. })
  264. self.env['res.country'].browse(self.ref('base.es')).write({
  265. 'enforce_cities': True
  266. })
  267. self.company = self.env.ref('base.main_company')
  268. self.state_bcn = self.env['res.country.state'].create({
  269. 'name': 'Barcelona',
  270. 'code': '08',
  271. 'country_id': self.ref('base.es'),
  272. })
  273. self.state_madrid = self.env['res.country.state'].create({
  274. 'name': 'Madrid',
  275. 'code': '28',
  276. 'country_id': self.ref('base.es'),
  277. })
  278. self.city_bcn = self.env['res.city'].create({
  279. 'name': 'Barcelona',
  280. 'state_id': self.state_bcn.id,
  281. 'country_id': self.ref('base.es'),
  282. })
  283. self.city_madrid = self.env['res.city'].create({
  284. 'name': 'Madrid',
  285. 'state_id': self.state_madrid.id,
  286. 'country_id': self.ref('base.es'),
  287. })
  288. self.city_lausanne = self.env['res.city'].create({
  289. 'name': 'Lausanne',
  290. 'state_id': self.state_vd.id,
  291. 'country_id': self.ref('base.ch'),
  292. })