diff --git a/connector_carepoint/tests/test_backend_adapter.py b/connector_carepoint/tests/test_backend_adapter.py index 3e3626f..33ec89c 100644 --- a/connector_carepoint/tests/test_backend_adapter.py +++ b/connector_carepoint/tests/test_backend_adapter.py @@ -124,12 +124,18 @@ def test_read_searches(self): attr_expect, ) - def test_read_returns(self): + def test_read_returns_first(self): """ It should return first record result """"" with self.mock_api() as api: res = self._init_model().read(123, ['expect', 'no_expect']) self.assertEqual(api().search()[0], res) + def test_read_returns_all(self): + """ It should return first record result """"" + with self.mock_api() as api: + res = self._init_model().read(123, ['expect', 'no_expect'], True) + self.assertEqual(api().search(), res) + def test_read_image_gets_file(self): """ It should get proper file path from server """ with self.mock_api() as api: @@ -248,21 +254,40 @@ def test_delete_returns_result(self): res = self._init_model().delete(123) self.assertEqual(api().delete(), res) - def test_write_gets_session(self): - """ It should get session for model """ + def test_write_reads(self): + """ It should get record for id """ + expect1, expect2 = 123, {'test': 'TEST'} + with self.mock_api(): + model = self._init_model() + with mock.patch.object(model, 'read') as read: + model.write(expect1, expect2) + read.assert_called_once_with( + expect1, + return_all=True, + ) + + def test_write_updates(self): + """ It should update record w/ data """ + expect1, expect2 = 123, {'test': 'TEST'} with self.mock_api() as api: - self._init_model().write(None, None) - api()._get_session.assert_called_once_with( - api()[self.api_camel], + self._init_model().write(expect1, expect2) + api().search().update.assert_called_once_with( + expect2, ) - def test_write_returns_result(self): - """ It should return result of write operation """ + def test_write_commits(self): + """ It should commit update to session """ + expect1, expect2 = 123, {'test': 'TEST'} with self.mock_api() as api: - res = self._init_model().write( - 123, {'data': 'test', 'col': 12323423}, - ) + self._init_model().write(expect1, expect2) + api().search().session.commit.assert_called_once_with() + + def test_write_returns(self): + """ It should return record object """ + expect1, expect2 = 123, {'test': 'TEST'} + with self.mock_api() as api: + res = self._init_model().write(expect1, expect2) self.assertEqual( - api()._do_queries(), - res, + api().search(), + res ) diff --git a/connector_carepoint/unit/backend_adapter.py b/connector_carepoint/unit/backend_adapter.py index 2f4088e..7a33cb5 100644 --- a/connector_carepoint/unit/backend_adapter.py +++ b/connector_carepoint/unit/backend_adapter.py @@ -56,14 +56,14 @@ def search(self, **filters): res = self.carepoint.search(model_obj, filters, [pk]) return [getattr(row, pk) for row in res] - def read(self, _id, attributes=None): + def read(self, _id, attributes=None, return_all=False): """ Gets record by id and returns the object :param _id: Id of record to get from Db. Can be comma sep str for multiple indexes :type _id: mixed :param attributes: Attributes to rcv from db. None for * :type attributes: list or None - :rtype: dict + :rtype: :class:`sqlalchemy.engine.ResultProxy` @TODO: Fix the conjoined index lookups, this is pretty flaky """ @@ -76,7 +76,8 @@ def read(self, _id, attributes=None): domain[pks[idx]] = id_part except AttributeError: domain[pks[0]] = _id - return self.carepoint.search(model_obj, domain, attributes)[0] + res = self.carepoint.search(model_obj, domain, attributes) + return res if return_all else res[0] def read_image(self, path): """ Returns an image resource from CarePoint @@ -136,16 +137,10 @@ def write(self, _id, data): :type data: dict :rtype: :class:`sqlalchemy.engine.ResultProxy` """ - model_obj = self.__get_cp_model() - session = self.carepoint._get_session(model_obj) - - def __update(): - record = self.read(_id) - for key, val in data.iteritems(): - setattr(record, key, val) - return record - - return self.carepoint._do_queries(session, __update) + record = self.read(_id, return_all=True) + record.update(data) + record.session.commit() + return record def delete(self, _id): """ Delete record on the external system