"""Test Releases Model.""" from unittest.mock import MagicMock from unittest.mock import Mock from unittest.mock import patch import sentry_sdk from sqlalchemy.exc import SQLAlchemyError from ows_product_physical import features from ows_product_physical.connector.mysql import db_session from ows_product_physical.models import product_physical_supply_chain_info from ows_product_physical.models \ import product_physical_supply_chain_metadata as \ product_physical_supply_chain_metadata_model from tests.utils import db def test_get_product_phsical_supply_chain_metadata_success( db_fixture, feature_engine, mock_app, expected_response_for_supply_chain_metadata): """Test successful retrieval of metadata of supply chain.""" feature_engine.force_flag( features.OA_PHYSICAL_SUPPLY_CHAIN_METADATA_CARVED_IN, False) db.insert_product_physcial_supply_chain_metadata() response = product_physical_supply_chain_metadata_model. \ get_product_supply_chain_metadata(1900609) assert response.message['items'] == \ expected_response_for_supply_chain_metadata def test_get_supply_chain_metadata_with_supply_chain_info_success( db_fixture, feature_engine, mock_app, expected_response_for_metadata_with_supply_chain_info, monkeypatch, product_physical_supply_chain_info_record): """Test successful retrieval of metadata of supply chain.""" feature_engine.force_flag( features.OA_PHYSICAL_SUPPLY_CHAIN_METADATA_CARVED_IN, True) db.insert_supply_chain_metadata_with_supply_chain_info() monkeypatch.setattr( product_physical_supply_chain_info, 'get_product_supply_chain_info_by_product_store_id', value=MagicMock( return_value=product_physical_supply_chain_info_record)) response = product_physical_supply_chain_metadata_model. \ get_product_supply_chain_metadata(1900609) assert response.message['items'] == \ expected_response_for_metadata_with_supply_chain_info @patch('ows_product_physical.connector.mysql._db_session') def test_get_product_physical_supply_chain_metadata_failure( db_session, db_with_data, db_fixture, monkeypatch, mocker, mock_app, data_to_be_inserted, valid_create_response_error, feature_engine): """Test failure in retrieval of metadata of supply chain.""" feature_engine.force_flag( features.OA_PHYSICAL_SUPPLY_CHAIN_METADATA_CARVED_IN, False) monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) session = Mock() session.flush = MagicMock(side_effect=SQLAlchemyError()) db_session.return_value = session result = product_physical_supply_chain_metadata_model. \ get_product_supply_chain_metadata(data_to_be_inserted) assert result.status == 500 assert sentry_sdk.capture_exception.called def test_set_physical_supply_chain_metadata_update_success( db_fixture, data_to_be_inserted, mocker, data_returned_for_existing_metadata): """Test successful update of metadata.""" db.insert_product_physcial_supply_chain_metadata() mocker.patch.object( product_physical_supply_chain_metadata_model, '_set_existing_store_with_override_dates', return_value=data_returned_for_existing_metadata, autospec=True) result = product_physical_supply_chain_metadata_model. \ set_physical_supply_chain_metadata(1900609, data_to_be_inserted) assert result.message[0]['product_id'] == 1900609 def test_set_physical_supply_chain_metadata_create_success( db_fixture, data_to_be_inserted, mocker, data_returned_for_new_metadata, updated_data, data_returned_after_create_mapping): """Test successful insertion or update of metadata.""" db.insert_product_physcial_supply_chain_metadata() mocker.patch.object( product_physical_supply_chain_metadata_model, '_set_existing_store_with_override_dates', return_value=data_returned_for_new_metadata, autospec=True) mocker.patch.object( product_physical_supply_chain_metadata_model, '_create_store_with_override_dates', return_value=updated_data, autospec=True) mocker.patch.object( product_physical_supply_chain_metadata_model, '_map_created_result', return_value=data_returned_after_create_mapping, autospec=True) result = product_physical_supply_chain_metadata_model. \ set_physical_supply_chain_metadata(1900609, data_to_be_inserted) assert result.message[0]['product_id'] == 1900609 def test_set_existing_store_with_override_dates( transactional_data, monkeypatch, db_fixture): """Test successful update of metadata.""" transaction_date = None db.insert_product_physcial_supply_chain_metadata() with db_session() as session: result = product_physical_supply_chain_metadata_model. \ _set_existing_store_with_override_dates( transactional_data, 6, 1900609, session, transaction_date) assert result['product_id'] == 1900609 def test_set_existing_store_with_override_dates_for_deleted( transactional_data, monkeypatch, db_fixture): """Test failure on update of metadata.""" transaction_date = None db.insert_deleted_product_physcial_supply_chain_metadata() with db_session() as session: result = product_physical_supply_chain_metadata_model. \ _set_existing_store_with_override_dates( transactional_data, 6, 1900609, session, transaction_date) assert not result['product_id'] == 1900609 def test_create_store_with_override_dates( created_metadata, monkeypatch, db_fixture, ): """Test successful insertion of metadata.""" session = Mock() transaction_date = None db.insert_product_physcial_supply_chain_metadata() result = product_physical_supply_chain_metadata_model. \ _create_store_with_override_dates( created_metadata, 739, 1900609, session, transaction_date) assert result.product_id == 1900609 @patch('ows_product_physical.connector.mysql._db_session') def test_set_product_physical_supply_chain_metadata_failure( db_session, db_with_data, db_fixture, monkeypatch, mocker, valid_create_response_error): """Test failure to set in metadata of supply chain.""" monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) session = Mock() session.flush = MagicMock(side_effect=SQLAlchemyError()) db_session.return_value = session result = product_physical_supply_chain_metadata_model. \ set_physical_supply_chain_metadata(1900609, None) assert result.status == 500 assert sentry_sdk.capture_exception.called def test_delete_supplychain_metadata_success( db_fixture, fixture_response_ok): """Test successful soft deletion of supply chain metadata.""" db.insert_product_physcial_supply_chain_metadata() result = product_physical_supply_chain_metadata_model. \ delete_supply_chain_metadata(1) assert result.message['status'] == fixture_response_ok.message['status'] def test_delete_supply_chain_metadata_by_product_and_store( db_fixture, fixture_response_ok): """Test delete_supply_chain_metadata_by_product method. Test successful soft deletion of supply chain metadata by product and store_id. """ db.insert_product_physcial_supply_chain_metadata() result = product_physical_supply_chain_metadata_model. \ delete_supply_chain_metadata_by_product_and_store( 1900609, 6) assert result.message['status'] == fixture_response_ok.message['status'] def test_get_supply_chain_metadata_with_initial_stock( db_fixture, feature_engine, mock_app, expected_response_for_supply_chain_metadata_with_initial_stock): """Test successful retrieval of metadata of supply chain.""" feature_engine.force_flag( features.OA_PHYSICAL_SUPPLY_CHAIN_METADATA_CARVED_IN, False) db.insert_product_physcial_supply_chain_metadata_with_initial_stock() response = product_physical_supply_chain_metadata_model. \ get_product_supply_chain_metadata(1900609) assert response.message['items'] == \ expected_response_for_supply_chain_metadata_with_initial_stock