|
| 1 | +import boto3 |
1 | 2 | import pytest |
2 | 3 | from haystack import component |
3 | 4 | from haystack.dataclasses import ChatMessage, Document |
| 5 | +from moto import mock_s3 |
4 | 6 |
|
5 | 7 | from src.adapters import db |
6 | 8 | from src.common import haystack_utils |
@@ -229,3 +231,51 @@ def test_save_to_db(db_session: db.Session): |
229 | 231 | supports = {support.name: support for support in listing_record.supports} |
230 | 232 | assert len(supports) == 1 |
231 | 233 | assert supports["Replacement"].description == "Replacement description" |
| 234 | + |
| 235 | + |
| 236 | +def test_extract_from_pdf_file_not_found(): |
| 237 | + """Test that FileNotFoundError is raised when file doesn't exist locally and smart_open fails.""" |
| 238 | + nonexistent_path = "/path/to/nonexistent/file.pdf" |
| 239 | + |
| 240 | + with pytest.raises(FileNotFoundError, match="File not found: /path/to/nonexistent/file.pdf"): |
| 241 | + extract_supports.extract_from_pdf(nonexistent_path) |
| 242 | + |
| 243 | + |
| 244 | +def test_extract_from_pdf_smart_open_exception(): |
| 245 | + """Test that FileNotFoundError is raised when smart_open throws an exception.""" |
| 246 | + # Test with an invalid S3 URI that will cause smart_open to fail |
| 247 | + invalid_s3_uri = "s3://nonexistent-bucket/nonexistent-file.pdf" |
| 248 | + |
| 249 | + with pytest.raises( |
| 250 | + FileNotFoundError, match="File not found: s3://nonexistent-bucket/nonexistent-file.pdf" |
| 251 | + ): |
| 252 | + extract_supports.extract_from_pdf(invalid_s3_uri) |
| 253 | + |
| 254 | + |
| 255 | +@mock_s3 |
| 256 | +def test_extract_from_pdf_with_s3_file(): |
| 257 | + # Create mock S3 bucket and upload the sample PDF |
| 258 | + bucket_name = "test-bucket" |
| 259 | + key = "test-files/SampleBasicNeedsGuide.pdf" |
| 260 | + s3_uri = f"s3://{bucket_name}/{key}" |
| 261 | + |
| 262 | + # Set up mock S3 |
| 263 | + s3_client = boto3.client("s3", region_name="us-east-1") |
| 264 | + s3_client.create_bucket(Bucket=bucket_name) |
| 265 | + |
| 266 | + # Read the sample PDF and upload to mock S3 |
| 267 | + with open("tests/sample_data/SampleBasicNeedsGuide.pdf", "rb") as f: |
| 268 | + pdf_content = f.read() |
| 269 | + |
| 270 | + s3_client.put_object(Bucket=bucket_name, Key=key, Body=pdf_content) |
| 271 | + |
| 272 | + # Test extracting from S3 URI |
| 273 | + document = extract_supports.extract_from_pdf(s3_uri) |
| 274 | + |
| 275 | + # Verify the document was extracted successfully |
| 276 | + assert document is not None |
| 277 | + assert document.content is not None |
| 278 | + assert len(document.content) > 0 |
| 279 | + |
| 280 | + # Verify it contains expected content from the PDF |
| 281 | + assert "Hope" in document.content and "Harbor" in document.content |
0 commit comments