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.

144 lines
5.3 KiB

  1. # Copyright 2016-2019 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_entry = self.env['res.city.zip'].search(
  73. [('city_id.country_id', '=', self.country.id)], limit=1
  74. )
  75. self.assertEqual(zip_entry.city_id.name, zip_entry.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_entry = self.env['res.city.zip'].search(
  84. [('city_id.country_id', '=', self.country.id)], limit=1
  85. )
  86. self.assertEqual(zip_entry.city_id.name, zip_entry.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()
  96. def test_import_duplicated_city_name(self):
  97. country = self.env.ref('base.us')
  98. self.wizard.country_id = country.id
  99. parsed_csv = [
  100. ['US', '95602', 'Auburn', ' California', 'CA', 'Placer', '61',
  101. '38.9829', '-121.0944', '4'],
  102. ['US', '95603', 'Auburn', ' California', 'CA', 'Placer', '61',
  103. '38.9115', '-121.08', '4'],
  104. ['US', '30011', 'Auburn', ' Georgia', 'GA', 'Barrow', '13',
  105. '34.0191', '-83.8261', '4'],
  106. ]
  107. self.wizard._process_csv(parsed_csv)
  108. cities = self.env['res.city'].search([('name', '=', 'Auburn')])
  109. self.assertEqual(len(cities), 2)
  110. mapping = [
  111. ['California', '95602'],
  112. ['California', '95603'],
  113. ['Georgia', '30011'],
  114. ]
  115. for state_name, zip_code in mapping:
  116. zip_entry = self.env['res.city.zip'].search([
  117. ('city_id.country_id', '=', country.id),
  118. ('name', '=', zip_code),
  119. ])
  120. state = self.env['res.country.state'].search([
  121. ('country_id', '=', country.id),
  122. ('name', '=', state_name),
  123. ])
  124. self.assertEqual(
  125. zip_entry.city_id.state_id, state,
  126. "Incorrect state for %s %s" % (state_name, zip_code),
  127. )