"""Tests for contract logic.""" from unittest.mock import MagicMock, patch from royalties.logic import accounting_period_search as logic from royalties.schemas import AccountingPeriodDetailSchema from royalties.tests.utils.factories import AccountingPeriodFactory def test__execute_paged_query(): """Test pagination.""" query = MagicMock() query.order_by.return_value = query.limit.return_value = query.all.return_value = ( query.offset.return_value ) = query logic._execute_paged_query(query, limit=100, offset=0) query.limit.assert_called_once_with(100) query.offset.assert_called_once_with(0) query.all.assert_called_once() @patch('royalties.models.accounting_period.AccountingPeriod.get_filtered_query') @patch('royalties.logic.accounting_period_search._execute_paged_query') @patch('royalties.logic.accounting_period_search._get_is_visible_from_params') def test__execute_accounting_period_query( mock_get_is_visible_from_params, mock_execute_paged_query, mock_get_filtered_query ): """Test contract searching function.""" raw_params = { 'is_visible': '1', } params = { 'is_visible': True, } mock_get_is_visible_from_params.return_value = True logic._execute_accounting_period_query(raw_params) mock_get_is_visible_from_params.assert_called_once_with(raw_params) mock_get_filtered_query.assert_called_once_with(**params) mock_execute_paged_query.assert_called_once() @patch('royalties.logic.accounting_period_search._execute_accounting_period_query') def test_get_accounting_periods(mock_execute_accounting_period_query): """Test main search function.""" params = {'contract_name': 'Test Contract', 'account_ids': '1,2'} accountingPeriods = AccountingPeriodFactory.create_batch(3) mock_execute_accounting_period_query.return_value = ( accountingPeriods, len(accountingPeriods), ) res = logic.get_accounting_periods(params) mock_execute_accounting_period_query.assert_called_once_with(params) assert res.message == { 'items': AccountingPeriodDetailSchema().dump(accountingPeriods, many=True), 'total_count': len(accountingPeriods), }