"""Image Assets Model Tests. Testing the image assets model """ import pytest from images.constants import errors from images.models import image_assets from tests.factories.image_assets import ImageAssetsFactory from tests.factories.image_assets import ImportAssetDetailFactory from tests.factories.image_assets import ReleaseCoverImagesFactory from tests.utils import db @pytest.fixture def test_filename(): """Return test filename.""" return '123456789012.jpg' @pytest.fixture def test_import_asset_id(): """Return test asset id.""" return 123 @pytest.fixture def test_upc(): """Return test upc.""" return 123456789012 @pytest.fixture def test_url(): """Return test complete image url.""" return ('https://images.example.com/release/large_cover/123456/789/' '123456789012.jpg') @pytest.fixture def test_image_asset(test_filename): """Return test image asset detail.""" return {'path': '/123456/789/', 'filename': test_filename} def test_get_file_path_for_image(test_schema, test_import_asset_id, test_url): """Test that image assets are properly filtered by filename.""" image_asset = ImageAssetsFactory.build() import_asset_detail = ImportAssetDetailFactory.build() release_cover_images = ReleaseCoverImagesFactory.build() db.seed_models([image_asset, import_asset_detail, release_cover_images]) result = image_assets.get_file_path_for_image(test_import_asset_id) assert result.message.get('path') == test_url def test_get_file_path_for_image_upc_error(test_schema, test_import_asset_id): """Test getting a 404 from image_assets for filename when upc not found.""" result = image_assets.get_file_path_for_image(test_import_asset_id) assert result.status == 404 assert result.message is None assert result.errors == { 'message': errors.IMPORT_UPC_NOT_FOUND, 'code': 'not_found_error'} def test_get_file_path_for_image_path_error( test_schema, test_import_asset_id, test_upc): """Test getting a 404 from image_assets for filename when upc not found.""" import_asset_detail = ImportAssetDetailFactory.build() db.seed_models(import_asset_detail) result = image_assets.get_file_path_for_image(test_import_asset_id) assert result.status == 404 assert result.message is None assert result.errors == { 'message': errors.IMAGE_PATH_NOT_FOUND, 'code': 'not_found_error'} def test_get_image_path_by_upc(test_schema, test_url, test_upc): """Test getting the image path from the URL.""" image_asset = ImageAssetsFactory.build() release_cover_images = ReleaseCoverImagesFactory.build() db.seed_models([image_asset, release_cover_images]) result = image_assets.get_image_path_by_upc(test_upc) assert result.message.get('path') == test_url def test_get_image_path_by_upc_path_error(test_schema, test_upc): """Test getting a 404 from image_assets for filename when upc not found.""" import_asset_detail = ImportAssetDetailFactory.build() release_cover_images = ReleaseCoverImagesFactory.build() db.seed_models([import_asset_detail, release_cover_images]) result = image_assets.get_image_path_by_upc(test_upc) assert result.status == 404 assert result.message is None assert result.errors == { 'message': errors.IMAGE_PATH_NOT_FOUND, 'code': 'not_found_error'}