"""Test DynamoDB utility functions.""" from unittest import mock import pytest from accounting.bin.build_helper import commands from accounting.bin.build_helper.commands import reserve_payouts @mock.patch('accounting.bin.build_helper.commands.os') def test_prevent_prod_execution(os): """Test prevent_prod_execution decorator.""" os.getenv.return_value = 'prod' with pytest.raises(EnvironmentError): commands.prevent_prod_execution(int)('1') os.getenv.return_value = 'qa' assert commands.prevent_prod_execution(int)('1') == 1 @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts' '.dynamodb') @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts' '.reserve_payout_setting') @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts.dynamodb_util') def test_dynamodb_clean( dynamodb_util, reserve_payout_setting, dynamodb): """Test dynamodb_clean function.""" table_name = 'qa_table' reserve_payout_setting.DYNAMODB_TABLE = table_name mock_table = mock.MagicMock() batch_writer = mock.MagicMock() batch_writer.__enter__.return_value = batch_writer mock_table.batch_writer.return_value = batch_writer dynamodb_util.get_dynamodb_table.return_value = mock_table keys = [1, 2] dynamodb.get_table_label_ids.return_value = keys # execute the function reserve_payouts.dynamodb_clean() dynamodb_util.get_dynamodb_table.assert_called_with(table_name) dynamodb.get_table_label_ids.assert_called_once() expected_calls = [ mock.call(Key=1), mock.call(Key=2), ] batch_writer.delete_item.assert_has_calls(expected_calls, any_order=True) @mock.patch('accounting.bin.build_helper.commands.os') def test_dynamodb_clean_raises_if_prod(os): """Test dynamodb_clean function is raising exception if prod env.""" os.getenv.return_value = 'prod' with pytest.raises(EnvironmentError): reserve_payouts.dynamodb_clean() @mock.patch('accounting.bin.build_helper.commands.os') def test_sqs_clean_raises(os): """Test sqs_clean function is raising exception if prod env.""" os.getenv.return_value = 'prod' with pytest.raises(EnvironmentError): reserve_payouts.sqs_clean() @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts.sqs') @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts' '.reserve_payout_setting') def test_sqs_clean(reserve_payout_setting, sqs): """Test sqs_clean function.""" queue_name = 'test_queue_name' reserve_payout_setting.SQS_QUEUE = queue_name mock_queue = mock.MagicMock() sqs.get_queue_by_name.return_value = mock_queue reserve_payouts.sqs_clean() sqs.get_queue_by_name.assert_called_once_with(queue_name) mock_queue.purge.assert_has_calls([mock.call()]) @mock.patch( 'accounting.bin.build_helper.commands.reserve_payouts.mysql') def test_filter_phys_transactions(mysql): """Test filter_phys_transactions function.""" period_id = 4242 reserve_payouts.filter_phys_transactions(period_id) mysql.truncate_reserves_temp_table.assert_has_calls([mock.call()]) mysql.populate_reserves_temp_table.assert_called_once_with_(period_id)