from unittest.mock import MagicMock from unittest.mock import patch import pytest from ows_accounting import config from ows_accounting.models import period from ows_accounting.models.sql import period as period_sql @patch('ows_accounting.models.period.dynamodb') def test_get_available_accounting_periods(mock_dynamodb): """Test get_available_accounting_periods """ mock_table = MagicMock() mock_dynamodb.query_items.return_value = [ {'file_type': 'AVRO', 'period_ids': '202'}, {'file_type': 'AVRO', 'period_ids': '203'} ] mock_dynamodb.get_table.return_value = mock_table result = period.get_available_accounting_periods(18805, 'vendor') assert result.message == [202, 203] assert result.status == 200 mock_dynamodb.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', file_type='AVRO', status=config.STATUS_GENERATED) @patch('ows_accounting.models.period.dynamodb') def test_get_available_accounting_periods_error(mock_dynamodb): """Test get_available_accounting_periods """ mock_table = MagicMock() mock_dynamodb.query_items.return_value = [] mock_dynamodb.get_table.return_value = mock_table result = period.get_available_accounting_periods(18805, 'vendor') assert not result assert result.status == 404 mock_dynamodb.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', file_type='AVRO', status=config.STATUS_GENERATED) @patch('ows_accounting.models.period.dynamodb') def test_get_available_periods(mock_dynamodb): """Test get_available_periods. """ mock_table = MagicMock() mock_dynamodb.query_items.return_value = [ {'file_type': 'AVRO', 'period_ids': '202'}, {'file_type': 'AVRO', 'period_ids': '203'} ] mock_dynamodb.get_table.return_value = mock_table result = period.get_available_periods('18805L') assert result.message == [202, 203] assert result.status == 200 mock_dynamodb.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', file_type='AVRO', status=config.STATUS_GENERATED) @patch('ows_accounting.models.period.dynamodb') def test_get_available_periods_error(mock_dynamodb): """Test get_available_accounting_periods """ mock_table = MagicMock() mock_dynamodb.query_items.return_value = [] mock_dynamodb.get_table.return_value = mock_table result = period.get_available_periods('18805L') assert not result assert result.status == 400 mock_dynamodb.query_items.assert_any_call( mock_table, 'user_id_type', '18805L', file_type='AVRO', status=config.STATUS_GENERATED) @patch('ows_accounting.models.period.mysql') @pytest.mark.parametrize('account_type', ['vendor', 'subaccount']) def test_get_first_statement_period( mock_mysql, account_type, monkeypatch): mock_result = MagicMock() mock_session = MagicMock() mock_context = MagicMock() mock_fetchone = MagicMock() mock_session.execute.return_value = mock_result mock_mysql.db_session.return_value = mock_context mock_context.__enter__.return_value = mock_session mock_session.__enter__.return_value = mock_fetchone monkeypatch.setattr( mock_fetchone, 'fetchone', MagicMock(return_value=[1])) period.get_first_statement_period(100, account_type) if account_type == 'subaccount': sql = period_sql.SQL_GET_FIRST_STATEMENT_PERIOD_SUBACCOUNT mock_session.execute.assert_called_with(sql, {'account_id': 100}) else: sql = period_sql.SQL_GET_FIRST_STATEMENT_PERIOD_VENDOR mock_session.execute.assert_called_with(sql, {'account_id': 100})