diff --git a/base_location_geonames_import/tests/test_base_location_geonames_import.py b/base_location_geonames_import/tests/test_base_location_geonames_import.py index 82874ae74..303e6ad67 100644 --- a/base_location_geonames_import/tests/test_base_location_geonames_import.py +++ b/base_location_geonames_import/tests/test_base_location_geonames_import.py @@ -5,12 +5,13 @@ from odoo.tests import common -class TestBaseLocationGeonamesImport(common.TransactionCase): - def setUp(self): - super(TestBaseLocationGeonamesImport, self).setUp() - self.country = self.env.ref('base.mc') - self.wizard = self.env['better.zip.geonames.import'].create({ - 'country_id': self.country.id, +class TestBaseLocationGeonamesImport(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestBaseLocationGeonamesImport, cls).setUpClass() + cls.country = cls.env.ref('base.mc') + cls.wizard = cls.env['better.zip.geonames.import'].create({ + 'country_id': cls.country.id, }) def test_import_country(self): diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index b31afd5af..c3c3e1266 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -4,7 +4,7 @@ # © 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models, fields, api, _ +from odoo import _, api, fields, models, tools from odoo.exceptions import UserError import requests import tempfile @@ -86,28 +86,30 @@ class BetterZipGeonamesImport(models.TransientModel): vals = self._prepare_better_zip(row, country) if vals: return zip_model.create(vals) - else: + else: # pragma: no cover return False + @tools.ormcache('country_id', 'code') + def _get_state(self, country_id, code, name): + state = self.env['res.country.state'].search( + [('country_id', '=', country_id), + ('code', '=', code)], limit=1, + ) + if state: # pragma: no cover + return state + else: + return self.env['res.country.state'].create({ + 'name': name, + 'code': code, + 'country_id': country_id, + }) + @api.model def select_or_create_state( self, row, country, code_row_index=4, name_row_index=3): - states = self.env['res.country.state'].search([ - ('country_id', '=', country.id), - ('code', '=', row[code_row_index]), - ]) - if len(states) > 1: - raise UserError( - _("Too many states with code %s for country %s") - % (row[code_row_index], country.code)) - if len(states) == 1: - return states[0] - else: - return self.env['res.country.state'].create({ - 'name': row[name_row_index], - 'code': row[code_row_index], - 'country_id': country.id - }) + return self._get_state( + country.id, row[code_row_index], row[name_row_index], + ) @api.multi def run_import(self):