"""Test suite for vendor_currency logic tier.""" from unittest.mock import MagicMock from ows_accounting import response from ows_accounting.constants import error from ows_accounting.logic import vendor_currency from ows_accounting.models import currency from ows_accounting.models import ows_contracts from ows_accounting.utils.request_flask import GrassAccount def test_get_currency_by_vendor_id(monkeypatch): """Test get_currency_by_vendor_id.""" expected_result = { 'vendor_id': 1234, 'currency_id': 1, 'code': 'USD', 'symbol': '$'} mock_ows_contract = {'items': [ {'id': 7157428, 'period_id': 251, 'vendor_id': 1234, 'currency_id': 1}, {'id': 7157421, 'period_id': 250, 'vendor_id': 1234, 'currency_id': 1} ]} mock_currency = { 'id': 1, 'code': 'USD', 'symbol': '$'} monkeypatch.setattr( ows_contracts, 'get_booked_contracts', MagicMock(return_value=response.Response(mock_ows_contract))) monkeypatch.setattr( currency, 'get_currency_by_id', MagicMock(return_value=response.Response(mock_currency))) vendor_currency_result = vendor_currency.get_currency_by_vendor_id( GrassAccount('vendor', 1234)) ows_contracts.get_booked_contracts.assert_called_once_with( account_type='vendor', account_id=1234, sort_order='desc') currency.get_currency_by_id.assert_called_once_with(1) assert vendor_currency_result.message == expected_result def test_get_currency_by_vendor_id_contracts_not_found(monkeypatch): """Test get_currency_by_vendor_id.""" monkeypatch.setattr( ows_contracts, 'get_booked_contracts', MagicMock( return_value=response.create_not_found_response( error.ERROR_MESSAGE_CONTRACTS_NOT_FOUND))) vendor_currency_result = vendor_currency.get_currency_by_vendor_id( GrassAccount('vendor', 1234)) ows_contracts.get_booked_contracts.assert_called_once_with( account_type='vendor', account_id=1234, sort_order='desc') assert vendor_currency_result.status == 404 def test_get_currency_by_vendor_id_currency_not_found(monkeypatch): """Test get_currency_by_vendor_id.""" mock_ows_contract = {'items': [ {'id': 7157428, 'period_id': 251, 'vendor_id': 1234, 'currency_id': 1}, {'id': 7157421, 'period_id': 250, 'vendor_id': 1234, 'currency_id': 1} ]} monkeypatch.setattr( ows_contracts, 'get_booked_contracts', MagicMock(return_value=response.Response(mock_ows_contract))) monkeypatch.setattr( currency, 'get_currency_by_id', MagicMock( return_value=response.create_not_found_response( error.ERROR_MESSAGE_CURRENCY_NOT_FOUND))) vendor_currency_result = vendor_currency.get_currency_by_vendor_id( GrassAccount('vendor', 1234)) ows_contracts.get_booked_contracts.assert_called_once_with( account_type='vendor', account_id=1234, sort_order='desc') currency.get_currency_by_id.assert_called_once_with(1) assert vendor_currency_result.status == 404