"""Test salessheets module that takes data from art_relation database.""" from flexmock import flexmock from sqlalchemy.exc import SQLAlchemyError from salessheets.connectors import mysql from salessheets.constants import errors from salessheets.constants import models from salessheets.models import salessheets def test_get_album_valid(): """Test getting album from art_relation.""" product_id = '1' test_result = {models.PRODUCT_ID: product_id} first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_album(product_id) assert result.message == test_result def test_get_album_without_product_id(): """Test getting album without product_id.""" product_id = None result = salessheets.get_album(product_id) assert result.message == {} def test_get_album_exception(): """Test getting album for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_album(product_id) assert result.status == 500 def test_get_album_no_data(): """Test getting album without necessary data in db.""" product_id = '1' first_proxy = flexmock(first=lambda: {}) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_album(product_id) assert result.status == 404 def test_get_tracks_valid(): """Test getting tracks.""" product_id = '1' test_execute_result = [{'track_id': '1'}] all_proxy = flexmock(all=lambda: test_execute_result) result_proxy = flexmock(mappings=lambda: all_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_tracks(product_id) assert result.message == { dict(row)[models.TRACK_ID]: dict(row) for row in test_execute_result } def test_get_tracks_without_product_id(): """Test getting tracks without product_id.""" product_id = None result = salessheets.get_tracks(product_id) assert result.message == {} def test_get_tracks_no_data(): """Test getting tracks without necessary data in db.""" product_id = '1' test_execute_result = [] all_proxy = flexmock(all=lambda: test_execute_result) result_proxy = flexmock(mappings=lambda: all_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_tracks(product_id) assert result.status == 404 def test_get_tracks_exception(): """Test getting tracks for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_tracks(product_id) assert result.status == 500 def test_get_artists_valid(): """Test getting artists.""" product_id = '1' test_res = [{models.ARTIST_NAME: product_id}] all_proxy = flexmock(all=lambda: test_res) result_proxy = flexmock(mappings=lambda: all_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_artists(product_id) assert result.message == [res[models.ARTIST_NAME] for res in test_res] def test_get_artists_without_product_id(): """Test getting artists without product_id.""" product_id = None result = salessheets.get_artists(product_id) assert result.message == [] def test_get_artists_no_data(): """Test getting artists without necessary data in db.""" product_id = '1' test_res = [] all_proxy = flexmock(all=lambda: test_res) result_proxy = flexmock(mappings=lambda: all_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_artists(product_id) assert result.status == 404 def test_get_artists_exception(): """Test getting artists for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_artists(product_id) assert result.status == 500 def test_get_product_type_id_valid(): """Test getting product_type_id from releases for a product id.""" product_id = '1' test_result = {'product_type_id': '1'} first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_product_type_id(product_id) assert result.message == test_result def test_get_product_type_id_without_product_id(): """Test getting product_type_id without product_id.""" product_id = None result = salessheets.get_product_type_id(product_id) assert result.message == {} def test_get_product_type_id_no_data(): """Test getting product_type_id without necessary data.""" product_id = '1' test_result = {} first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_product_type_id(product_id) assert result.status == 404 def test_get_product_type_id_exception(): """Test getting product_type_id for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_product_type_id(product_id) assert result.status == 500 def test_get_distribution_format_id_valid(): """Test getting distribution_format for a product id succeeds.""" product_id = 1 expected_result = {'display_distribution_format': '7" Album'} display_configuration_result = {'display_configuration': '7" Album'} first_proxy = flexmock(first=lambda: display_configuration_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) flexmock(mysql).should_receive('session_scope').and_return(session_scope) result = salessheets.get_distribution_format(product_id) assert result.message == expected_result def test_get_distribution_format_id_without_product_id(): """Test getting distribution_format without product_id.""" product_id = None result = salessheets.get_distribution_format(product_id) assert result.message == {} def test_get_distribution_format_id_no_data(): """Test getting distribution_format without necessary data.""" product_id = '1' test_result = [] first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_distribution_format(product_id) assert result.status == 404 def test_get_distribution_format_id_exception(): """Test getting distribution_format for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_distribution_format(product_id) assert result.status == 500 def test_get_context_type_and_status_valid(): """Test get context type and status of release from art_relations.""" product_id = '1' test_result = {'context_type': 'digital', 'status': 'in_content'} first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_context_type_and_status(product_id) assert result.message == test_result def test_get_context_type_and_status_without_product_id(): """Test get context type and status without product_id.""" product_id = None result = salessheets.get_context_type_and_status(product_id) assert result.message == {} def test_get_context_type_and_status_exception(): """Test get context type and status for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_context_type_and_status(product_id) assert result.status == 500 def test_get_context_type_and_status_no_data(): """Test get context type and status without necessary data in db.""" product_id = '1' first_proxy = flexmock(first=lambda: {}) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_context_type_and_status(product_id) assert result.status == 404 def test_get_album_for_digital_valid(): """Test getting album for digital from art_relation.""" product_id = '1' test_result = { 'product_id': '12345', 'product_name': 'Product Name', 'vendor_name': 'Vendor Name' } first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_album_for_digital(product_id) assert result.message == test_result def test_get_album_for_digital_without_product_id(): """Test getting album for digital without product_id.""" product_id = None result = salessheets.get_album_for_digital(product_id) assert result.message == {} def test_get_album_for_digital_exception(): """Test getting album for digital for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_album_for_digital(product_id) assert result.status == 500 def test_get_album_for_digital_no_data(): """Test get album for digital if no necessary data in db.""" product_id = '1' first_proxy = flexmock(first=lambda: {}) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_album_for_digital(product_id) assert result.status == 404 def test_get_genre_and_subgenre_valid(): """Test getting genre and subgenre from art_relations.""" product_id = '1' test_result = { 'genre': 'test genre', 'subgnre': 'test subgenre' } first_proxy = flexmock(first=lambda: test_result) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_genre_and_subgenre(product_id) assert result.message == test_result def test_get_genre_and_subgenre_without_product_id(): """Test get genre and subgenre without product_id.""" product_id = None result = salessheets.get_genre_and_subgenre(product_id) assert result.errors['message'] == errors.ERROR_MESSAGE_MISSING_PRODUCT_ID def test_get_genre_and_subgenre_exception(): """Test get genre and subgenre for valid exception.""" product_id = '1' (flexmock(mysql) .should_receive('session_scope') .and_raise(SQLAlchemyError)) result = salessheets.get_genre_and_subgenre(product_id) assert result.status == 500 def test_get_genre_and_subgenre_no_data(): """Test get genre and subgenre without necessary data in db.""" product_id = '1' first_proxy = flexmock(first=lambda: {}) result_proxy = flexmock(mappings=lambda: first_proxy) session_scope = flexmock(execute=lambda query, data: result_proxy) (flexmock(mysql) .should_receive('session_scope') .and_return(session_scope)) result = salessheets.get_genre_and_subgenre(product_id) assert result.status == 404