from unittest.mock import patch from ows_accounting import response from ows_accounting.constants import error from ows_accounting.constants import header from ows_accounting.logic import transaction_types @patch('ows_accounting.logic.transaction_types.model') def test_get_transaction_types(mock_model): """Test the get_transaction_types method. """ trans_types = [ {'transactiontypedesc': 'Ad-Supported Audio Streams', 'transactiontypeid': '10', 'transactiontypeabbr': 'AS'}, {'transactiontypedesc': 'Ad-Supported Video Streams', 'transactiontypeid': '9', 'transactiontypeabbr': 'AV'}, {'transactiontypedesc': 'Cloud Match Units', 'transactiontypeid': '28', 'transactiontypeabbr': 'CL'} ] expected = [ {'short_name': 'AS', 'long_name': 'Ad-Supported Audio Streams'}, {'short_name': 'AV', 'long_name': 'Ad-Supported Video Streams'}, {'short_name': 'CL', 'long_name': 'Cloud Match Units'}] mock_model.get_transaction_types_with_sales.return_value = trans_types logic_response = transaction_types.get_transaction_types( 123, header.GRASS_ACCOUNT_TYPE_VENDOR, ['203', '204', '205']) assert logic_response.message == {'items': expected} @patch('ows_accounting.logic.transaction_types.validators') def test_get_transaction_types_invalid_account_type(mock_validation): """Test get_transaction_types with invalid account_type param. """ message = '{error} {account}'.format( error=error.ERROR_MESSAGE_INVALID_ACCOUNT, account='test') mock_validation.validate_account_type.return_value = \ response.create_error_response( message=message, code=error.ERROR_CODE_INVALID_REQUEST) result = transaction_types.get_transaction_types( 123, 'test', ['203', '204', '205']) assert result.status == 400 assert result.errors.get('message') == message @patch('ows_accounting.logic.transaction_types.model') def test_get_transaction_types_periods_out_of_order(mock_model): """Test get_transaction_types with periods out of order. """ trans_types = [ {'transactiontypedesc': 'Ad-Supported Audio Streams', 'transactiontypeid': '10', 'transactiontypeabbr': 'AS'}, {'transactiontypedesc': 'Ad-Supported Video Streams', 'transactiontypeid': '9', 'transactiontypeabbr': 'AV'}, {'transactiontypedesc': 'Cloud Match Units', 'transactiontypeid': '28', 'transactiontypeabbr': 'CL'} ] expected = [ {'short_name': 'AS', 'long_name': 'Ad-Supported Audio Streams'}, {'short_name': 'AV', 'long_name': 'Ad-Supported Video Streams'}, {'short_name': 'CL', 'long_name': 'Cloud Match Units'}] mock_model.get_transaction_types_with_sales.return_value = trans_types logic_response = transaction_types.get_transaction_types( 123, header.GRASS_ACCOUNT_TYPE_VENDOR, ['205', '203', '204']) assert logic_response.message == {'items': expected} mock_model.get_transaction_types_with_sales.assert_any_call( 123, header.GRASS_ACCOUNT_TYPE_VENDOR, '203', '205')