"""Test cases for assets validators.""" from unittest.mock import MagicMock from oto import response from assets.constants import error from assets.constants import success from assets.logic import validators from assets.models import ows_product from assets.models.legacy import asset def test_check_for_same_bit_depth( valid_bit_per_sample_data_for_upc, monkeypatch): """Test _is_bit_depth_uniform with valid UPC having same bit depth.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( valid_bit_per_sample_data_for_upc))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert result assert result.status == 200 def test_check_for_mixed_bit_depth( mixed_bit_per_sample_data_for_upc, monkeypatch): """Test _is_bit_depth_uniform mixed bit depth.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( mixed_bit_per_sample_data_for_upc))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert not result assert result.status == 400 def test_check_bit_depth_valid_error_correction( valid_bit_per_sample_data_upc_error_correction, monkeypatch): """Test _is_bit_depth_uniform having same bit depth error correction.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( valid_bit_per_sample_data_upc_error_correction))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert result assert result.status == 200 def test_check_bit_depth_invalid_error_correction( invalid_bit_per_sample_upc_error_correction, monkeypatch): """Test _is_bit_depth_uniform mixed bit depth some tracks reuploaded.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( invalid_bit_per_sample_upc_error_correction))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert result.status == 400 def test_check_bit_depth_valid_error_correction_reupload_all( valid_bit_per_sample_error_correction_reupload_all, monkeypatch): """Test _is_bit_depth_uniform some tracks reuploaded.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( valid_bit_per_sample_error_correction_reupload_all))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert result.status == 200 def test_check_bit_depth_invalid_error_correction_reupload_all( invalid_bit_per_sample_error_correction_reupload_all, monkeypatch): """Test _is_bit_depth_uniform mixed bit depth all tracks reuploaded.""" upc = 191018729677 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock(return_value=response.Response( invalid_bit_per_sample_error_correction_reupload_all))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert result.status == 400 def test_check_for_bit_depth_with_error(monkeypatch): """Test _is_bit_depth_uniform with an error.""" upc = 55555 monkeypatch.setattr( asset, 'get_bits_per_sample_data_for_tracks', MagicMock( return_value=response.create_not_found_response( error.NO_BITS_PER_SAMPLE_FOUND))) result = validators._is_bit_depth_uniform(upc, 1, 1) assert not result assert result.status == 404 def test_validate_bit_depth_by_invalid_upc(monkeypatch): """Test validate_bit_depth_by_upc by providing invalid UPC.""" upc = 123 physical_location_id = 1 asset_type_id = 2 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.create_not_found_response( message=str(error.ERROR_RELEASE_NOT_FOUND)))) result = validators.validate_bit_depth_by_upc( upc, physical_location_id, asset_type_id) assert result.status == 404 def test_validate_for_same_bit_depth(monkeypatch): """Test validate_bit_depth_by_upc with valid UPC having same bit depth.""" upc = 191018729677 physical_location_id = 1 asset_type_id = 1 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.Response( message={'upc': 191018729677}))) monkeypatch.setattr( validators, '_is_bit_depth_uniform', MagicMock( return_value=response.Response( message=success.SAME_BITS_PER_SAMPLE_FOUND))) result = validators.validate_bit_depth_by_upc( upc, physical_location_id, asset_type_id) assert result assert result.status == 200 def test_validate_mixed_bit_depth_by_upc(monkeypatch): """Test validate_bit_depth_by_upc with valid UPC having mixed bit depth.""" upc = 191018729677 physical_location_id = 1 asset_type_id = 1 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.Response( message={'upc': 191018729677}))) monkeypatch.setattr( validators, '_is_bit_depth_uniform', MagicMock( return_value=response.Response( message=error.MIXED_BITS_PER_SAMPLE_FOUND, status=400))) result = validators.validate_bit_depth_by_upc( upc, physical_location_id, asset_type_id) assert result.status == 400 def test_is_desired_bit_depth_found(monkeypatch): """Test with valid UPC having desired bit depth.""" upc = 191018729677 desired_bit_depth = 64 mock_respone = [64] monkeypatch.setattr( asset, 'get_bitrates_for_upc', MagicMock(return_value=response.Response(mock_respone))) result = validators._is_desired_bit_depth_found( upc, desired_bit_depth, 1, 1) assert result assert result.status == 200 assert result.message == success.DESIRED_BIT_DEPTH_FOUND def test_no_desired_bit_depth_found(monkeypatch): """Test with valid UPC not having desired bit depth.""" upc = 191018729677 desired_bit_depth = 64 mock_respone = [16] monkeypatch.setattr( asset, 'get_bitrates_for_upc', MagicMock(return_value=response.Response(mock_respone))) result = validators._is_desired_bit_depth_found( upc, desired_bit_depth, 1, 1) assert not result assert result.status == 400 assert result.message == error.DESIRED_BIT_DEPTH_NOT_FOUND def test_is_bit_depth_uniform_with_error(monkeypatch): """Test _is_bit_depth_uniform with an error.""" upc = 191018729677 desired_bit_depth = [64] monkeypatch.setattr( asset, 'get_bitrates_for_upc', MagicMock( return_value=response.create_not_found_response( error.NO_BITS_PER_SAMPLE_FOUND))) result = validators._is_desired_bit_depth_found( upc, desired_bit_depth, 1, 1) assert not result assert result.status == 404 assert result.message == error.NO_BITS_PER_SAMPLE_FOUND def test_check_for_desired_bit_depth_with_invalid_upc(monkeypatch): """Test check_for_desired_bit_depth by providing invalid UPC.""" upc = 123 desired_bit_depth = 64 physical_location_id = 1 asset_type_id = 2 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.create_not_found_response( message=str(error.ERROR_RELEASE_NOT_FOUND)))) result = validators.check_for_desired_bit_depth( upc, desired_bit_depth, physical_location_id, asset_type_id) assert result.status == 404 assert result.message == 'Product not found for provided product id.' def test_check_for_desired_bit_depth(monkeypatch): """Test check_for_desired_bit_depth having desired bit depth.""" upc = 191018729677 desired_bit_depth = 64 physical_location_id = 1 asset_type_id = 1 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.Response( message={'upc': 191018729677}))) monkeypatch.setattr( validators, '_is_desired_bit_depth_found', MagicMock( return_value=response.Response( message=success.DESIRED_BIT_DEPTH_FOUND, status=200))) result = validators.check_for_desired_bit_depth( upc, desired_bit_depth, physical_location_id, asset_type_id) assert result assert result.status == 200 assert result.message == success.DESIRED_BIT_DEPTH_FOUND def test_check_for_desired_bit_depth_not_found(monkeypatch): """Test check_for_desired_bit_depth with no desired bit depth.""" upc = 191018729677 desired_bit_depth = 64 physical_location_id = 2 asset_type_id = 2 monkeypatch.setattr( ows_product, 'get_product_by_id', MagicMock(return_value=response.Response( message={'upc': 191018729677}))) monkeypatch.setattr( validators, '_is_desired_bit_depth_found', MagicMock( return_value=response.Response( message=error.DESIRED_BIT_DEPTH_NOT_FOUND, status=400))) result = validators.check_for_desired_bit_depth( upc, desired_bit_depth, physical_location_id, asset_type_id) assert result.status == 400 assert result.message == error.DESIRED_BIT_DEPTH_NOT_FOUND