Browse Source

[IMP] partner_external_map: Add tests

pull/505/head
Pedro M. Baeza 9 years ago
parent
commit
f46914a90b
  1. 6
      partner_external_map/README.rst
  2. 4
      partner_external_map/hooks.py
  3. 2
      partner_external_map/models/map_website.py
  4. 6
      partner_external_map/models/res_partner.py
  5. 12
      partner_external_map/models/res_users.py
  6. 3
      partner_external_map/tests/__init__.py
  7. 84
      partner_external_map/tests/test_partner_external_map.py

6
partner_external_map/README.rst

@ -57,6 +57,12 @@ or a route map.
:alt: Try me on Runbot :alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/134/9.0 :target: https://runbot.odoo-community.org/runbot/134/9.0
Known issues / Roadmap
======================
* Let decide if the user prefers to use addresses instead coordinates when
*base_geolocalize* is installed.
Bug Tracker Bug Tracker
=========== ===========

4
partner_external_map/hooks.py

@ -10,7 +10,7 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def set_default_map_settings(cr, pool):
def set_default_map_settings(cr, registry):
"""Method called as post-install script """Method called as post-install script
The default method on the field can't be used, because it would be executed The default method on the field can't be used, because it would be executed
before loading map_website_data.xml, so it would not be able to set a before loading map_website_data.xml, so it would not be able to set a
@ -18,7 +18,7 @@ def set_default_map_settings(cr, pool):
with api.Environment.manage(): with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {}) env = api.Environment(cr, SUPERUSER_ID, {})
user_model = env['res.users'] user_model = env['res.users']
users = user_model.search([])
users = user_model.search([('context_map_website_id', '=', False)])
logger.info('Updating user settings for maps...') logger.info('Updating user settings for maps...')
users.write({ users.write({
'context_map_website_id': user_model._default_map_website().id, 'context_map_website_id': user_model._default_map_website().id,

2
partner_external_map/models/map_website.py

@ -3,7 +3,7 @@
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com> # © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import models, fields, api, _
from openerp import models, fields
class MapWebsite(models.Model): class MapWebsite(models.Model):

6
partner_external_map/models/res_partner.py

@ -3,7 +3,7 @@
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com> # © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import models, fields, api, _
from openerp import models, api, _
from openerp.exceptions import Warning as UserError from openerp.exceptions import Warning as UserError
import logging import logging
@ -45,6 +45,7 @@ class ResPartner(models.Model):
@api.multi @api.multi
def open_map(self): def open_map(self):
self.ensure_one()
map_website = self.env.user.context_map_website_id map_website = self.env.user.context_map_website_id
if not map_website: if not map_website:
raise UserError( raise UserError(
@ -72,6 +73,7 @@ class ResPartner(models.Model):
@api.multi @api.multi
def open_route_map(self): def open_route_map(self):
self.ensure_one()
if not self.env.user.context_route_map_website_id: if not self.env.user.context_route_map_website_id:
raise UserError( raise UserError(
_('Missing route map website: ' _('Missing route map website: '
@ -87,7 +89,7 @@ class ResPartner(models.Model):
self.partner_latitude and self.partner_longitude and self.partner_latitude and self.partner_longitude and
start_partner.partner_latitude and start_partner.partner_latitude and
start_partner.partner_longitude): start_partner.partner_longitude):
url = self._prepare_url(
url = self._prepare_url( # pragma: no cover
map_website.route_lat_lon_url, { map_website.route_lat_lon_url, {
'{START_LATITUDE}': start_partner.partner_latitude, '{START_LATITUDE}': start_partner.partner_latitude,
'{START_LONGITUDE}': start_partner.partner_longitude, '{START_LONGITUDE}': start_partner.partner_longitude,

12
partner_external_map/models/res_users.py

@ -3,7 +3,7 @@
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com> # © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import models, fields, api, _
from openerp import models, fields, api
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -40,3 +40,13 @@ class ResUsers(models.Model):
"form to display an itinerary.") "form to display an itinerary.")
context_route_start_partner_id = fields.Many2one( context_route_start_partner_id = fields.Many2one(
'res.partner', string='Start Address for Route Map') 'res.partner', string='Start Address for Route Map')
@api.model
def create(self, vals):
"""On creation, if no starting partner is provided, assign the current
created one.
"""
user = super(ResUsers, self).create(vals)
if not vals.get('context_route_start_partner_id'):
user.context_route_start_partner_id = user.partner_id.id
return user

3
partner_external_map/tests/__init__.py

@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import test_partner_external_map

84
partner_external_map/tests/test_partner_external_map.py

@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
# © 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp.tests import common
from openerp.exceptions import Warning as UserError
from ..hooks import set_default_map_settings
class TestPartnerExternalMap(common.TransactionCase):
def setUp(self):
super(TestPartnerExternalMap, self).setUp()
self.user = self.env['res.users'].create({
'name': 'Test user',
'login': 'test_login',
'context_map_website_id': self.ref(
'partner_external_map.google_maps'),
'context_route_map_website_id': self.ref(
'partner_external_map.google_maps'),
})
self.user.partner_id.city = 'Tomelloso'
self.partner = self.env['res.partner'].create({
'name': 'Test partner',
'city': 'Madrid',
})
def test_post_init_hook(self):
# Call this again for coverage purposes, but it has been already run
set_default_map_settings(self.cr, self.registry)
self.assertTrue(self.env.user.context_map_website_id)
self.assertTrue(self.env.user.context_route_map_website_id)
self.assertEqual(self.env.user.partner_id,
self.env.user.context_route_start_partner_id)
def test_create_user(self):
self.assertEqual(
self.user.partner_id, self.user.context_route_start_partner_id)
def test_open_map(self):
action = self.partner.sudo(self.user.id).open_map()
self.assertEqual(
action['url'], "https://www.google.com/maps?ie=UTF8&q=Madrid")
def test_open_route_map(self):
action = self.partner.sudo(self.user.id).open_route_map()
self.assertEqual(
action['url'], "https://www.google.com/maps?saddr=Tomelloso&daddr="
"Madrid&directionsmode=driving")
def test_open_map_with_coordinates(self):
# Simulate that we have the base_geolocalize module installed creating
# by hand the variables - This can't be done with routes
partner = self.partner.sudo(self.user.id)
partner.partner_latitude = 39.15837
partner.partner_longitude = -3.02145
action = partner.open_map()
self.assertEqual(
action['url'],
"https://www.google.com/maps?z=15&q=39.15837,-3.02145")
def test_exception_no_map_website(self):
self.user.context_map_website_id = False
with self.assertRaises(UserError):
self.partner.sudo(self.user.id).open_map()
def test_exception_no_map_route_website(self):
self.user.context_route_start_partner_id = False
with self.assertRaises(UserError):
self.partner.sudo(self.user.id).open_route_map()
def test_exception_no_starting_partner(self):
self.user.context_route_map_website_id = False
with self.assertRaises(UserError):
self.partner.sudo(self.user.id).open_route_map()
def test_exception_no_address_url(self):
self.user.context_map_website_id.address_url = False
with self.assertRaises(UserError):
self.partner.sudo(self.user.id).open_map()
def test_exception_no_route_address_url(self):
self.user.context_map_website_id.route_address_url = False
with self.assertRaises(UserError):
self.partner.sudo(self.user.id).open_route_map()
Loading…
Cancel
Save