"""Tests for import asset model.""" from datetime import datetime from unittest.mock import patch from flexmock import flexmock import pytest from sqlalchemy.exc import SQLAlchemyError from assets.connectors import mysql from assets.constants import asset_types from assets.constants import error from assets.models.legacy import import_asset from tests.models.legacy import ar_operations @pytest.fixture def import_asset_data(): """Get data to seed import_asset table.""" return [ { 'id': 1, 'import_asset_batch_id': 1, 'foldername': 'import_assets_folder', 'asset_type': asset_types.TYPE_IMPORT_AUDIO, 'status': 'finished', 'upload_completed': datetime.strptime( '2017-01-10T11:41:12.997656', '%Y-%m-%dT%H:%M:%S.%f'), 'encoding_completed': datetime.strptime( '2017-02-15T17:40:12.997656', '%Y-%m-%dT%H:%M:%S.%f'), 'result': 'went ok, i guess', 'filename': 'unique_filename_1', }, { 'id': 2, 'import_asset_batch_id': 3, 'foldername': 'import_assets_folder', 'asset_type': asset_types.TYPE_IMPORT_IMAGE, 'status': 'finished', 'upload_completed': datetime.strptime( '2017-01-28T16:41:12.997656', '%Y-%m-%dT%H:%M:%S.%f'), 'encoding_completed': datetime.strptime( '2017-02-28T15:40:12.997656', '%Y-%m-%dT%H:%M:%S.%f'), 'result': None, 'filename': 'unique_filename_2', } ] @pytest.fixture def import_asset_detail_data(): """Get data to seed import_asset_detail table.""" return [ { 'id': 1, 'import_asset_id': 1, 'upc': 111222333, 'track_id': 1 }, { 'id': 2, 'import_asset_id': 2, 'upc': 111222333, 'track_id': 2 } ] @pytest.fixture def db_fixture(): """Set up the import asset table.""" ar_operations.create_tables() @pytest.fixture def db_with_data_fixture(import_asset_data, import_asset_detail_data): """Set up the import asset table with test entries.""" ar_operations.create_tables() ar_operations.seed_import_asset_table(import_asset_data) ar_operations.seed_import_asset_detail_table(import_asset_detail_data) @pytest.mark.parametrize('initial_type, expected_type', [ (asset_types.TYPE_IMPORT_AUDIO, asset_types.TYPE_IMPORT_AUDIO), (asset_types.TYPE_IMPORT_IMAGE, asset_types.TYPE_IMPORT_IMAGE), (asset_types.TYPE_FILE_TIF, asset_types.TYPE_IMPORT_IMAGE), (asset_types.TYPE_FILE_JPG, asset_types.TYPE_IMPORT_IMAGE), (asset_types.TYPE_FILE_WAV, asset_types.TYPE_IMPORT_AUDIO), (asset_types.TYPE_FILE_FLAC, asset_types.TYPE_IMPORT_AUDIO), (asset_types.TYPE_FILE_M4A, asset_types.TYPE_IMPORT_AUDIO)]) def test_create_import_asset( db_fixture, monkeypatch, initial_type, expected_type): """Test import asset record creation.""" expected_batch_id = 1 expected_foldername = 'folder' expected_filename = 'filename.ext' response = import_asset.create_import_asset( import_asset_batch_id=expected_batch_id, foldername=expected_foldername, filename=expected_filename, asset_type=initial_type) assert response.status assert response.message['id'] == 1 assert response.message['import_asset_batch_id'] == expected_batch_id assert response.message['foldername'] == expected_foldername assert response.message['filename'] == expected_filename assert response.message['asset_type'] == expected_type def test_create_import_asset_with_wrong_type(): """Test import asset record creation with wrong asset type.""" response = import_asset.create_import_asset( import_asset_batch_id=10, foldername='folder', filename='filename', asset_type='something') assert response.status == 400 @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_create_import_asset_error(monkeypatch): """Test import asset record creation with exception.""" response = import_asset.create_import_asset( import_asset_batch_id=10, foldername='folder', filename='filename', asset_type=asset_types.TYPE_IMPORT_IMAGE) assert response.status == 500 def test_get_import_asset_by_filename_success( db_with_data_fixture, import_asset_data): """Test successful get of existing import asset.""" existing_filename = 'unique_filename_1' response = import_asset.get_import_asset_by_filename(existing_filename) assert response assert response.message['filename'] == existing_filename assert response.message['result'] == import_asset_data[0]['result'] def test_get_import_asset_by_filename_failures(db_with_data_fixture): """Test get_import_asset_by_filename fail if filename not found.""" not_existing_filename = 'not_existing_filename_1' response = import_asset.get_import_asset_by_filename(not_existing_filename) assert response.status == 404 assert response.errors['code'] == error.ERROR_CODE_NOT_FOUND assert response.errors['message'] == error.ERROR_MESSAGE_ASSET_NOT_FOUND def test_get_asset_upload_sql_error(db_with_data_fixture): """Test SQLAlchemyError failure.""" (flexmock(mysql) .should_receive('ar_db_session') .and_raise(SQLAlchemyError(500))) existing_filename = 'unique_filename_1' response = import_asset.get_import_asset_by_filename(existing_filename) assert response.status == 500 def test_get_assets_by_upc( db_with_data_fixture, import_asset_data, import_asset_detail_data): """Test getting import assets by product upc.""" response = import_asset.get_assets_by_upc(111222333) data = response.message assertion_keys = [ 'id', 'asset_type', 'filename', 'foldername', 'status', 'import_asset_batch_id'] assert response.status assert len(data) == 2 for key in assertion_keys: assert data[0]['import_asset'][key] == import_asset_data[1][key] assert data[1]['import_asset'][key] == import_asset_data[0][key] assert data[0]['import_asset_detail'] == import_asset_detail_data[1] assert data[1]['import_asset_detail'] == import_asset_detail_data[0] def test_get_assets_by_upc_empty(db_with_data_fixture): """Test getting import assets by product upc with empty response.""" response = import_asset.get_assets_by_upc(999888) data = response.message assert response assert data == [] @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_get_assets_by_upc_error(monkeypatch): """Test getting import assets by product upc with exception.""" response = import_asset.get_assets_by_upc(111222333) assert response.status == 500 def test_get_assets_by_ids( db_with_data_fixture, import_asset_data, import_asset_detail_data): """Test getting import assets by ids.""" response = import_asset.get_assets_by_ids([2]) data = response.message assertion_keys = [ 'id', 'asset_type', 'filename', 'foldername', 'status', 'import_asset_batch_id'] assert response.status assert len(data) == 1 for key in assertion_keys: assert data[0]['import_asset'][key] == import_asset_data[1][key] assert data[0]['import_asset_detail'] == import_asset_detail_data[1] def test_get_assets_by_ids_empty(db_with_data_fixture): """Test getting import assets by ids with empty response.""" response = import_asset.get_assets_by_ids([]) data = response.message assert response assert data == [] @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_get_assets_by_ids_error(monkeypatch): """Test getting import assets by ids with exception.""" response = import_asset.get_assets_by_ids([1, 2]) assert response.status == 500