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.

192 lines
6.7 KiB

  1. # Copyright 2016-2020 Tecnativa - Pedro M. Baeza
  2. # Copyright 2020 Manuel Regidor <manuel.regidor@sygel.es>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from odoo.exceptions import UserError
  5. from odoo.tests import common
  6. class TestBaseLocationGeonamesImport(common.SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super().setUpClass()
  10. cls.country = cls.env.ref("base.mc")
  11. cls.city = cls.env["res.city"].create(
  12. {"name": "Test city", "country_id": cls.country.id}
  13. )
  14. cls.wizard = cls.env["city.zip.geonames.import"].create(
  15. {"country_ids": [(6, 0, [cls.country.id])]}
  16. )
  17. cls.wrong_country = cls.env["res.country"].create(
  18. {"name": "Wrong country", "code": "ZZYYXX"}
  19. )
  20. cls.wrong_wizard = cls.env["city.zip.geonames.import"].create(
  21. {"country_ids": [(6, 0, [cls.wrong_country.id])]}
  22. )
  23. cls.country_2 = cls.env.ref("base.li")
  24. cls.country_3 = cls.env.ref("base.sm")
  25. cls.wizard_2 = cls.env["city.zip.geonames.import"].create(
  26. {"country_ids": [(6, 0, [cls.country_2.id, cls.country_3.id])]}
  27. )
  28. cls.country_4 = cls.env.ref("base.ad")
  29. cls.wizard_3 = cls.env["city.zip.geonames.import"].create(
  30. {"country_ids": [(4, cls.country_4.id)]}
  31. )
  32. def test_import_country(self):
  33. max_import = 10
  34. self.wizard.with_context(max_import=max_import).run_import()
  35. # Look if there are imported states for the country
  36. state_count = self.env["res.country.state"].search_count(
  37. [("country_id", "=", self.country.id)]
  38. )
  39. self.assertTrue(state_count)
  40. # Look if there are imported zips
  41. zip_count = self.env["res.city.zip"].search_count(
  42. [("city_id.country_id", "=", self.country.id)]
  43. )
  44. self.assertEqual(zip_count, max_import)
  45. # Look if there are imported cities
  46. city_count = self.env["res.city"].search_count(
  47. [("country_id", "=", self.country.id)]
  48. )
  49. self.assertTrue(city_count)
  50. # Reimport again to see that there's no duplicates
  51. self.wizard.with_context(max_import=max_import).run_import()
  52. state_count2 = self.env["res.country.state"].search_count(
  53. [("country_id", "=", self.country.id)]
  54. )
  55. self.assertEqual(state_count, state_count2)
  56. city_count2 = self.env["res.city"].search_count(
  57. [("country_id", "=", self.country.id)]
  58. )
  59. self.assertEqual(city_count, city_count2)
  60. zip_count = self.env["res.city.zip"].search_count(
  61. [("city_id.country_id", "=", self.country.id)]
  62. )
  63. self.assertEqual(zip_count, max_import)
  64. def test_delete_old_entries(self):
  65. zip_entry = self.env["res.city.zip"].create(
  66. {"name": "Brussels", "city_id": self.city.id}
  67. )
  68. self.wizard.run_import()
  69. self.assertFalse(zip_entry.exists())
  70. city_entry = self.env["res.city"].create(
  71. {"name": "Test city", "country_id": self.country.id}
  72. )
  73. self.wizard.run_import()
  74. self.assertFalse(city_entry.exists())
  75. def test_import_title(self):
  76. self.wizard.letter_case = "title"
  77. self.wizard.with_context(max_import=1).run_import()
  78. city_zip = self.env["res.city.zip"].search(
  79. [("city_id.country_id", "=", self.country.id)], limit=1
  80. )
  81. self.assertEqual(city_zip.city_id.name, city_zip.city_id.name.title())
  82. city = self.env["res.city"].search(
  83. [("country_id", "=", self.country.id)], limit=1
  84. )
  85. self.assertEqual(city.name, city.name.title())
  86. def test_import_upper(self):
  87. self.wizard.letter_case = "upper"
  88. self.wizard.with_context(max_import=1).run_import()
  89. city_zip = self.env["res.city.zip"].search(
  90. [("city_id.country_id", "=", self.country.id)], limit=1
  91. )
  92. self.assertEqual(city_zip.city_id.name, city_zip.city_id.name.upper())
  93. city = self.env["res.city"].search(
  94. [("country_id", "=", self.country.id)], limit=1
  95. )
  96. self.assertEqual(city.name, city.name.upper())
  97. def test_download_error(self):
  98. """Check that we get an error when trying to download
  99. with a wrong country code"""
  100. with self.assertRaises(UserError):
  101. self.wrong_wizard.run_import()
  102. def test_import_duplicated_city_name(self):
  103. country = self.env.ref("base.us")
  104. self.wizard.country_ids = [(6, 0, country.ids)]
  105. parsed_csv = [
  106. [
  107. "US",
  108. "95602",
  109. "Auburn",
  110. " California",
  111. "CA",
  112. "Placer",
  113. "61",
  114. "38.9829",
  115. "-121.0944",
  116. "4",
  117. ],
  118. [
  119. "US",
  120. "95603",
  121. "Auburn",
  122. " California",
  123. "CA",
  124. "Placer",
  125. "61",
  126. "38.9115",
  127. "-121.08",
  128. "4",
  129. ],
  130. [
  131. "US",
  132. "30011",
  133. "Auburn",
  134. " Georgia",
  135. "GA",
  136. "Barrow",
  137. "13",
  138. "34.0191",
  139. "-83.8261",
  140. "4",
  141. ],
  142. ]
  143. self.wizard._process_csv(parsed_csv, country)
  144. cities = self.env["res.city"].search([("name", "=", "Auburn")])
  145. self.assertEqual(len(cities), 2)
  146. mapping = [
  147. ["California", "95602"],
  148. ["California", "95603"],
  149. ["Georgia", "30011"],
  150. ]
  151. for state_name, zip_code in mapping:
  152. zip_entry = self.env["res.city.zip"].search(
  153. [("city_id.country_id", "=", country.id), ("name", "=", zip_code)]
  154. )
  155. state = self.env["res.country.state"].search(
  156. [("country_id", "=", country.id), ("name", "=", state_name)]
  157. )
  158. self.assertEqual(
  159. zip_entry.city_id.state_id,
  160. state,
  161. "Incorrect state for {} {}".format(state_name, zip_code),
  162. )
  163. def test_import_countries(self):
  164. max_import = 1
  165. self.wizard_2.with_context(max_import=max_import).run_import()
  166. # Look if there are imported zips
  167. zip_country_2_count = self.env["res.city.zip"].search_count(
  168. [("city_id.country_id", "=", self.country_2.id)]
  169. )
  170. zip_country_3_count = self.env["res.city.zip"].search_count(
  171. [("city_id.country_id", "=", self.country_3.id)]
  172. )
  173. self.assertEqual(zip_country_2_count, max_import)
  174. self.assertEqual(zip_country_3_count, max_import)