"""Tests for accounting intervals.""" from flexmock import flexmock import pytest from ows_accounting import response from ows_accounting.logic import accounting_intervals from ows_accounting.models import accounting_period from ows_accounting.models import ows_contracts @pytest.fixture def fixture_booked_contracts(): """Fixture for vendor booked contracts.""" booked_contracts = [ [{ 'id': 11, 'contract_id': 105, 'period_id': 216, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 1 }, { 'id': 10, 'contract_id': 105, 'period_id': 215, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 1 }, { 'id': 9, 'contract_id': 105, 'period_id': 214, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 1 }, { 'id': 8, 'contract_id': 104, 'period_id': 213, 'vendor_id': 100, 'payment_interval': 'month', 'currency_id': 1 }, { 'id': 7, 'contract_id': 103, 'period_id': 210, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 2 }], [{ 'id': 6, 'contract_id': 103, 'period_id': 209, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 2 }, { 'id': 5, 'contract_id': 103, 'period_id': 208, 'vendor_id': 100, 'payment_interval': 'quarter', 'currency_id': 2 }, { 'id': 4, 'contract_id': 102, 'period_id': 207, 'vendor_id': 100, 'payment_interval': 'month', 'currency_id': 10 }, { 'id': 3, 'contract_id': 101, 'period_id': 206, 'vendor_id': 100, 'payment_interval': 'month', 'currency_id': 1 }] ] return booked_contracts @pytest.fixture def fixture_booked_contracts_pagination(): """Fixture for vendor booked contracts pagination info.""" return [{ 'type': 'standard', 'offset': 0, 'limit': 5, 'total_records': 9, }, { 'type': 'standard', 'offset': 5, 'limit': 5, 'total_records': 9, }] @pytest.fixture def fixture_accounting_periods(): """Fixture for accounting periods cache.""" return [ { 'period_type': 'month', 'first_period_id': 206, 'last_period_id': 206, 'year': 2016, 'quarter': 1, 'month': 2 }, { 'period_type': 'month', 'first_period_id': 207, 'last_period_id': 207, 'year': 2016, 'quarter': 1, 'month': 3 }, { 'period_type': 'month', 'first_period_id': 208, 'last_period_id': 208, 'year': 2016, 'quarter': 2, 'month': 4 }, { 'period_type': 'month', 'first_period_id': 209, 'last_period_id': 209, 'year': 2016, 'quarter': 2, 'month': 5 }, { 'period_type': 'month', 'first_period_id': 210, 'last_period_id': 210, 'year': 2016, 'quarter': 2, 'month': 6 }, { 'period_type': 'month', 'first_period_id': 213, 'last_period_id': 213, 'year': 2016, 'quarter': 3, 'month': 9 }, { 'period_type': 'month', 'first_period_id': 214, 'last_period_id': 214, 'year': 2016, 'quarter': 4, 'month': 10 }, { 'period_type': 'month', 'first_period_id': 215, 'last_period_id': 215, 'year': 2016, 'quarter': 4, 'month': 11 } ] @pytest.fixture def fixture_accounting_intervals(): """Fixture for expected accounting intervals.""" return [ { 'type': 'quarter', 'number': 4, 'year': 2016, 'currency_id': 1, 'periods': [215, 214] }, { 'type': 'month', 'number': 9, 'year': 2016, 'currency_id': 1, 'periods': [213] }, { 'type': 'quarter', 'number': 2, 'year': 2016, 'currency_id': 2, 'periods': [210, 209, 208] }, { 'type': 'month', 'number': 3, 'year': 2016, 'currency_id': 10, 'periods': [207] } ] def test_get_intervals_from_contracts( fixture_accounting_intervals, fixture_booked_contracts, fixture_accounting_periods, fixture_booked_contracts_pagination): """Test getting accounting intervals.""" expected_account_type = 'vendor' expected_account_id = 100 accounting_period._cached_periods = fixture_accounting_periods (flexmock(ows_contracts) .should_receive('get_booked_contracts') .with_args(expected_account_type, expected_account_id, 0) .and_return(response.Response({ 'items': fixture_booked_contracts[0], 'pagination': fixture_booked_contracts_pagination[0]})) .once()) (flexmock(ows_contracts) .should_receive('get_booked_contracts') .with_args(expected_account_type, expected_account_id, 5) .and_return(response.Response({ 'items': fixture_booked_contracts[1], 'pagination': fixture_booked_contracts_pagination[1]})) .once()) result = accounting_intervals.get_intervals_from_contracts( expected_account_type, expected_account_id, 4) assert result.status assert result.message == fixture_accounting_intervals def test_get_intervals_from_contracts_with_wrong_contracts(): """Test getting accounting intervals with wrong booked contracts.""" expected_account_type = 'vendor' expected_account_id = 100 (flexmock(ows_contracts) .should_receive('get_booked_contracts') .with_args(expected_account_type, expected_account_id, 0) .and_return(response.create_error_response('code', 'message'))) result = accounting_intervals.get_intervals_from_contracts( expected_account_type, expected_account_id, 4) assert result.status == 400 def test_get_intervals_from_contracts_with_wrong_period( fixture_booked_contracts, fixture_booked_contracts_pagination): """Test getting accounting intervals with wrong accounting period.""" expected_account_type = 'vendor' expected_account_id = 100 (flexmock(ows_contracts) .should_receive('get_booked_contracts') .with_args(expected_account_type, expected_account_id, 0) .and_return(response.Response({ 'items': fixture_booked_contracts[0], 'pagination': fixture_booked_contracts_pagination[0]}))) (flexmock(accounting_period) .should_receive('get_period_by_id') .and_return(response.create_not_found_response())) result = accounting_intervals.get_intervals_from_contracts( expected_account_type, expected_account_id, 4) assert result.status == 404 def test_get_period_by_id(): """Test get accounting period by id.""" response_dict = { 'year': '2020', 'quarter': 1, 'month': 2, 'period_type': 'month', 'first_period_id': 254, 'last_period_id': 254 } (flexmock( accounting_period).should_receive( 'get_period_by_id').and_return( response.Response(response_dict))) result = accounting_intervals.get_period_by_id(254) assert result.status == 200 assert result.message == response_dict