"""Unit tests for Product logic.""" from owsrequest import request as requests from owsrequest import test_utils from promo_player.constants import service_name from promo_player.logic import product def test_get_by_product_id(monkeypatch): """Test retrieving a product by product_id from ows-product-digital.""" product_id = 1 json_response_mock = { 'product_id': 1 } path = '/product/audio/{product_id}'.format(product_id=product_id) call_specs = [{ 'service': service_name.OWS_PRODUCT_DIGITAL, 'path': path, 'status': 200, 'json': json_response_mock }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = product.get_by_product_id(product_id) assert result.message['product_id'] == 1 def test_get_by_product_id_error(monkeypatch): """Test that an error response is returned if the request fails.""" product_id = 1 path = '/product/audio/{product_id}'.format(product_id=product_id) call_specs = [{ 'service': service_name.OWS_PRODUCT_DIGITAL, 'path': path, 'status': 404 }] monkeypatch.setattr( requests, 'get', test_utils.mock_ows_requests(call_specs)) result = product.get_by_product_id(product_id) assert result.status == 404 def test_get_artist_name_single_primary(): """Test extracting the artist name from the product.""" product_details = { 'product_artists': [ { 'name': 'PRIMARY 1', 'role': 'primary_artist' } ] } result = product.get_artist_name(product_details) assert result == 'PRIMARY 1' def test_get_artist_name_multiple_primary(): """Test extracting the artist name from the product.""" product_details = { 'product_artists': [ { 'name': 'PRIMARY 1', 'role': 'primary_artist' }, { 'name': 'PRIMARY 2', 'role': 'primary_artist' } ] } result = product.get_artist_name(product_details) assert result == 'PRIMARY 1 & PRIMARY 2' def test_get_artist_name_primary_with_single_featuring(): """Test extracting the artist name from the product.""" product_details = { 'product_artists': [ { 'name': 'PRIMARY 1', 'role': 'primary_artist' }, { 'name': 'FEATURING 1', 'role': 'featuring' } ] } result = product.get_artist_name(product_details) assert result == 'PRIMARY 1 ft. FEATURING 1' def test_get_artist_name_primary_with_multiple_featuring(): """Test extracting the artist name from the product.""" product_details = { 'product_artists': [ { 'name': 'PRIMARY 1', 'role': 'primary_artist' }, { 'name': 'PRIMARY 2', 'role': 'primary_artist' }, { 'name': 'FEATURING 1', 'role': 'featuring' }, { 'name': 'FEATURING 2', 'role': 'featuring' } ] } result = product.get_artist_name(product_details) assert result == 'PRIMARY 1 & PRIMARY 2 ft. FEATURING 1 & FEATURING 2' def test_get_artist_name_not_found(): """Test extracting the artist name from the product.""" product_details = { 'product_artists': [] } result = product.get_artist_name(product_details) assert result == 'N/A'