"""Tests for Persister.""" from unittest.mock import MagicMock from unittest.mock import Mock import sentry_sdk from sqlalchemy.exc import OperationalError from ows_product_physical.connector.mysql import db_session from ows_product_physical.models import ows_marketing from ows_product_physical.models import persister def test_persister_fetch_product( monkeypatch, db_fixture, valid_post_product_sql_object): """Assert product fetch successful.""" mock_response = Mock() mock_response.rowcount = 1 mock_response.fetchone = MagicMock( return_value=valid_post_product_sql_object) session = Mock() session.execute = MagicMock(return_value=mock_response) monkeypatch.setattr( ows_marketing, 'get_product_highlight_by_product_id', value=MagicMock()) resp = persister._fetch_product_by_release_id(session, 47) assert session.execute.called assert resp.status == 200 def test_persister_fetch_product_required_fields_only( monkeypatch, db_fixture, valid_post_product_sql_object_required_fields_only ): """Test success for fetching physical product with required fields only.""" mock_response = Mock() mock_response.rowcount = 1 mock_response.fetchone = MagicMock( return_value=valid_post_product_sql_object_required_fields_only) session = Mock() session.execute = MagicMock(return_value=mock_response) monkeypatch.setattr( ows_marketing, 'get_product_highlight_by_product_id', value=MagicMock()) resp = persister._fetch_product_by_release_id(session, 47) assert session.execute.called assert resp.status == 200 def test_persister_fetch_product_not_found(db_fixture): """Test getting a product that doesn't exist.""" with db_session() as session: resp = persister._fetch_product_by_release_id(session, 47) assert resp.status == 404 def test_persister_fetch_product_fail( monkeypatch, db_fixture, valid_post_product_data, valid_create_response_error): """Assert product fetch exception is captured.""" monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) session = Mock() session.execute = MagicMock(side_effect=OperationalError('', None, None)) monkeypatch.setattr( ows_marketing, 'get_product_highlight_by_product_id', value=MagicMock()) resp = persister._fetch_product_by_release_id( session, 47) assert resp.status == 500 assert sentry_sdk.capture_exception.called def test_persister_fetch_product_multiple_primary_artists( monkeypatch, db_with_data): """Assert products primary artists concatenated.""" monkeypatch.setattr( ows_marketing, 'get_product_highlight_by_product_id', value=MagicMock()) with db_session() as session: resp = persister._fetch_product_by_release_id(session, 42) assert resp.message['primary_artist'] == 'Hawaiian People, James Cook' assert resp.status == 200 def test_persister_gets_display_upc( monkeypatch, db_with_data): """Assert products primary artists concatenated.""" monkeypatch.setattr( ows_marketing, 'get_product_highlight_by_product_id', value=MagicMock()) with db_session() as session: resp = persister._fetch_product_by_release_id(session, 42) assert resp.message['display_upc'] == '0666677778888' assert resp.message['upc'] == 666677778888 assert resp.status == 200