"""Tests for import asset detail model.""" from unittest.mock import patch import pytest from sqlalchemy.exc import SQLAlchemyError from assets.models.legacy import import_asset_detail from tests.models.legacy import ar_operations @pytest.fixture def import_asset_detail_data(): """Get data to seed import_asset_detail table.""" return [ { 'id': 20, 'import_asset_id': 30, 'upc': 10001, 'track_id': 1 } ] @pytest.fixture def db_fixture(import_asset_detail_data): """Set up the import asset detail table.""" ar_operations.create_tables() ar_operations.seed_import_asset_detail_table(import_asset_detail_data) def test_create_import_asset_detail(db_fixture): """Test import asset detail record creation.""" expected_asset_id = 10 expected_upc = 111222333 expected_track_id = 101 response = import_asset_detail.create_import_asset_detail( import_asset_id=expected_asset_id, upc=expected_upc, track_id=expected_track_id) assert response.status assert response.message['import_asset_id'] == expected_asset_id assert response.message['upc'] == expected_upc assert response.message['track_id'] == expected_track_id @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_create_import_asset_batch_error(monkeypatch): """Test import asset detail record creation with exception.""" response = import_asset_detail.create_import_asset_detail( import_asset_id=1, upc=2, track_id=3) assert response.status == 500 def test_get_by_import_asset_id_success(db_fixture): """Test get_by_import_asset_id success.""" response = import_asset_detail.get_by_import_asset_id(import_asset_id=30) assert response assert response.message['upc'] == 10001 def test_get_by_import_asset_id_not_found(db_fixture): """Test get_by_import_asset_id not found.""" response = import_asset_detail.get_by_import_asset_id(import_asset_id=330) assert not response assert response.status == 404 @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_get_by_import_asset_id_error(monkeypatch): """Test get_by_import_asset_id with exception.""" response = import_asset_detail.get_by_import_asset_id( import_asset_id=1) assert response.status == 500