"""Tests for Persister.""" from unittest.mock import MagicMock from unittest.mock import patch from sqlalchemy import text from sqlalchemy.exc import OperationalError from ows_product_physical.connector.mysql import db_session from ows_product_physical.models import persister from ows_product_physical import features def test_persister_delete_product_removes_metainformation( monkeypatch, valid_create_product_fields, valid_artist_id, valid_vendor_id, valid_subaccount_id, valid_project_code, db_fixture, context, feature_engine, mocker): """Assert the product metainformation is deleted for the release id.""" feature_engine.force_flag( features.CCM_PHYSICAL_SUPPLYCHAININFO_OA, False) mocker.patch.object( features, 'is_ccm_physical_supplychaininfo_oa_enabled', return_value=False) with context: create_response = persister.create_product( valid_create_product_fields, valid_vendor_id, valid_subaccount_id, valid_project_code, valid_artist_id) product_id = create_response.message.get('product_id') persister.delete_product(product_id) with db_session() as session: tables = ['release_status', 'release_subgenre', 'release_artist', 'product_physical'] for table in tables: query = \ text('SELECT * FROM {0} WHERE release_id = {1};'.format( table, product_id)) record = session.execute(query).fetchone() assert not record @patch('ows_product_physical.connector.mysql._db_session') def test_persister_delete_product_failure( db_session, valid_create_product_fields, valid_artist_id, valid_vendor_id, valid_subaccount_id, valid_project_code, db_fixture, context, feature_engine, mocker): """Assert the product is not deleted for the given release id.""" session = MagicMock() session.query = MagicMock(side_effect=OperationalError('', None, None)) db_session.return_value = session feature_engine.force_flag( features.CCM_PHYSICAL_SUPPLYCHAININFO_OA, False) mocker.patch.object( features, 'is_ccm_physical_supplychaininfo_oa_enabled', return_value=False) with context: create_response = persister.create_product( valid_create_product_fields, valid_vendor_id, valid_subaccount_id, valid_project_code, valid_artist_id) product_id = create_response.message.get('product_id') delete_response = persister.delete_product(product_id) assert delete_response.status == 500 def test_perister_delete_product_success( valid_create_product_fields, valid_artist_id, valid_vendor_id, valid_subaccount_id, valid_project_code, db_fixture, context, feature_engine, mocker): """Assert the product is deleted for the given release id.""" feature_engine.force_flag( features.CCM_PHYSICAL_SUPPLYCHAININFO_OA, False) mocker.patch.object( features, 'is_ccm_physical_supplychaininfo_oa_enabled', return_value=False) with context: create_response = persister.create_product( valid_create_product_fields, valid_vendor_id, valid_subaccount_id, valid_project_code, valid_artist_id) product_id = create_response.message.get('product_id') delete_response = persister.delete_product(product_id) assert delete_response.status == 200 with db_session() as session: product = persister._fetch_product_by_release_id( session, product_id) assert not product