"""Test accounting_period model.""" from flask import g from royalties.constants import constants from royalties.models.accounting_period import AccountingPeriod from royalties.tests.utils.factories import ( AccountingPeriodFactory, AccountingRunFactory, StatementPeriodFactory, ) def test_get_current_period(): """Test get current period returns the open accounting period.""" statement_period = StatementPeriodFactory() AccountingPeriodFactory.create_batch(size=10, closed_date='2019-01-15') current_period = AccountingPeriodFactory( closed_date=None, statement_period=statement_period ) contract_type = constants.CONTRACT_TYPES.DISTRIBUTION result = AccountingPeriod.get_current_period( statement_period.statement_period_id, contract_type ) assert result == current_period def test_get_s3_folder_name(): """Test get_s3_folder_name.""" test_period = AccountingPeriodFactory.build( accounting_period_id=2, accounting_period_name='April 3030 #2' ) result = test_period.get_s3_folder_name() assert result == '2-April30302' def test_is_new_acc_period_status_valid(): """Test is_new_acc_period_status_valid.""" g.user_details = {'type': 'foo', 'id': '17'} open_status = AccountingPeriodFactory.build( accounting_period_status=constants.ACCOUNTING_PERIOD_STATUSES.OPEN ) closed_status = AccountingPeriodFactory.build( accounting_period_status=constants.ACCOUNTING_PERIOD_STATUSES.CLOSED ) AccountingRunFactory.build( accounting_period=closed_status, run_status=constants.ACCOUNTING_RUN_STATUSES.COMMITTED, ) AccountingRunFactory.build( accounting_period=closed_status, run_status=constants.ACCOUNTING_RUN_STATUSES.SKIPPED, ) AccountingRunFactory.build( accounting_period=closed_status, run_status=constants.ACCOUNTING_RUN_STATUSES.SENT_TO_WORKSTATION, ) AccountingRunFactory.build( accounting_period=closed_status, run_status=constants.ACCOUNTING_RUN_STATUSES.EXPORT_PAYMENTS, ) AccountingRunFactory.build( accounting_period=closed_status, run_status=constants.ACCOUNTING_RUN_STATUSES.INVALID, ) assert ( open_status.is_new_acc_period_status_valid( constants.ACCOUNTING_PERIOD_STATUSES.CLOSED ) is True ) assert ( closed_status.is_new_acc_period_status_valid( constants.ACCOUNTING_PERIOD_STATUSES.OPEN ) is False ) def test_get_filtered_query(): """Get accounts by request parameters.""" AccountingPeriodFactory.create(is_visible=True) AccountingPeriodFactory.create( is_visible=False, accounting_period_name='Test Accounting Period' ) AccountingPeriodFactory.create(is_visible=True) accounting_periods = AccountingPeriod.get_filtered_query().all() assert len(accounting_periods) == 3 accounting_periods = AccountingPeriod.get_filtered_query(is_visible=None).all() assert len(accounting_periods) == 3 accounting_periods = AccountingPeriod.get_filtered_query(is_visible=False).all() assert len(accounting_periods) == 1 assert accounting_periods[0].accounting_period_name == 'Test Accounting Period' accounting_periods = AccountingPeriod.get_filtered_query(is_visible=True).all() assert len(accounting_periods) == 2