"""Tests for Persister.""" from unittest.mock import MagicMock from unittest.mock import patch import sentry_sdk from sqlalchemy.exc import SQLAlchemyError from ows_product_physical.models import persister from ows_product_physical.models.product_physical_packaging import \ ProductPhysicalPackaging def _response_helper(packaging_response): coll = [] for item in packaging_response: coll.append(ProductPhysicalPackaging(id=item['id'], name=item['name'])) return coll @patch('ows_product_physical.connector.mysql._db_session') def test_persister_get_product_physical_packaging_sucesss( db_session, db_fixture, db_with_data, valid_get_product_physical_packaging_response): """Assert the get packaging was called properly in success case.""" session = MagicMock() session.query = MagicMock() session.query.all = MagicMock(return_value=lambda: _response_helper( valid_get_product_physical_packaging_response)) db_session.return_value = session resp = persister.get_product_physical_packaging() assert session.query.called assert session.query.call_args[0][0] == ProductPhysicalPackaging assert resp.status == 200 @patch('ows_product_physical.connector.mysql._db_session') def test_persister_get_product_physical_packaging_fail( db_session, monkeypatch, db_fixture, db_with_data, valid_get_product_physical_packaging_response): """Assert the get packaging was called properly in fail case.""" monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) session = MagicMock() session.query = MagicMock(side_effect=SQLAlchemyError()) db_session.return_value = session resp = persister.get_product_physical_packaging() assert resp.status == 500 assert sentry_sdk.capture_exception.called