|
1 | 1 | import sys |
2 | 2 | from datetime import datetime, timezone |
| 3 | +from pathlib import Path |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from inline_snapshot import snapshot |
@@ -612,3 +613,37 @@ def test_binary_content_validation_with_optional_identifier(): |
612 | 613 | 'identifier': 'foo', |
613 | 614 | } |
614 | 615 | ) |
| 616 | + |
| 617 | + |
| 618 | +def test_binary_content_from_path(tmp_path: Path): |
| 619 | + # test normal file |
| 620 | + test_xml_file = tmp_path / 'test.xml' |
| 621 | + test_xml_file.write_text('<think>about trains</think>', encoding='utf-8') |
| 622 | + binary_content = BinaryContent.from_path(test_xml_file) |
| 623 | + assert binary_content == snapshot(BinaryContent(data=b'<think>about trains</think>', media_type='application/xml')) |
| 624 | + |
| 625 | + # test non-existent file |
| 626 | + non_existent_file = tmp_path / 'non-existent.txt' |
| 627 | + with pytest.raises(FileNotFoundError, match='File not found:'): |
| 628 | + BinaryContent.from_path(non_existent_file) |
| 629 | + |
| 630 | + # test file with unknown media type |
| 631 | + test_unknown_file = tmp_path / 'test.unknownext' |
| 632 | + test_unknown_file.write_text('some content', encoding='utf-8') |
| 633 | + binary_content = BinaryContent.from_path(test_unknown_file) |
| 634 | + assert binary_content == snapshot(BinaryContent(data=b'some content', media_type='application/octet-stream')) |
| 635 | + |
| 636 | + # test string path |
| 637 | + test_txt_file = tmp_path / 'test.txt' |
| 638 | + test_txt_file.write_text('just some text', encoding='utf-8') |
| 639 | + string_path = test_txt_file.as_posix() |
| 640 | + binary_content = BinaryContent.from_path(string_path) # pyright: ignore[reportArgumentType] |
| 641 | + assert binary_content == snapshot(BinaryContent(data=b'just some text', media_type='text/plain')) |
| 642 | + |
| 643 | + # test image file |
| 644 | + test_jpg_file = tmp_path / 'test.jpg' |
| 645 | + test_jpg_file.write_bytes(b'\xff\xd8\xff\xe0' + b'0' * 100) # minimal JPEG header + padding |
| 646 | + binary_content = BinaryContent.from_path(test_jpg_file) |
| 647 | + assert binary_content == snapshot( |
| 648 | + BinaryImage(data=b'\xff\xd8\xff\xe0' + b'0' * 100, media_type='image/jpeg', _identifier='bc8d49') |
| 649 | + ) |
0 commit comments