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.

111 lines
3.9 KiB

  1. # Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests import common
  4. from odoo.exceptions import UserError
  5. class TestBaseLocationGeonamesImport(common.SavepointCase):
  6. @classmethod
  7. def setUpClass(cls):
  8. super().setUpClass()
  9. cls.country = cls.env.ref('base.mc')
  10. cls.city = cls.env['res.city'].create({
  11. 'name': 'Test city',
  12. 'country_id': cls.country.id,
  13. })
  14. cls.wizard = cls.env['city.zip.geonames.import'].create({
  15. 'country_id': cls.country.id,
  16. })
  17. cls.wrong_country = cls.env['res.country'].create({
  18. 'name': 'Wrong country',
  19. 'code': 'ZZYYXX',
  20. })
  21. cls.wrong_wizard = cls.env['city.zip.geonames.import'].create({
  22. 'country_id': cls.wrong_country.id,
  23. })
  24. def test_import_country(self):
  25. max_import = 10
  26. self.wizard.with_context(max_import=max_import).run_import()
  27. # Look if there are imported states for the country
  28. state_count = self.env['res.country.state'].search_count([
  29. ('country_id', '=', self.country.id)
  30. ])
  31. self.assertTrue(state_count)
  32. # Look if there are imported zips
  33. zip_count = self.env['res.city.zip'].search_count([
  34. ('city_id.country_id', '=', self.country.id)
  35. ])
  36. self.assertEqual(zip_count, max_import)
  37. # Look if there are imported cities
  38. city_count = self.env['res.city'].search_count([
  39. ('country_id', '=', self.country.id)
  40. ])
  41. self.assertTrue(city_count)
  42. # Reimport again to see that there's no duplicates
  43. self.wizard.with_context(max_import=max_import).run_import()
  44. state_count2 = self.env['res.country.state'].search_count([
  45. ('country_id', '=', self.country.id)
  46. ])
  47. self.assertEqual(state_count, state_count2)
  48. city_count2 = self.env['res.city'].search_count([
  49. ('country_id', '=', self.country.id)
  50. ])
  51. self.assertEqual(city_count, city_count2)
  52. zip_count = self.env['res.city.zip'].search_count([
  53. ('city_id.country_id', '=', self.country.id)
  54. ])
  55. self.assertEqual(zip_count, max_import)
  56. def test_delete_old_entries(self):
  57. zip_entry = self.env['res.city.zip'].create({
  58. 'name': 'Brussels',
  59. 'city_id': self.city.id,
  60. })
  61. self.wizard.run_import()
  62. self.assertFalse(zip_entry.exists())
  63. city_entry = self.env['res.city'].create({
  64. 'name': 'Test city',
  65. 'country_id': self.country.id,
  66. })
  67. self.wizard.run_import()
  68. self.assertFalse(city_entry.exists())
  69. def test_import_title(self):
  70. self.wizard.letter_case = 'title'
  71. self.wizard.with_context(max_import=1).run_import()
  72. zip = self.env['res.city.zip'].search(
  73. [('city_id.country_id', '=', self.country.id)], limit=1
  74. )
  75. self.assertEqual(zip.city_id.name, zip.city_id.name.title())
  76. city = self.env['res.city'].search(
  77. [('country_id', '=', self.country.id)], limit=1
  78. )
  79. self.assertEqual(city.name, city.name.title())
  80. def test_import_upper(self):
  81. self.wizard.letter_case = 'upper'
  82. self.wizard.with_context(max_import=1).run_import()
  83. zip = self.env['res.city.zip'].search(
  84. [('city_id.country_id', '=', self.country.id)], limit=1
  85. )
  86. self.assertEqual(zip.city_id.name, zip.city_id.name.upper())
  87. city = self.env['res.city'].search(
  88. [('country_id', '=', self.country.id)], limit=1
  89. )
  90. self.assertEqual(city.name, city.name.upper())
  91. def test_download_error(self):
  92. """Check that we get an error when trying to download
  93. with a wrong country code"""
  94. with self.assertRaises(UserError):
  95. self.wrong_wizard.run_import()