"""Test for asset_received_destination_detail model.""" from unittest.mock import patch import pytest from sqlalchemy.exc import SQLAlchemyError from assets.models.legacy import \ asset_received_destination_details as destination_details from tests.models.legacy import dd_operations @pytest.fixture def data(): """Get data to seed table.""" return [ { 'asset_received_queue_id': 1, 'destination_upc': 123, 'destination_track_id': 456, 'destination_asset_name': 'fred' } ] @pytest.fixture def db_fixture(data): """Set up the asset received queue table.""" dd_operations.create_tables() dd_operations.seed_asset_received_destination_details_table(data) def test_get_destination_detail_by_id(db_fixture, data): """Test the model definition is interpretable by sqlalchemy.""" result = destination_details.get_destination_detail_by_id(1) assert result message = result.message assert message.get('name') == data[0].get( 'destination_asset_name') def test_get_destination_detail_by_id_not_found(db_fixture): """Test the model definition is interpretable by sqlalchemy.""" result = destination_details.get_destination_detail_by_id(100) assert not result assert result.status == 404 def test_create_destination_detail(db_fixture): """Test asset received destination detail record creation.""" expected_asset_received_queue_id = 50 expected_upc = 111222333 expected_track_id = 12 expected_name = 'name' response = destination_details.create_destination_detail( asset_received_queue_id=expected_asset_received_queue_id, upc=expected_upc, track_id=expected_track_id, name=expected_name) assert response.status assert response.message['id'] == 2 assert response.message['asset_received_queue_id'] ==\ expected_asset_received_queue_id assert response.message['upc'] == expected_upc assert response.message['track_id'] == expected_track_id assert response.message['name'] == expected_name @patch('assets.connectors.mysql.dd_db_session', side_effect=SQLAlchemyError()) def test_create_asset_received_queue_error(monkeypatch): """Test asset received destination detail record with exception.""" response = destination_details.create_destination_detail( asset_received_queue_id=50, upc=111222333, track_id=12, name='name') assert response.status == 500