from unittest.mock import patch from ows_accounting.logic import period from ows_accounting.response import Response @patch('ows_accounting.logic.period.period_model') def test_get_available_periods(mock_period_model, mock_periods): """Test if periods are retrieved from model. """ mock_period_model.get_available_periods.return_value = Response( mock_periods) result = period.get_available_periods(18805, 'vendor') mock_period_model.get_available_periods.assert_any_call('18805L') assert result.status == 200 assert result.message == mock_periods @patch('ows_accounting.logic.period.period_model') @patch('ows_accounting.logic.period.fact_sales') def test_get_first_period_with_first_statement_period( mock_fact_sales, mock_period_model): """Test getting first period for account with sales and first statement period """ mock_fact_sales.get_first_sale_accounting_period.return_value = Response( 206) mock_period_model.get_first_statement_period.return_value = Response(204) result = period.get_first_period(18805, 'vendor', 247) mock_fact_sales.get_first_sale_accounting_period.assert_called_once_with( 'vendor', 18805 ) mock_period_model.get_first_statement_period.assert_called_once_with( 18805, 'vendor' ) assert result == 204 @patch('ows_accounting.logic.period.period_model') @patch('ows_accounting.logic.period.fact_sales') def test_get_first_period_without_first_statement_period( mock_fact_sales, mock_period_model): """Test getting first period for account with sales and no first statement period """ mock_fact_sales.get_first_sale_accounting_period.return_value = Response( 206) mock_period_model.get_first_statement_period.return_value = Response(1) result = period.get_first_period(18805, 'vendor', 247) mock_fact_sales.get_first_sale_accounting_period.assert_called_once_with( 'vendor', 18805 ) mock_period_model.get_first_statement_period.assert_called_once_with( 18805, 'vendor' ) assert result == 206 @patch('ows_accounting.logic.period.fact_sales') def test_get_first_period_without_sales(mock_fact_sales): """Test getting first period for account with no sales """ mock_fact_sales.get_first_sale_accounting_period.return_value = Response( None) result = period.get_first_period(18805, 'vendor', 247) mock_fact_sales.get_first_sale_accounting_period.assert_called_once_with( 'vendor', 18805 ) assert result == 247 @patch('ows_accounting.logic.period.config') @patch('ows_accounting.logic.period.ows_contracts') @patch('ows_accounting.logic.period.get_first_period') @patch('ows_accounting.logic.period.accounting_period') def test_get_all_periods_quarter_interval( mock_accounting_period, mock_get_first_period, mock_ows_contracts, mock_config, fixture_quarterly_accounting_periods_information): """Test getting accounting period information for account with quarterly statements """ mock_ows_contracts.get_booked_contracts.return_value = Response({ 'items': [{ 'payment_interval': 'quarter' }] }) mock_config.ACTIVE_ACCOUNTING_PERIOD = 247 mock_get_first_period.return_value = 204 mock_accounting_period.get_quarterly_period_information.return_value = \ fixture_quarterly_accounting_periods_information result = period.get_all_periods(18805, 'vendor') mock_ows_contracts.get_booked_contracts.assert_called_once_with( 'vendor', 18805, page_offset=0, page_limit=1, sort_order='desc') mock_get_first_period.assert_called_once_with( 18805, 'vendor', 247) mock_accounting_period.get_quarterly_period_information \ .assert_called_once_with(204, 247) assert result == fixture_quarterly_accounting_periods_information @patch('ows_accounting.logic.period.config') @patch('ows_accounting.logic.period.ows_contracts') @patch('ows_accounting.logic.period.get_first_period') @patch('ows_accounting.logic.period.accounting_period') def test_get_all_periods_month_interval( mock_accounting_period, mock_get_first_period, mock_ows_contracts, mock_config, fixture_monthly_accounting_periods_information): """Test getting accounting period information for account with monthly statements """ mock_ows_contracts.get_booked_contracts.return_value = Response({ 'items': [{ 'payment_interval': 'month' }] }) mock_config.ACTIVE_ACCOUNTING_PERIOD = 247 mock_get_first_period.return_value = 204 mock_accounting_period.get_monthly_period_information.return_value = \ fixture_monthly_accounting_periods_information result = period.get_all_periods(18805, 'vendor') mock_ows_contracts.get_booked_contracts.assert_called_once_with( 'vendor', 18805, page_offset=0, page_limit=1, sort_order='desc') mock_get_first_period.assert_called_once_with( 18805, 'vendor', 247) mock_accounting_period.get_monthly_period_information \ .assert_called_once_with(204, 247) assert result == fixture_monthly_accounting_periods_information @patch('ows_accounting.logic.period.config') @patch('ows_accounting.logic.period.ows_contracts') @patch('ows_accounting.logic.period.get_first_period') @patch('ows_accounting.logic.period.accounting_period') def test_get_all_periods_no_booked_contracts( mock_accounting_period, mock_get_first_period, mock_ows_contracts, mock_config): """Test getting accounting period information for account with monthly statements """ mock_ows_contracts.get_booked_contracts.return_value = Response( {'items': []}) mock_config.ACTIVE_ACCOUNTING_PERIOD = 247 result = period.get_all_periods(18805, 'vendor') assert result.message['items'] == []