Skip to content

Commit

Permalink
[IMP] connector_carepoint: Abstract address on physician
Browse files Browse the repository at this point in the history
* Implement abstract address import logic on `medical.physician`
  • Loading branch information
lasley committed Sep 9, 2016
1 parent c8c40ce commit 7eb488a
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 43 deletions.
78 changes: 35 additions & 43 deletions connector_carepoint/models/address_physician.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

import logging
from openerp import models, fields
from openerp.addons.connector.connector import ConnectorUnit
from openerp.addons.connector.unit.mapper import (mapping,
only_create,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
CarepointImporter,
)
from ..unit.import_synchronizer import DelayedBatchImporter

from .address_abstract import (CarepointAddressAbstractImportMapper,
CarepointAddressAbstractImporter,
)

_logger = logging.getLogger(__name__)

Expand All @@ -35,19 +36,13 @@ class CarepointCarepointAddressPhysician(models.Model):


class CarepointAddressPhysician(models.Model):
""" Adds the ``one2many`` relation to the Carepoint bindings
""" Adds the ``One2many`` relation to the Carepoint bindings
(``carepoint_bind_ids``)
"""
_inherits = {'carepoint.address': 'address_id'}
_name = 'carepoint.address.physician'
_inherit = 'carepoint.address.abstract'
_description = 'Carepoint Address Physician'

address_id = fields.Many2one(
string='Address',
comodel_name='carepoint.address',
required=True,
ondelete='cascade',
)
carepoint_bind_ids = fields.One2many(
comodel_name='carepoint.carepoint.address.physician',
inverse_name='odoo_id',
Expand All @@ -70,38 +65,21 @@ class CarepointAddressPhysicianBatchImporter(DelayedBatchImporter):


@carepoint
class CarepointAddressPhysicianImportMapper(CarepointImportMapper):
class CarepointAddressPhysicianImportMapper(
CarepointAddressAbstractImportMapper,
):
_model_name = 'carepoint.carepoint.address.physician'

@mapping
@only_create
def parent_id(self, record):
def partner_id(self, record):
""" It returns either the commercial partner or parent & defaults """
binder = self.binder_for('carepoint.medical.physician')
physician_id = binder.to_odoo(record['md_id'])
partner_id = self.env['medical.physician'].browse(
physician_id).partner_id
return {
'parent_id': partner_id.id,
}

@mapping
@only_create
def partner_and_address_id(self, record):
binder = self.binder_for('carepoint.carepoint.address')
address_id = binder.to_odoo(record['addr_id'])
address_id = self.env['carepoint.address'].browse(address_id)
return {
'partner_id': address_id.partner_id.id,
'address_id': address_id.id,
}

@mapping
def type(self, record):
return {'type': 'delivery'}

@mapping
def customer(self, record):
return {'customer': False}
physician_id = binder.to_odoo(record['md_id'], browse=True)
_sup = super(CarepointAddressPhysicianImportMapper, self)
return _sup.partner_id(
record, physician_id,
)

@mapping
def carepoint_id(self, record):
Expand All @@ -110,12 +88,26 @@ def carepoint_id(self, record):


@carepoint
class CarepointAddressPhysicianImporter(CarepointImporter):
class CarepointAddressPhysicianImporter(
CarepointAddressAbstractImporter,
):
_model_name = ['carepoint.carepoint.address.physician']
_base_mapper = CarepointAddressPhysicianImportMapper

def _import_dependencies(self):
""" Import depends for record """
record = self.carepoint_record
self._import_dependency(record['addr_id'],
'carepoint.carepoint.address')
super(CarepointAddressPhysicianImporter, self)._import_dependencies()
self._import_dependency(self.carepoint_record['md_id'],
'carepoint.medical.physician')


@carepoint
class CarepointAddressPhysicianUnit(ConnectorUnit):
_model_name = 'carepoint.carepoint.address.physician'

def _import_addresses(self, physician_id, partner_binding):
adapter = self.unit_for(CarepointCRUDAdapter)
importer = self.unit_for(CarepointAddressPhysicianImporter)
address_ids = adapter.search(md_id=physician_id)
for address_id in address_ids:
importer.run(address_id)
1 change: 1 addition & 0 deletions connector_carepoint/tests/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
from . import test_address_patient
from . import test_address_store
from . import test_address_organization
from . import test_address_physician
141 changes: 141 additions & 0 deletions connector_carepoint/tests/models/test_address_physician.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import mock

from openerp.addons.connector_carepoint.models import address_physician

from ...unit.backend_adapter import CarepointCRUDAdapter

from ..common import SetUpCarepointBase


_file = 'openerp.addons.connector_carepoint.models.address_physician'


class EndTestException(Exception):
pass


class AddressPhysicianTestBase(SetUpCarepointBase):

def setUp(self):
super(AddressPhysicianTestBase, self).setUp()
self.model = 'carepoint.address.physician'
self.mock_env = self.get_carepoint_helper(
self.model
)
self.record = {
'md_id': 1,
'addr_id': 2,
}


class TestAddressPhysicianImportMapper(AddressPhysicianTestBase):

def setUp(self):
super(TestAddressPhysicianImportMapper, self).setUp()
self.Unit = address_physician.CarepointAddressPhysicianImportMapper
self.unit = self.Unit(self.mock_env)

def test_partner_id_get_binder(self):
""" It should get binder for physician """
with mock.patch.object(self.unit, 'binder_for'):
self.unit.binder_for.side_effect = EndTestException
with self.assertRaises(EndTestException):
self.unit.partner_id(self.record)
self.unit.binder_for.assert_called_once_with(
'carepoint.medical.physician'
)

def test_partner_id_to_odoo(self):
""" It should get Odoo record for physician """
with mock.patch.object(self.unit, 'binder_for'):
self.unit.binder_for().to_odoo.side_effect = EndTestException
with self.assertRaises(EndTestException):
self.unit.partner_id(self.record)
self.unit.binder_for().to_odoo.assert_called_once_with(
self.record['md_id'], browse=True,
)

def test_carepoint_id(self):
""" It should return correct attribute """
res = self.unit.carepoint_id(self.record)
expect = {
'carepoint_id': '%d,%d' % (
self.record['md_id'],
self.record['addr_id'],
),
}
self.assertDictEqual(expect, res)


class TestAddressPhysicianImporter(AddressPhysicianTestBase):

def setUp(self):
super(TestAddressPhysicianImporter, self).setUp()
self.Unit = address_physician.CarepointAddressPhysicianImporter
self.unit = self.Unit(self.mock_env)
self.unit.carepoint_record = self.record

@mock.patch('%s.CarepointAddressAbstractImporter' % _file,
spec=address_physician.CarepointAddressAbstractImporter,
)
def test_import_dependencies_super(self, _super):
""" It should call the super """
_super()._import_dependencies.side_effect = EndTestException
with self.assertRaises(EndTestException):
self.unit._import_dependencies()

@mock.patch('%s.CarepointAddressAbstractImporter' % _file,
spec=address_physician.CarepointAddressAbstractImporter,
)
def test_import_dependencies_super(self, _super):
""" It should import all dependencies """
with mock.patch.object(self.unit, '_import_dependency') as mk:
self.unit._import_dependencies()
mk.assert_has_calls([
mock.call(
self.record['md_id'],
'carepoint.medical.physician',
),
])


class TestCarepointAddressPhysicianUnit(AddressPhysicianTestBase):

def setUp(self):
super(TestCarepointAddressPhysicianUnit, self).setUp()
self.Unit = address_physician.CarepointAddressPhysicianUnit
self.unit = self.Unit(self.mock_env)

def test_import_addresses_unit(self):
""" It should get units for adapter and importer """
with mock.patch.object(self.unit, 'unit_for') as mk:
mk.side_effect = [None, EndTestException]
with self.assertRaises(EndTestException):
self.unit._import_addresses(None, None)
mk.assert_has_calls([
mock.call(CarepointCRUDAdapter),
mock.call(
address_physician.CarepointAddressPhysicianImporter,
),
])

def test_import_addresses_search(self):
""" It should search adapter for filters """
physician = mock.MagicMock()
with mock.patch.object(self.unit, 'unit_for') as mk:
self.unit._import_addresses(physician, None)
mk().search.assert_called_once_with(
md_id=physician,
)

def test_import_addresses_import(self):
""" It should run importer on search results """
expect = mock.MagicMock()
with mock.patch.object(self.unit, 'unit_for') as mk:
mk().search.return_value = [expect]
self.unit._import_addresses(1, None)
mk().run.assert_called_once_with(expect)

0 comments on commit 7eb488a

Please sign in to comment.