"""Tests for Accounting period model.""" from unittest.mock import patch from sqlalchemy.exc import SQLAlchemyError from ows_accounting.models import accounting_period def test_get_period_by_id(db_fixture): """Test getting accounting period by id.""" expected_response = { 'period_type': 'month', 'first_period_id': 2, 'last_period_id': 2, 'year': 2017, 'quarter': 1, 'month': 2 } response = accounting_period.get_period_by_id(2) assert response.status assert response.message == expected_response def test_get_period_by_id_from_cache_list(db_fixture): """Test getting accounting period by id from cached list.""" expected_response = { 'period_type': 'month', 'first_period_id': 5, 'last_period_id': 5, 'year': 2018, 'quarter': 4, 'month': 12, } accounting_period._cached_periods.append( accounting_period.AccountingPeriod( period_id=5, year=2018, quarter=4, month=12).as_dict()) response = accounting_period.get_period_by_id(5) assert response.status assert response.message == expected_response def test_get_period_by_id_not_found(db_fixture): """Test getting no accounting period.""" response = accounting_period.get_period_by_id(105) assert response.status == 404 @patch('ows_accounting.utils.mysql.db_session', side_effect=SQLAlchemyError()) def test_get_currency_by_id_exception(monkeypatch): """Test getting accounting period with exception.""" response = accounting_period.get_period_by_id(100) assert response.status == 500 def test_get_monthly_period_information(): expected_response = [{ 'period_type': 'month', 'first_period_id': 3, 'last_period_id': 3, 'year': 2017, 'quarter': 1, 'month': 3 }, { 'period_type': 'month', 'first_period_id': 4, 'last_period_id': 4, 'year': 2017, 'quarter': 2, 'month': 4 }, { 'period_type': 'month', 'first_period_id': 5, 'last_period_id': 5, 'year': 2017, 'quarter': 2, 'month': 5 }, { 'period_type': 'month', 'first_period_id': 6, 'last_period_id': 6, 'year': 2017, 'quarter': 2, 'month': 6 }] response = accounting_period.get_monthly_period_information(3, 6) assert response.status assert response.message == expected_response def test_get_quarterly_period_information_all_current_querters(): expected_response = [{ 'period_type': 'quarter', 'first_period_id': 1, 'last_period_id': 3, 'year': 2017, 'quarter': 1, 'month': None }, { 'period_type': 'quarter', 'first_period_id': 4, 'last_period_id': 6, 'year': 2017, 'quarter': 2, 'month': None }, { 'period_type': 'quarter', 'first_period_id': 7, 'last_period_id': 9, 'year': 2017, 'quarter': 3, 'month': None }] response = accounting_period.get_quarterly_period_information(3, 9) assert response.status assert response.message == expected_response def test_get_quarterly_period_information_skip_last_quarter(): expected_response = [{ 'period_type': 'quarter', 'first_period_id': 1, 'last_period_id': 3, 'year': 2017, 'quarter': 1, 'month': None }, { 'period_type': 'quarter', 'first_period_id': 4, 'last_period_id': 6, 'year': 2017, 'quarter': 2, 'month': None }] response = accounting_period.get_quarterly_period_information(3, 8) assert response.status assert response.message == expected_response