"""Test data module.""" import datetime from unittest.mock import Mock from unittest.mock import patch import pytest from accounting.cacheable import cache_item from accounting.cacheable import get_cached_item from accounting.data import buffered_cache_appended_fields from accounting.data import check_cache_validity from accounting.data import get_transaction_start_id from accounting.data import get_transaction_start_id_from_warehouse from accounting.data import load_statements from accounting.models.statement import Statement @patch('accounting.cacheable.get_cache_adapter') @patch('accounting.data.get_transaction_start_id_from_warehouse') @patch('accounting.data.get_warehouse_adapter') def test_get_transaction_start_id( warehouse_getter, mock_fetch, cache_getter, mock_cache_adapter, mock_warehouse_adapter): """Test retrieving the max statement_detail_id.""" fake_value = 3 mock_cache_adapter.flushall() warehouse_getter.return_value = mock_warehouse_adapter cache_getter.return_value = mock_cache_adapter mock_fetch.return_value = fake_value found_id = get_transaction_start_id() assert found_id == fake_value class MockWarehouseAdapter(): """Mock warehouse adapter class for testing. Constructor accepts an arbitrary value and returns it by calling execute() """ def __init__(self, value=False): """Store a value passed during construction. Args: value (mixed): arbitrary value to stash in the instance. """ self.stored_value = value def execute(self, sql): """Return the stored value.""" return self.stored_value @patch('accounting.data.get_warehouse_adapter') def test_get_transaction_start_id_from_warehouse(warehouse_getter): """Test getting the start_id (int) from the data warehouse.""" fake_value = 3 warehouse_getter.return_value = MockWarehouseAdapter([[fake_value]]) returned_value = get_transaction_start_id_from_warehouse() assert returned_value == fake_value @patch('accounting.cacheable.get_cache_adapter') @patch('accounting.data.get_warehouse_adapter') def test_get_cached_transaction_start_id( warehouse_getter, cache_getter, mock_warehouse_adapter, mock_cache_adapter): """Test getting the start_id from the cache.""" fake_value = 3 warehouse_getter.return_value = mock_warehouse_adapter cache_getter.return_value = mock_cache_adapter cache_item('data', 'start_id', fake_value) returned_value = get_transaction_start_id() assert returned_value == fake_value @patch('accounting.cacheable.get_cache_adapter') @patch('accounting.adapters.file.read_statement_file') def test_load_statements_no_file( mock_reader, adapter_getter, mock_cache_adapter): """Test loading statements when file does not exist.""" with pytest.raises(FileNotFoundError): adapter_getter.return_value = mock_cache_adapter mock_cache_adapter.flushall() load_statements('fake') @patch('accounting.data.get_db_adapter') @patch('accounting.adapters.db.DatabaseAdapter.yield_rows') @patch('accounting.cacheable.get_cache_adapter') def test_cache_transaction_appended_fields_from_db( cache_adapter_getter, mock_fetch, db_adapter_getter, mock_db_adapter, mock_cache_adapter): """Test getting appended fields from the database.""" mock_fetch.return_value = [(1, 1, 'in_content', 1)] db_adapter_getter.return_value = mock_db_adapter buffered_cache_appended_fields() @patch('accounting.cacheable.get_cache_adapter') def test_cache_item_found(cache_adapter_getter, mock_cache_adapter): """Test getting a value that is in the cache.""" cache_adapter_getter.return_value = mock_cache_adapter mock_cache_adapter.flushall() mock_cache_adapter.cache_item('dt_test_value', 'text') returned_data = get_cached_item('data', 'test_value') assert returned_data == 'text' def mocking_helper(name, uuid): """Test helper. Replaces get_cached_dict to force reaching an exception. """ if name == 'statement': return Statement() return None @patch('accounting.data.get_db_adapter') def test_cache_appended_fields_no_response(mock_db_getter): """Test a faulty db request.""" mock_db_getter.return_value = Mock() with pytest.raises(Exception) as err: buffered_cache_appended_fields() assert 'unknown state' in str(err.value) @patch('accounting.cacheable.get_cache_adapter') def test_cache_validity_valid(cache_adapter_getter, mock_cache_adapter): """Test checking a valid cache.""" cache_adapter_getter.return_value = mock_cache_adapter mock_cache_adapter.flushall() cache_item( 'data', 'last_updated', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) check_cache_validity() @patch('accounting.cacheable.get_cache_adapter') def test_cache_validity_empty(cache_adapter_getter, mock_cache_adapter): """Test checking an empty cache.""" cache_adapter_getter.return_value = mock_cache_adapter mock_cache_adapter.flushall() with pytest.raises(Exception): check_cache_validity() @patch('accounting.cacheable.get_cache_adapter') def test_cache_validity_expired(cache_adapter_getter, mock_cache_adapter): """Test checking an expired cache.""" cache_adapter_getter.return_value = mock_cache_adapter mock_cache_adapter.flushall() two_days_ago = datetime.datetime.strftime( datetime.datetime.now() - datetime.timedelta(1), '%Y-%m-%d') cache_item('data', 'last_updated', two_days_ago) with pytest.raises(Exception): check_cache_validity() @patch('accounting.cacheable.get_cached_item') def test_expired_cache(mock_get_item): """Test checking an expired cache.""" mock_get_item.return_value = '1980-01-01 01:01:00' with pytest.raises(Exception): check_cache_validity()