"""Tests for payments.""" from unittest.mock import call from unittest.mock import MagicMock import pytest from ows_accounting import response from ows_accounting.logic import accounting_intervals from ows_accounting.logic import payments from ows_accounting.models import currency from ows_accounting.models import currency_exchange_rates from ows_accounting.models import payment as payment_model from ows_accounting.models import period as period_model @pytest.fixture def fixture_payments(): """Fixture for payments info from art_relations.""" return [ [100, 207, 'Check', 500], [100, 207, 'Check', 0.5], [100, 208, 'Check', 33], [100, 209, 'Check', 34.5], [100, 210, 'Check', 32.5] ] @pytest.fixture def fixture_currency(): """Fixtures for currency model.""" return { 'USD': { 'id': 1, 'code': 'USD', 'symbol': '$', 'name': 'United States Dollar' }, 'EUR': { 'id': 4, 'code': 'EUR', 'symbol': '€', 'name': 'Europian Euro' } } @pytest.fixture def fixture_exchange_rate(): """Fixture for currency exchange rate.""" return { 'currency_from_id': 1, 'currency_to_id': 4, 'exchange_rate': 2 } @pytest.mark.parametrize('currency_code,expected_payments', [ ('USD', [0, 100, 500.5]), ('EUR', [0, 50, 250.25])]) def test_get_payments( monkeypatch, fixture_accounting_intervals, fixture_payments, fixture_currency, fixture_exchange_rate, currency_code, expected_payments): """Test getting payments.""" currency_info = fixture_currency[currency_code] expected_payments = [ { 'min_accounting_period': 222, 'max_accounting_period': 222, 'interval': { 'type': 'month', 'number': 9, 'year': 2016 }, 'currency_id': currency_info['id'], 'currency_symbol': currency_info['symbol'], 'payment': expected_payments[0], }, { 'min_accounting_period': 208, 'max_accounting_period': 210, 'interval': { 'type': 'quarter', 'number': 2, 'year': 2016 }, 'currency_id': currency_info['id'], 'currency_symbol': currency_info['symbol'], 'payment': expected_payments[1], }, { 'min_accounting_period': 207, 'max_accounting_period': 207, 'interval': { 'type': 'month', 'number': 3, 'year': 2016 }, 'currency_id': currency_info['id'], 'currency_symbol': currency_info['symbol'], 'payment': expected_payments[2], } ] monkeypatch.setattr( period_model, 'get_first_statement_period', MagicMock(return_value=response.Response(207))) monkeypatch.setattr( accounting_intervals, 'get_intervals_from_contracts', MagicMock(return_value=fixture_accounting_intervals)) monkeypatch.setattr( payment_model, 'get_vendor_payments', MagicMock(return_value=response.Response({'items': fixture_payments}))) monkeypatch.setattr( currency, 'get_currency_by_id', MagicMock(return_value=response.Response(currency_info))) monkeypatch.setattr( currency_exchange_rates, 'get_currency_exchange_rate', MagicMock(return_value=response.Response(fixture_exchange_rate))) result = payments.get_payments('vendor', 100, 4) accounting_intervals.get_intervals_from_contracts.assert_called_with( 'vendor', 100, 4) payment_model.get_vendor_payments.assert_called_with( 100, [222, 210, 209, 208, 207]) if currency_code == 'EUR': calls = [call(210, 4), call(209, 4), call(208, 4), call(207, 4)] currency_exchange_rates.get_currency_exchange_rate.assert_has_calls( calls) assert result.status assert result.message == expected_payments def test_get_payments_with_wrong_account_type(): """Test getting payments with wrong account type.""" result = payments.get_payments('subaccount', 100, 4) assert result.status == 400 def test_get_payments_with_wrong_intervals(monkeypatch): """Test getting payments with wrong accounting intervals.""" monkeypatch.setattr( accounting_intervals, 'get_intervals_from_contracts', MagicMock(return_value=response.create_error_response( 'code', 'message'))) result = payments.get_payments('vendor', 100, 4) accounting_intervals.get_intervals_from_contracts.assert_called_with( 'vendor', 100, 4) assert result.status == 400 def test_get_payments_with_wrong_currency( monkeypatch, fixture_accounting_intervals, fixture_payments): """Test getting payments with wrong currency id.""" monkeypatch.setattr( accounting_intervals, 'get_intervals_from_contracts', MagicMock(return_value=fixture_accounting_intervals)) monkeypatch.setattr( payments, '_get_first_statement_period', MagicMock(return_value=fixture_accounting_intervals.message)) monkeypatch.setattr( payment_model, 'get_vendor_payments', MagicMock(return_value=response.Response({'items': fixture_payments}))) monkeypatch.setattr( currency, 'get_currency_by_id', MagicMock(return_value=response.create_not_found_response())) result = payments.get_payments('vendor', 100, 4) accounting_intervals.get_intervals_from_contracts.assert_called_with( 'vendor', 100, 4) payment_model.get_vendor_payments.assert_called_with( 100, [222, 210, 209, 208, 207]) assert result.status == 404 def test_get_payments_with_wrong_currency_exchange_rate( monkeypatch, fixture_accounting_intervals, fixture_payments, fixture_currency): """Test getting payments with wrong currency exchange rate.""" monkeypatch.setattr( accounting_intervals, 'get_intervals_from_contracts', MagicMock(return_value=fixture_accounting_intervals)) monkeypatch.setattr( payments, '_get_first_statement_period', MagicMock(return_value=fixture_accounting_intervals.message)) monkeypatch.setattr( payment_model, 'get_vendor_payments', MagicMock(return_value=response.Response({'items': fixture_payments}))) monkeypatch.setattr( currency, 'get_currency_by_id', MagicMock(return_value=response.Response(fixture_currency['EUR']))) monkeypatch.setattr( currency_exchange_rates, 'get_currency_exchange_rate', MagicMock(return_value=response.create_not_found_response())) result = payments.get_payments('vendor', 100, 4) accounting_intervals.get_intervals_from_contracts.assert_called_with( 'vendor', 100, 4) payment_model.get_vendor_payments.assert_called_with( 100, [222, 210, 209, 208, 207]) assert result.status == 404 @pytest.mark.parametrize('account_type', ['vendor', 'subaccount']) def test_get_first_statement_period(monkeypatch, account_type): """Test filtering intervals.""" given_intervals = [ { 'type': 'month', 'number': 12, 'year': '2017', 'currency_id': 1, 'periods': [228] }, { 'type': 'month', 'number': 11, 'year': '2017', 'currency_id': 1, 'periods': [227] }, { 'type': 'month', 'number': 10, 'year': '2017', 'currency_id': 1, 'periods': [226] }, { 'type': 'month', 'number': 9, 'year': '2017', 'currency_id': 1, 'periods': [225] } ] expected_intervals = [ { 'type': 'month', 'number': 12, 'year': '2017', 'currency_id': 1, 'periods': [228] }, { 'type': 'month', 'number': 11, 'year': '2017', 'currency_id': 1, 'periods': [227] } ] monkeypatch.setattr( period_model, 'get_first_statement_period', MagicMock(return_value=response.Response(227))) result = payments._get_first_statement_period( 123, account_type, given_intervals) assert result == expected_intervals