|
| 1 | +import pytest |
| 2 | +from io import BytesIO |
| 3 | +from unittest.mock import patch |
| 4 | +from datetime import datetime, timezone |
| 5 | + |
| 6 | +from ingestify.main import get_engine |
| 7 | +from ingestify.domain import Dataset, Identifier, Revision, File |
| 8 | +from ingestify.domain.models.dataset.revision import RevisionSource, SourceType |
| 9 | + |
| 10 | + |
| 11 | +def test_file_cache(config_file): |
| 12 | + """Test file caching with the with_file_cache context manager.""" |
| 13 | + # Get engine from the fixture |
| 14 | + engine = get_engine(config_file, "main") |
| 15 | + store = engine.store |
| 16 | + |
| 17 | + # Create a timestamp for test data |
| 18 | + now = datetime.now(timezone.utc) |
| 19 | + |
| 20 | + # Create a test file |
| 21 | + test_file = File( |
| 22 | + file_id="test_file_id", |
| 23 | + data_feed_key="test_file", |
| 24 | + tag="test_tag", |
| 25 | + data_serialization_format="txt", |
| 26 | + storage_path="test/path", |
| 27 | + storage_size=100, |
| 28 | + storage_compression_method="none", |
| 29 | + created_at=now, |
| 30 | + modified_at=now, |
| 31 | + size=100, |
| 32 | + content_type="text/plain", |
| 33 | + data_spec_version="v1", |
| 34 | + ) |
| 35 | + |
| 36 | + # Create a test revision with the file |
| 37 | + revision = Revision( |
| 38 | + revision_id=1, |
| 39 | + created_at=now, |
| 40 | + description="Test revision", |
| 41 | + modified_files=[test_file], |
| 42 | + source={"source_type": SourceType.MANUAL, "source_id": "test"}, |
| 43 | + ) |
| 44 | + |
| 45 | + # Create a test dataset with the revision |
| 46 | + dataset = Dataset( |
| 47 | + bucket="test-bucket", |
| 48 | + dataset_id="test-dataset", |
| 49 | + name="Test Dataset", |
| 50 | + state="COMPLETE", |
| 51 | + identifier=Identifier(test_id=1), |
| 52 | + dataset_type="test", |
| 53 | + provider="test-provider", |
| 54 | + metadata={}, |
| 55 | + created_at=now, |
| 56 | + updated_at=now, |
| 57 | + last_modified_at=now, |
| 58 | + revisions=[revision], |
| 59 | + ) |
| 60 | + |
| 61 | + # Create a simple pass-through reader function to replace the gzip reader |
| 62 | + def simple_reader(stream): |
| 63 | + return stream |
| 64 | + |
| 65 | + # Mock both the file repository and the _prepare_read_stream method |
| 66 | + with patch.object( |
| 67 | + store.file_repository, "load_content" |
| 68 | + ) as mock_load_content, patch.object( |
| 69 | + store, "_prepare_read_stream" |
| 70 | + ) as mock_prepare_read_stream: |
| 71 | + |
| 72 | + # Set up the mocks |
| 73 | + mock_load_content.return_value = BytesIO(b"test content") |
| 74 | + mock_prepare_read_stream.return_value = (simple_reader, "") |
| 75 | + |
| 76 | + # Test without caching - should load files twice |
| 77 | + store.load_files(dataset) |
| 78 | + store.load_files(dataset) |
| 79 | + |
| 80 | + # Should have called load_content twice (without caching) |
| 81 | + assert mock_load_content.call_count == 2 |
| 82 | + |
| 83 | + # Reset the mock |
| 84 | + mock_load_content.reset_mock() |
| 85 | + |
| 86 | + # Test with caching - should load files only once |
| 87 | + with store.with_file_cache(): |
| 88 | + store.load_files(dataset) |
| 89 | + store.load_files(dataset) |
| 90 | + |
| 91 | + # Should have called load_content only once (with caching) |
| 92 | + assert mock_load_content.call_count == 1 |
| 93 | + |
| 94 | + # After exiting context, caching should be disabled |
| 95 | + store.load_files(dataset) |
| 96 | + |
| 97 | + # Should have called load_content again |
| 98 | + assert mock_load_content.call_count == 2 |
0 commit comments