1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
|
# ruff: noqa: PLC2701 DOC402 ANN201 RUF043
import logging
from collections.abc import AsyncGenerator, Generator
from contextlib import suppress
from pathlib import Path
from typing import Optional
import pytest
from minio import Minio
from pytest_databases.docker.minio import MinioService
from sqlalchemy import Engine, String, create_engine, event
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
from advanced_alchemy._listeners import set_async_context, setup_file_object_listeners
from advanced_alchemy.base import create_registry
from advanced_alchemy.exceptions import ImproperConfigurationError
from advanced_alchemy.types.file_object import (
FileObject,
FileObjectList,
StoredObject,
)
from advanced_alchemy.types.file_object.backends.fsspec import FSSpecBackend
from advanced_alchemy.types.file_object.backends.obstore import ObstoreBackend
from advanced_alchemy.types.file_object.registry import StorageRegistry, storages
from advanced_alchemy.types.mutables import MutableList
# Setup logger
logger = logging.getLogger(__name__)
pytestmark = pytest.mark.integration
def remove_listeners() -> None:
"""Remove file object listeners safely to prevent test interactions."""
from sqlalchemy.event import contains
from advanced_alchemy._listeners import FileObjectListener
# Only try to remove listeners if they're actually registered
if contains(Session, "before_flush", FileObjectListener.before_flush):
with suppress(InvalidRequestError):
event.remove(Session, "before_flush", FileObjectListener.before_flush)
if contains(Session, "after_commit", FileObjectListener.after_commit):
with suppress(InvalidRequestError):
event.remove(Session, "after_commit", FileObjectListener.after_commit)
if contains(Session, "after_rollback", FileObjectListener.after_rollback):
with suppress(InvalidRequestError):
event.remove(Session, "after_rollback", FileObjectListener.after_rollback)
# Reset async context flag to ensure clean state
set_async_context(False)
# --- Fixtures ---
orm_registry = create_registry()
# --- SQLAlchemy Model Definition ---
class Base(DeclarativeBase):
metadata = orm_registry.metadata
class Document(Base):
__tablename__ = "documents"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(50))
# Single file storage
attachment: Mapped[Optional[FileObject]] = mapped_column(
StoredObject(backend="local_test_store"), # Use StoredObject wrapper
nullable=True,
)
# Multiple file storage
images: Mapped[Optional[FileObjectList]] = mapped_column(
StoredObject(backend="local_test_store", multiple=True), # Use StoredObject wrapper
nullable=True,
)
@pytest.fixture()
def storage_registry(tmp_path: Path) -> "StorageRegistry":
"""Clears and returns the global storage registry for the module.
Returns:
StorageRegistry: The global storage registry.
"""
from obstore.store import LocalStore, MemoryStore
if not storages.is_registered("memory"):
storages.register_backend(ObstoreBackend(fs=MemoryStore(), key="memory"))
if storages.is_registered("local_test_store"):
storages.unregister_backend("local_test_store")
# Create the storage directory
storage_dir = tmp_path / "file_object_test_storage"
storage_dir.mkdir(parents=True, exist_ok=True)
storages.register_backend(
ObstoreBackend(
fs=LocalStore(prefix=str(storage_dir)), # pyright: ignore
key="local_test_store",
)
)
return storages
@pytest.fixture()
def sync_db_engine(tmp_path: Path) -> Generator[Engine, None, None]:
"""Provides an SQLite engine scoped to each function."""
db_file = tmp_path / "test_file_object_sync.db"
engine = create_engine(f"sqlite:///{db_file}", execution_options={"enable_file_object_listener": True})
yield engine
db_file.unlink(missing_ok=True)
@pytest.fixture()
def async_db_engine(tmp_path: Path) -> Generator[AsyncEngine, None, None]:
"""Provides an SQLite engine scoped to each function."""
db_file = tmp_path / "test_file_object_async.db"
engine = create_async_engine(
f"sqlite+aiosqlite:///{db_file}", execution_options={"enable_file_object_listener": True}
)
yield engine
db_file.unlink(missing_ok=True)
@pytest.fixture()
def session(
sync_db_engine: Engine, storage_registry: "StorageRegistry"
) -> Generator[Session, None, None]: # Depend on sqlalchemy_config to ensure setup runs
"""Provides a SQLAlchemy session scoped to each function."""
Base.metadata.create_all(sync_db_engine)
with Session(sync_db_engine) as db_session:
yield db_session
Base.metadata.drop_all(sync_db_engine)
@pytest.fixture()
async def async_session(
async_db_engine: AsyncEngine, storage_registry: "StorageRegistry"
) -> AsyncGenerator[AsyncSession, None]: # Depend on sqlalchemy_config to ensure setup runs
"""Provides a SQLAlchemy session scoped to each function."""
async with async_db_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# Create session with flag for listener to identify async operations
set_async_context(True)
# Create session factory
async_session_factory = async_sessionmaker(
async_db_engine,
expire_on_commit=False,
)
# Create session
async with async_session_factory() as db_session:
# Add flag to session.info dictionary
db_session.info["enable_file_object_listener"] = True
logger.debug(f"Created async session: {id(db_session)}, with info: {db_session.info}")
yield db_session
# Reset async context flag
set_async_context(False)
async with async_db_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await async_db_engine.dispose()
@pytest.mark.xdist_group("file_object")
async def test_fsspec_s3_basic_operations_async(
storage_registry: StorageRegistry,
minio_client: "Minio",
minio_service: "MinioService",
minio_default_bucket_name: str,
) -> None:
"""Test basic save, get_content, delete via backend and FileObject with prefix."""
remove_listeners()
try:
import s3fs
except ImportError:
pytest.skip("s3fs not installed")
assert minio_client.bucket_exists(minio_default_bucket_name)
_ = minio_client
# Create s3fs filesystem instance without bucket info
fs = s3fs.S3FileSystem(
anon=False,
key=minio_service.access_key,
secret=minio_service.secret_key,
endpoint_url=f"http://{minio_service.endpoint}",
client_kwargs={
"verify": False,
"use_ssl": False,
},
)
# Initialize backend with prefix
backend = FSSpecBackend(
key="s3_test_store",
fs=fs,
prefix=minio_default_bucket_name,
)
test_content = b"Hello Storage!"
# Use relative path, prefix handles the bucket
file_path = "test_basic_s3_async.txt"
# Create initial FileObject with relative path
obj = FileObject(backend=backend, filename="test_basic_s3_async.txt", to_filename=file_path)
# Save using backend
updated_obj = await backend.save_object_async(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path # Path should remain relative
assert obj.filename == "test_basic_s3_async.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "s3" # Based on s3fs filesystem
# Retrieve content via FileObject method (uses relative obj.path, backend adds prefix)
retrieved_content = await obj.get_content_async()
assert retrieved_content == test_content
# Test sign_async method
url_async = await obj.sign_async(expires_in=3600)
assert isinstance(url_async, str)
assert url_async.startswith("http")
# Test for_upload parameter
with pytest.raises(
NotImplementedError,
match=r"Generating signed URLs for upload is generally not supported by fsspec's generic sign method.",
):
_ = await obj.sign_async(for_upload=True)
# Delete via FileObject method (uses relative obj.path, backend adds prefix)
await obj.delete_async()
# Verify deletion using relative path with backend (backend adds prefix)
with pytest.raises(FileNotFoundError):
await backend.get_content_async(file_path)
@pytest.mark.xdist_group("file_object")
def test_fsspec_s3_basic_operations_sync(
storage_registry: StorageRegistry,
minio_client: "Minio",
minio_service: "MinioService",
minio_default_bucket_name: str,
) -> None:
"""Test basic save, get_content, delete via backend and FileObject with prefix."""
remove_listeners()
try:
import s3fs
except ImportError:
pytest.skip("s3fs not installed")
assert minio_client.bucket_exists(minio_default_bucket_name)
_ = minio_client
# Create s3fs filesystem instance without bucket info
fs = s3fs.S3FileSystem(
anon=False,
key=minio_service.access_key,
secret=minio_service.secret_key,
endpoint_url=f"http://{minio_service.endpoint}",
client_kwargs={
"verify": False,
"use_ssl": False,
},
asynchronous=False,
loop=None,
)
# Initialize backend with prefix
backend = FSSpecBackend(
key="s3_test_store",
fs=fs,
prefix=minio_default_bucket_name,
)
test_content = b"Hello Storage!"
# Use relative path, prefix handles the bucket
file_path = "test_basic_s3_sync.txt"
# Create initial FileObject with relative path
obj = FileObject(backend=backend, filename="test_basic_s3_sync.txt", to_filename=file_path)
# Save using backend
updated_obj = backend.save_object(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path # Path should remain relative
assert obj.filename == "test_basic_s3_sync.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "s3" # Based on s3fs filesystem
# Retrieve content via FileObject method (uses relative obj.path, backend adds prefix)
retrieved_content = obj.get_content()
assert retrieved_content == test_content
# Test sign_async method
url_async = obj.sign(expires_in=3600)
assert isinstance(url_async, str)
assert url_async.startswith("http")
# Test for_upload parameter
with pytest.raises(
NotImplementedError,
match=r"Generating signed URLs for upload is generally not supported by fsspec's generic sign method.",
):
_ = obj.sign(for_upload=True)
# Delete via FileObject method (uses relative obj.path, backend adds prefix)
obj.delete()
# Verify deletion using relative path with backend (backend adds prefix)
with pytest.raises(FileNotFoundError):
backend.get_content(file_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_s3_basic_operations_async(
storage_registry: StorageRegistry,
minio_client: "Minio",
minio_service: "MinioService",
minio_default_bucket_name: str,
) -> None:
"""Test basic save, get_content, delete via backend and FileObject."""
remove_listeners()
assert minio_client.bucket_exists(minio_default_bucket_name)
_ = minio_client
backend = ObstoreBackend(
key="s3_test_store",
fs=f"s3://{minio_default_bucket_name}/",
aws_endpoint=f"http://{minio_service.endpoint}/",
aws_access_key_id=minio_service.access_key,
aws_secret_access_key=minio_service.secret_key,
aws_virtual_hosted_style_request=False,
client_options={"allow_http": True},
)
test_content = b"Hello Storage!"
file_path = "test_basic_s3_async.txt" # Relative path for the backend
# Create initial FileObject
obj = FileObject(backend=backend, filename="test_basic_s3_async.txt", to_filename=file_path)
# Save using backend
updated_obj = await backend.save_object_async(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path
assert obj.filename == "test_basic_s3_async.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "s3" # Based on LocalFileSystem
# Retrieve content via FileObject method
retrieved_content = await obj.get_content_async()
assert retrieved_content == test_content
# Test sign method
url = obj.sign(expires_in=3600)
assert isinstance(url, str)
assert url.startswith("http")
# Test sign_async method
url_async = await obj.sign_async(expires_in=3600)
assert isinstance(url_async, str)
assert url_async.startswith("http")
url_for_upload_async = await obj.sign_async(for_upload=True)
assert isinstance(url_for_upload_async, str)
assert url_for_upload_async.startswith("http")
# Delete via FileObject method
await obj.delete_async()
# Verify deletion (expect FileNotFoundError or similar from backend)
with pytest.raises(FileNotFoundError):
await backend.get_content_async(file_path)
@pytest.mark.xdist_group("file_object")
def test_obstore_s3_basic_operations_sync(
storage_registry: StorageRegistry,
minio_client: "Minio",
minio_service: "MinioService",
minio_default_bucket_name: str,
) -> None:
"""Test basic save, get_content, delete via backend and FileObject."""
remove_listeners()
assert minio_client.bucket_exists(minio_default_bucket_name)
_ = minio_client
backend = ObstoreBackend(
key="s3_test_store",
fs=f"s3://{minio_default_bucket_name}/",
aws_endpoint=f"http://{minio_service.endpoint}/",
aws_access_key_id=minio_service.access_key,
aws_secret_access_key=minio_service.secret_key,
aws_virtual_hosted_style_request=False,
client_options={"allow_http": True},
)
test_content = b"Hello Storage!"
file_path = "test_basic_s3_sync.txt" # Relative path for the backend
# Create initial FileObject
obj = FileObject(backend=backend, filename="test_basic_s3_sync.txt", to_filename=file_path)
# Save using backend
updated_obj = backend.save_object(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path
assert obj.filename == "test_basic_s3_sync.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "s3" # Based on LocalFileSystem
# Retrieve content via FileObject method
retrieved_content = obj.get_content()
assert retrieved_content == test_content
# Test sign method
url = obj.sign(expires_in=3600)
assert isinstance(url, str)
assert url.startswith("http")
# Test sign_async method
url_async = obj.sign(expires_in=3600)
assert isinstance(url_async, str)
assert url_async.startswith("http")
# Test for_upload parameter
url_for_upload = obj.sign(for_upload=True)
assert isinstance(url_for_upload, str)
assert url_for_upload.startswith("http")
# Delete via FileObject method
obj.delete()
# Verify deletion (expect FileNotFoundError or similar from backend)
with pytest.raises(FileNotFoundError):
backend.get_content(file_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_basic_operations_async(storage_registry: StorageRegistry) -> None:
"""Test basic save, get_content, delete via backend and FileObject."""
remove_listeners()
backend = storage_registry.get_backend("local_test_store")
test_content = b"Hello Storage!"
file_path = "test_basic_async.txt" # Relative path for the backend
# Create initial FileObject
obj = FileObject(backend=backend, filename="test_basic_async.txt", to_filename=file_path)
# Save using backend
updated_obj = await backend.save_object_async(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path
assert obj.filename == "test_basic_async.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "file" # Based on LocalFileSystem
# Retrieve content via FileObject method
retrieved_content = await obj.get_content_async()
assert retrieved_content == test_content
# Delete via FileObject method
await obj.delete_async()
# Verify deletion (expect FileNotFoundError or similar from backend)
with pytest.raises(FileNotFoundError):
await backend.get_content_async(file_path)
@pytest.mark.xdist_group("file_object")
def test_obstore_basic_operations_sync(storage_registry: StorageRegistry) -> None:
"""Test basic save, get_content, delete via backend and FileObject."""
remove_listeners()
backend = storage_registry.get_backend("local_test_store")
test_content = b"Hello Storage!"
file_path = "test_basic_sync.txt" # Relative path for the backend
# Create initial FileObject
obj = FileObject(backend=backend, filename="test_basic_sync.txt", to_filename=file_path)
# Save using backend
updated_obj = backend.save_object(obj, test_content)
# Assert FileObject updated
assert updated_obj is obj # Should update in-place
assert obj.path == file_path
assert obj.filename == "test_basic_sync.txt"
assert obj.etag is not None
assert obj.size == len(test_content)
assert obj.backend is backend
assert obj.protocol == "file" # Based on LocalFileSystem
# Retrieve content via FileObject method
retrieved_content = obj.get_content()
assert retrieved_content == test_content
# Delete via FileObject method
obj.delete()
# Verify deletion (expect FileNotFoundError or similar from backend)
with pytest.raises(FileNotFoundError):
backend.get_content(file_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_single_file_async_no_listener(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test saving and loading a model with a single StoredObject."""
remove_listeners()
file_content = b"SQLAlchemy Integration Test"
doc_name = "Integration Doc"
file_path = "sqlalchemy_single_async.bin"
# 1. Prepare FileObject and save via backend
initial_obj = FileObject(
backend="local_test_store",
filename="report.bin",
to_filename=file_path,
content_type="application/octet-stream",
)
updated_obj = await initial_obj.save_async(data=file_content)
# 2. Create and save model instance
doc = Document(name=doc_name, attachment=updated_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
assert doc.id is not None
assert doc.attachment is not None
assert isinstance(doc.attachment, FileObject)
assert doc.attachment.filename == "sqlalchemy_single_async.bin"
assert doc.attachment.path == file_path
assert doc.attachment.size == len(file_content) or doc.attachment.size is None
assert doc.attachment.content_type == "application/octet-stream"
assert doc.attachment.backend.key == "local_test_store"
# 3. Retrieve content via loaded FileObject
loaded_content = await doc.attachment.get_content_async()
assert loaded_content == file_content
@pytest.mark.xdist_group("file_object")
async def test_obstore_multiple_files_async_no_listener(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test saving and loading a model with multiple StoredObjects."""
remove_listeners()
backend = storage_registry.get_backend("local_test_store")
img1_content = b"img_data_1"
img2_content = b"img_data_2"
doc_name = "Multi Image Doc"
img1_path = "img1.jpg"
img2_path = "img2.png"
# 1. Prepare FileObjects and save via backend
obj1 = FileObject(backend=backend, filename="image1.jpg", to_filename=img1_path, content_type="image/jpeg")
obj1_updated = await obj1.save_async(img1_content)
obj2 = FileObject(backend=backend, filename="image2.png", to_filename=img2_path, content_type="image/png")
obj2_updated = await obj2.save_async(img2_content)
# 2. Create and save model instance with MutableList
img_list = MutableList[FileObject]([obj1_updated, obj2_updated])
doc = Document(name=doc_name, images=img_list)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
assert doc.id is not None
assert doc.images is not None
assert isinstance(doc.images, MutableList)
assert len(doc.images) == 2
# Verify loaded objects
loaded_obj1 = doc.images[0]
loaded_obj2 = doc.images[1]
assert isinstance(loaded_obj1, FileObject)
assert loaded_obj1.filename == "img1.jpg"
assert loaded_obj1.path == img1_path
assert loaded_obj1.size == len(img1_content) or loaded_obj1.size is None
assert loaded_obj1.backend and loaded_obj1.backend.driver == backend.driver
assert isinstance(loaded_obj2, FileObject)
assert loaded_obj2.filename == "img2.png"
assert loaded_obj2.path == img2_path
assert loaded_obj2.size == len(img2_content) or loaded_obj2.size is None
assert loaded_obj2.backend and loaded_obj2.backend.driver == backend.driver
# Verify content
assert await loaded_obj1.get_content_async() == img1_content
assert await loaded_obj2.get_content_async() == img2_content
@pytest.mark.xdist_group("file_object")
async def test_obstore_update_async_with_listener(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes old file when attribute is updated and session committed."""
# Set async context flag to enable async operations in the listener
set_async_context(True)
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
old_content = b"Old file content"
new_content = b"New file content"
old_path = "old_file_async.txt"
new_path = "new_file_async.txt"
# Save initial file and model
old_obj = FileObject(backend=backend, filename="old_file_async.txt", to_filename=old_path, content=old_content)
# Make sure file is saved to the backend
old_obj = await old_obj.save_async()
doc = Document(name="DocToUpdate", attachment=old_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify old file exists
assert await backend.get_content_async(old_path) == old_content
# Prepare new file
new_obj = FileObject(backend=backend, filename="new_file_async.txt", to_filename=new_path, content=new_content)
# Update the document with the new file
doc.attachment = new_obj
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify new file exists and attachment updated
assert await backend.get_content_async(new_path) == new_content
assert doc.attachment is not None and doc.attachment.path == new_path # pyright: ignore
# Verify the listener deleted the old file
with pytest.raises(FileNotFoundError):
await backend.get_content_async(old_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_delete_async_on_update_clear_with_listener(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes file when attribute is cleared.
Note that AsyncSession in SQLAlchemy 2.0 has limitations with event listeners.
We will manually handle cleanup of files to ensure proper functionality.
"""
# Set async context flag to enable async operations in the listener
set_async_context(True)
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
old_content = b"File to clear"
old_path = "clear_me_async.log"
# Save initial file and model
old_obj = FileObject(backend=backend, filename="clear_me_async.log", to_filename=old_path, content=old_content)
old_obj = await old_obj.save_async() # Make sure it's saved to the backend
doc = Document(name="DocToClear", attachment=old_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify old file exists
assert await backend.get_content_async(old_path) == old_content
# Clear the attachment
doc.attachment = None
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify attachment is None
assert doc.attachment is None
# Verify the listener deleted the file
with pytest.raises(FileNotFoundError):
await backend.get_content_async(old_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_delete_async_multiple_removed_with_listener(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes files removed from a multiple list.
Note that AsyncSession in SQLAlchemy 2.0 has limitations with event listeners.
MutableList tracking doesn't work properly with AsyncSession, so we use direct
assignment for updates instead of mutating the list in-place.
"""
# Set async context flag to enable async operations in the listener
set_async_context(True)
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
content1 = b"img1"
content2 = b"img2"
path1 = "img1_list_async.jpg"
path2 = "img2_list_async.png"
# Create file objects and save them
obj1 = FileObject(backend=backend, filename="img1_list_async.jpg", to_filename=path1, content=content1)
obj1 = await obj1.save_async()
obj2 = FileObject(backend=backend, filename="img2_list_async.png", to_filename=path2, content=content2)
obj2 = await obj2.save_async()
# Create and save model with both images
img_list = MutableList[FileObject]([obj1, obj2])
doc = Document(name="ImagesDoc", images=img_list)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify files exist
assert await backend.get_content_async(path1) == content1
assert await backend.get_content_async(path2) == content2
# Verify images are loaded
assert doc.images is not None
assert len(doc.images) == 2
# With AsyncSession, mutations to MutableList may not be tracked correctly.
# Instead of mutating the list in place, we'll create a new list with only obj2
doc.images = MutableList[FileObject]([obj2])
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify only one image remains
assert doc.images is not None
assert len(doc.images or []) == 1
assert doc.images[0].path == path2 # pyright: ignore
# Verify first file is deleted and second still exists
with pytest.raises(FileNotFoundError):
await backend.get_content_async(path1)
assert await backend.get_content_async(path2) == content2
@pytest.mark.xdist_group("file_object")
async def test_file_object_invalid_init(storage_registry: StorageRegistry) -> None:
"""Test FileObject initialization with invalid parameters."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content"
test_path = Path("test.txt")
# Test both content and source_path provided
with pytest.raises(ValueError, match="Cannot provide both 'source_content' and 'source_path'"):
FileObject(
backend=backend,
filename="test.txt",
content=test_content,
source_path=test_path,
)
@pytest.mark.xdist_group("file_object")
async def test_file_object_metadata_management(storage_registry: StorageRegistry) -> None:
"""Test FileObject metadata handling."""
backend = storage_registry.get_backend("local_test_store")
initial_metadata = {"category": "test", "tags": ["sample"]}
additional_metadata = {"priority": "high", "tags": ["important"]}
# Create FileObject with initial metadata
obj = FileObject(
backend=backend,
filename="test.txt",
metadata=initial_metadata,
)
assert obj.metadata == initial_metadata
# Update metadata
obj.update_metadata(additional_metadata)
expected_metadata = {
"category": "test",
"tags": ["important"], # New tags override old ones
"priority": "high",
}
assert obj.metadata == expected_metadata
@pytest.mark.xdist_group("file_object")
async def test_file_object_to_dict(storage_registry: StorageRegistry) -> None:
"""Test FileObject to_dict method."""
backend = storage_registry.get_backend("local_test_store")
obj = FileObject(
backend=backend,
filename="test.txt",
content_type="text/plain",
size=100,
last_modified=1234567890.0,
checksum="abc123",
etag="xyz789",
version_id="v1",
metadata={"category": "test"},
)
# Convert to dict
obj_dict = obj.to_dict()
assert obj_dict == {
"filename": "test.txt",
"content_type": "text/plain",
"size": 100,
"last_modified": 1234567890.0,
"checksum": "abc123",
"etag": "xyz789",
"version_id": "v1",
"metadata": {"category": "test"},
"backend": "local_test_store",
}
@pytest.mark.xdist_group("file_object")
async def test_obstore_local_sign_urls(storage_registry: StorageRegistry) -> None:
"""Test FileObject sign and sign_async methods."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content for signing"
file_path = "test_sign.txt"
# Create and save file
obj = FileObject(backend=backend, filename="test.txt", to_filename=file_path)
await obj.save_async(data=test_content)
# Test sign method
with pytest.raises(NotImplementedError, match=r"Error signing path test_sign.txt"):
_ = obj.sign(expires_in=3600)
# Test sign_async method
with pytest.raises(NotImplementedError, match=r"Error signing path test_sign.txt"):
_ = await obj.sign_async(expires_in=3600)
with pytest.raises(
NotImplementedError,
match=r"Error signing path test_sign.txt",
):
_ = obj.sign(for_upload=True)
with pytest.raises(
NotImplementedError,
match=r"Error signing path test_sign.txt",
):
_ = await obj.sign_async(for_upload=True)
@pytest.mark.xdist_group("file_object")
async def test_file_object_save_with_different_data_types(storage_registry: StorageRegistry) -> None:
"""Test FileObject save with different data types."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content"
file_path = "test_data_types.txt"
# Test with bytes
obj1 = FileObject(backend=backend, filename="test1.txt", to_filename=file_path, content=test_content)
obj1.save()
assert await obj1.get_content_async() == test_content
# Test with Path
import tempfile
with tempfile.NamedTemporaryFile(mode="wb", delete=False) as f:
f.write(test_content)
temp_path = Path(f.name)
obj2 = FileObject(backend=backend, filename="test2.txt", to_filename=file_path)
await obj2.save_async(data=temp_path)
assert await obj2.get_content_async() == test_content
assert obj2.get_content() == test_content
# Cleanup
temp_path.unlink()
@pytest.mark.xdist_group("file_object")
async def test_file_object_pending_data_property(storage_registry: StorageRegistry) -> None:
"""Test FileObject has_pending_data property."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content"
test_path = Path("test.txt")
# Test with content
obj1 = FileObject(backend=backend, filename="test1.txt", content=test_content)
assert obj1.has_pending_data
# Test with source_path
obj2 = FileObject(backend=backend, filename="test2.txt", source_path=test_path)
assert obj2.has_pending_data
# Test without pending data
obj3 = FileObject(backend=backend, filename="test3.txt")
assert not obj3.has_pending_data
@pytest.mark.xdist_group("file_object")
async def test_file_object_delete_methods(storage_registry: StorageRegistry) -> None:
"""Test FileObject delete and delete_async methods."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content to delete"
file_path = "test_delete.txt"
# Create and save file
obj = FileObject(backend=backend, filename="test.txt", to_filename=file_path)
await obj.save_async(data=test_content)
# Verify file exists
assert await backend.get_content_async(file_path) == test_content
# Test delete_async
await obj.delete_async()
with pytest.raises(FileNotFoundError):
await backend.get_content_async(file_path)
# Create and save file again
await obj.save_async(data=test_content)
assert await backend.get_content_async(file_path) == test_content
# Test delete
obj.delete()
with pytest.raises(FileNotFoundError):
await backend.get_content_async(file_path)
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_storage_registry_management(storage_registry: StorageRegistry) -> None:
"""Test StorageRegistry management methods."""
from obstore.store import MemoryStore
from advanced_alchemy.types.file_object.backends.obstore import ObstoreBackend
# Test registered_backends
initial_backends = storage_registry.registered_backends()
assert "local_test_store" in initial_backends
assert "memory" in initial_backends
# Test unregister_backend
storage_registry.unregister_backend("local_test_store")
assert "local_test_store" not in storage_registry.registered_backends()
with pytest.raises(ImproperConfigurationError):
storage_registry.get_backend("local_test_store")
# Test clear_backends
storage_registry.clear_backends()
assert not storage_registry.registered_backends()
# Test set_default_backend
storage_registry.set_default_backend("advanced_alchemy.types.file_object.backends.obstore.ObstoreBackend")
assert storage_registry.default_backend == "advanced_alchemy.types.file_object.backends.obstore.ObstoreBackend"
# Test register_backend with string value
storage_registry.register_backend("memory://", key="test_backend")
assert "test_backend" in storage_registry.registered_backends()
assert isinstance(storage_registry.get_backend("test_backend"), ObstoreBackend)
# Test register_backend with StorageBackend instance
test_backend = ObstoreBackend(fs=MemoryStore(), key="test_backend2")
storage_registry.register_backend(test_backend)
assert "test_backend2" in storage_registry.registered_backends()
assert storage_registry.get_backend("test_backend2") is test_backend
# Test error cases
with pytest.raises(ImproperConfigurationError, match="key is required when registering a string value"):
storage_registry.register_backend("memory://") # type: ignore[arg-type]
with pytest.raises(ImproperConfigurationError, match="key is not allowed when registering a StorageBackend"):
storage_registry.register_backend(test_backend, key="invalid_key") # type: ignore[arg-type]
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_storage_registry_error_handling(storage_registry: StorageRegistry) -> None:
"""Test StorageRegistry error handling."""
# Test get_backend with non-existent key
with pytest.raises(ImproperConfigurationError, match="No storage backend registered with key nonexistent"):
storage_registry.get_backend("nonexistent")
# Test unregister_backend with non-existent key
storage_registry.unregister_backend("nonexistent") # Should not raise an error
# Test set_default_backend with invalid backend
storage_registry.set_default_backend("invalid.module.path.Backend")
@pytest.mark.xdist_group("file_object")
async def test_fsspec_backend_basic_operations(storage_registry: StorageRegistry) -> None:
"""Test basic operations with FSSpec backend."""
try:
import fsspec
except ImportError:
pytest.skip("fsspec not installed")
# Create a local filesystem backend
fs = fsspec.filesystem("file")
backend = FSSpecBackend(fs=fs, key="fsspec_test")
test_content = b"Test content"
file_path = "test_fsspec.txt"
# Test save and get content
obj = FileObject(backend=backend, filename="test.txt", to_filename=file_path)
await obj.save_async(data=test_content)
assert await obj.get_content_async() == test_content
# Test delete
await obj.delete_async()
with pytest.raises(FileNotFoundError):
await obj.get_content_async()
@pytest.mark.xdist_group("file_object")
async def test_fsspec_backend_protocols(storage_registry: StorageRegistry) -> None:
"""Test FSSpec backend with different protocols."""
try:
import fsspec
except ImportError:
pytest.skip("fsspec not installed")
# Test local filesystem
fs_local = fsspec.filesystem("file")
backend_local = FSSpecBackend(fs=fs_local, key="fsspec_local")
assert backend_local.protocol == "file"
# Test memory filesystem
fs_memory = fsspec.filesystem("memory")
backend_memory = FSSpecBackend(fs=fs_memory, key="fsspec_memory")
assert backend_memory.protocol == "memory"
# Test with protocol string
backend_from_string = FSSpecBackend(fs="file", key="fsspec_string")
assert backend_from_string.protocol == "file"
@pytest.mark.xdist_group("file_object")
async def test_fsspec_backend_content_types(storage_registry: StorageRegistry) -> None:
"""Test FSSpec backend with different content types."""
try:
import fsspec
except ImportError:
pytest.skip("fsspec not installed")
fs = fsspec.filesystem("memory")
backend = FSSpecBackend(fs=fs, key="fsspec_content")
file_path = "test_content.txt"
# Test with bytes
content_bytes = b"Test bytes"
obj_bytes = FileObject(backend=backend, filename="test_bytes.txt", to_filename=file_path)
await obj_bytes.save_async(data=content_bytes)
assert await obj_bytes.get_content_async() == content_bytes
# Test with string
content_str = "Test string"
obj_str = FileObject(backend=backend, filename="test_str.txt", to_filename=file_path)
await obj_str.save_async(data=content_str.encode("utf-8"))
assert await obj_str.get_content_async() == content_str.encode("utf-8")
@pytest.mark.xdist_group("file_object")
async def test_fsspec_backend_multipart_upload(storage_registry: StorageRegistry) -> None:
"""Test FSSpec backend multipart upload."""
try:
import fsspec
except ImportError:
pytest.skip("fsspec not installed")
fs = fsspec.filesystem("memory")
backend = FSSpecBackend(fs=fs, key="fsspec_multipart")
file_path = "test_multipart.txt"
# Create large content for multipart upload
large_content = b"x" * (5 * 1024 * 1024 + 1) # 5MB + 1 byte
obj = FileObject(backend=backend, filename="test.txt", to_filename=file_path)
# Test with multipart upload
await obj.save_async(
data=large_content,
use_multipart=True,
chunk_size=1024 * 1024, # 1MB chunks
max_concurrency=4,
)
assert await obj.get_content_async() == large_content
@pytest.mark.xdist_group("file_object")
async def test_fsspec_backend_sign_urls(storage_registry: StorageRegistry, tmp_path: Path) -> None:
"""Test FSSpec backend URL signing."""
try:
import fsspec
except ImportError:
pytest.skip("fsspec not installed")
fs = fsspec.filesystem("file")
backend = FSSpecBackend(fs=fs, key="fsspec_sign", prefix=str(tmp_path))
file_path = "test_sign.txt"
# Create and save test file
test_content = b"Test content for signing"
obj = FileObject(backend=backend, filename="test.txt", to_filename=file_path)
await obj.save_async(data=test_content)
# Test sign method
with pytest.raises(NotImplementedError, match="Signing URLs not supported by file backend"):
_ = obj.sign(expires_in=3600)
# Test sign_async method
with pytest.raises(NotImplementedError, match="Signing URLs not supported by file backend"):
_ = await obj.sign_async(expires_in=3600)
# Test for_upload parameter
with pytest.raises(
NotImplementedError,
match=r"Generating signed URLs for upload is generally not supported by fsspec's generic sign method.",
):
_ = obj.sign(for_upload=True)
@pytest.mark.xdist_group("file_object")
def test_file_object_sync_save_and_get_content(storage_registry: StorageRegistry) -> None:
"""Test FileObject synchronous save and get_content methods."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test synchronous content"
file_path = "test_sync_save.txt"
# Create FileObject with content
obj = FileObject(backend=backend, filename="test_sync.txt", to_filename=file_path, content=test_content)
# Test synchronous save method
updated_obj = obj.save()
# Verify save worked correctly
assert updated_obj is obj # Should update in-place
assert obj.path == file_path
assert obj.size == len(test_content) or obj.size is None
# Test synchronous get_content method
retrieved_content = obj.get_content()
assert retrieved_content == test_content
# Clean up
obj.delete()
@pytest.mark.xdist_group("file_object")
def test_file_object_save_with_source_path(storage_registry: StorageRegistry, tmp_path: Path) -> None:
"""Test FileObject save with source_path."""
backend = storage_registry.get_backend("local_test_store")
test_content = b"Test content from file"
file_path = "test_source_path.txt"
# Create a temporary file
source_file = tmp_path / "source.txt"
source_file.write_bytes(test_content)
# Create FileObject with source_path
obj = FileObject(backend=backend, filename="test_source.txt", to_filename=file_path, source_path=source_file)
# Test save method with source_path
obj.save()
# Verify save worked correctly
retrieved_content = obj.get_content()
assert retrieved_content == test_content
# Clean up
obj.delete()
@pytest.mark.xdist_group("file_object")
def test_file_object_equality_and_hash(storage_registry: StorageRegistry) -> None:
"""Test FileObject __eq__ and __hash__ methods."""
backend = storage_registry.get_backend("local_test_store")
# Create two identical FileObjects
obj1 = FileObject(backend=backend, filename="test.txt", to_filename="same_path.txt")
obj2 = FileObject(backend=backend, filename="different.txt", to_filename="same_path.txt")
# They should be equal because they have the same path and backend
assert obj1 == obj2
assert hash(obj1) == hash(obj2)
# Create a different FileObject
obj3 = FileObject(backend=backend, filename="test.txt", to_filename="different_path.txt")
# They should not be equal because they have different paths
assert obj1 != obj3
assert hash(obj1) != hash(obj3)
# Compare with a non-FileObject
assert obj1 != "not a file object"
@pytest.mark.xdist_group("file_object")
def test_file_object_property_setters(storage_registry: StorageRegistry) -> None:
"""Test FileObject property setters."""
backend = storage_registry.get_backend("local_test_store")
obj = FileObject(backend=backend, filename="test.txt")
# Test size property
obj.size = 100
assert obj.size == 100
# Test last_modified property
timestamp = 1234567890.0
obj.last_modified = timestamp
assert obj.last_modified == timestamp
# Test checksum property
obj.checksum = "abc123"
assert obj.checksum == "abc123"
# Test etag property
obj.etag = "etag123"
assert obj.etag == "etag123"
# Test version_id property
obj.version_id = "v1"
assert obj.version_id == "v1"
# Test metadata property
new_metadata = {"key": "value"}
obj.metadata = new_metadata
assert obj.metadata == new_metadata
@pytest.mark.xdist_group("file_object")
def test_file_object_repr(storage_registry: StorageRegistry) -> None:
"""Test FileObject __repr__ method."""
backend = storage_registry.get_backend("local_test_store")
# Create a FileObject with all attributes set
obj = FileObject(
backend=backend,
filename="test.txt",
size=100,
content_type="text/plain",
last_modified=1234567890.0,
etag="etag123",
version_id="v1",
)
# Test __repr__ method
repr_str = repr(obj)
assert "FileObject" in repr_str
assert "filename=test.txt" in repr_str
assert "backend=local_test_store" in repr_str
assert "size=100" in repr_str
assert "content_type=text/plain" in repr_str
assert "etag=etag123" in repr_str
assert "last_modified=1234567890.0" in repr_str
assert "version_id=v1" in repr_str
@pytest.mark.xdist_group("file_object")
def test_file_object_content_type_guessing(storage_registry: StorageRegistry) -> None:
"""Test content_type guessing from filename."""
backend = storage_registry.get_backend("local_test_store")
# Test common file types
file_types = {
"test.txt": "text/plain",
"image.jpg": "image/jpeg",
"doc.pdf": "application/pdf",
"data.json": "application/json",
"unknown": "application/octet-stream",
}
for filename, expected_type in file_types.items():
obj = FileObject(backend=backend, filename=filename)
assert obj.content_type == expected_type
@pytest.mark.xdist_group("file_object")
def test_file_object_save_no_data(storage_registry: StorageRegistry) -> None:
"""Test save method with no data."""
backend = storage_registry.get_backend("local_test_store")
# Create a FileObject with no content or source_path
obj = FileObject(backend=backend, filename="test.txt")
# Saving with no data should raise a TypeError
with pytest.raises(TypeError, match="No data provided and no pending content/path found to save."):
obj.save()
@pytest.mark.xdist_group("file_object")
async def test_file_object_save_async_no_data(storage_registry: StorageRegistry) -> None:
"""Test save_async method with no data."""
backend = storage_registry.get_backend("local_test_store")
# Create a FileObject with no content or source_path
obj = FileObject(backend=backend, filename="test.txt")
# Saving with no data should raise a TypeError
with pytest.raises(TypeError, match="No data provided and no pending content/path found to save."):
await obj.save_async()
@pytest.mark.xdist_group("file_object")
def test_obstore_backend_sqlalchemy_single_file_persist_sync(
session: Session, storage_registry: StorageRegistry
) -> None:
"""Test saving and loading a model with a single StoredObject using synchronous SQLAlchemy session."""
remove_listeners()
file_content = b"SQLAlchemy Sync Integration Test"
doc_name = "Sync Integration Doc"
file_path = "sqlalchemy_single_sync.bin"
# 1. Prepare FileObject and save via backend
initial_obj = FileObject(
backend="local_test_store",
filename="report.bin",
to_filename=file_path,
content_type="application/octet-stream",
)
updated_obj = initial_obj.save(data=file_content)
# 2. Create and save model instance
doc = Document(name=doc_name, attachment=updated_obj)
session.add(doc)
session.commit()
session.refresh(doc)
assert doc.id is not None
assert doc.attachment is not None
assert isinstance(doc.attachment, FileObject)
assert doc.attachment.filename == "sqlalchemy_single_sync.bin"
assert doc.attachment.path == file_path
assert doc.attachment.size == len(file_content) or doc.attachment.size is None
assert doc.attachment.content_type == "application/octet-stream"
assert doc.attachment.backend.key == "local_test_store"
# 3. Retrieve content via loaded FileObject
loaded_content = doc.attachment.get_content()
assert loaded_content == file_content
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_listener_sqlalchemy_single_file_persist_async(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test saving and loading a model with a single StoredObject using synchronous SQLAlchemy session."""
setup_file_object_listeners()
set_async_context(True)
file_content = b"SQLAlchemy Async Integration Test"
doc_name = "Sync Integration Doc"
file_path = "sqlalchemy_single_async.bin"
# 1. Prepare FileObject and save via backend
initial_obj = FileObject(
backend="local_test_store",
filename="report.bin",
to_filename=file_path,
content_type="application/octet-stream",
content=file_content,
)
# 2. Create and save model instance
doc = Document(name=doc_name, attachment=initial_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
assert doc.id is not None
assert doc.attachment is not None
assert isinstance(doc.attachment, FileObject)
assert doc.attachment.filename == "sqlalchemy_single_async.bin"
assert doc.attachment.path == file_path
assert doc.attachment.size == len(file_content) or doc.attachment.size is None
assert doc.attachment.content_type == "application/octet-stream"
assert doc.attachment.backend.key == "local_test_store"
# 3. Retrieve content via loaded FileObject
loaded_content = doc.attachment.get_content()
assert loaded_content == file_content
@pytest.mark.xdist_group("file_object")
def test_obstore_backend_sqlalchemy_multiple_files_persist_sync(
session: Session, storage_registry: StorageRegistry
) -> None:
"""Test saving and loading a model with multiple StoredObjects using synchronous SQLAlchemy session."""
remove_listeners()
backend = storage_registry.get_backend("local_test_store")
img1_content = b"img_data_1_sync"
img2_content = b"img_data_2_sync"
doc_name = "Multi Image Doc Sync"
img1_path = "img1_list_sync.jpg"
img2_path = "img2_list_sync.png"
# 1. Prepare FileObjects and save via backend
obj1 = FileObject(
backend=backend, filename="image1_list_sync.jpg", to_filename=img1_path, content_type="image/jpeg"
)
obj1_updated = obj1.save(img1_content)
obj2 = FileObject(backend=backend, filename="image2_list_sync.png", to_filename=img2_path, content_type="image/png")
obj2_updated = obj2.save(img2_content)
# 2. Create and save model instance with MutableList
img_list = MutableList[FileObject]([obj1_updated, obj2_updated])
doc = Document(name=doc_name, images=img_list)
session.add(doc)
session.commit()
session.refresh(doc)
assert doc.id is not None
assert doc.images is not None
assert isinstance(doc.images, MutableList)
assert len(doc.images) == 2
# Verify loaded objects
loaded_obj1 = doc.images[0]
loaded_obj2 = doc.images[1]
assert isinstance(loaded_obj1, FileObject)
assert loaded_obj1.filename == "img1_list_sync.jpg"
assert loaded_obj1.path == img1_path
assert loaded_obj1.size == len(img1_content) or loaded_obj1.size is None
assert loaded_obj1.backend and loaded_obj1.backend.driver == backend.driver
assert isinstance(loaded_obj2, FileObject)
assert loaded_obj2.filename == "img2_list_sync.png"
assert loaded_obj2.path == img2_path
assert loaded_obj2.size == len(img2_content) or loaded_obj2.size is None
assert loaded_obj2.backend and loaded_obj2.backend.driver == backend.driver
# Verify content
assert loaded_obj1.get_content() == img1_content
assert loaded_obj2.get_content() == img2_content
@pytest.mark.xdist_group("file_object")
def test_obstore_backend_listener_delete_on_update_clear_sync(
session: Session, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes old file when attribute is cleared using synchronous SQLAlchemy session."""
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
old_content = b"File to clear sync"
old_path = "clear_me_sync.log"
# Save initial file and model
old_obj = FileObject(backend=backend, filename="clear.log", to_filename=old_path, content=old_content)
doc = Document(name="DocToClearSync", attachment=old_obj)
session.add(doc)
session.commit()
session.refresh(doc)
# Verify old file exists
assert backend.get_content(old_path) == old_content
# Clear the attachment
doc.attachment = None
session.add(doc)
session.commit()
session.refresh(doc)
# Verify attachment is None
assert doc.attachment is None
# Verify the listener deleted the file from storage
with pytest.raises(FileNotFoundError):
backend.get_content(old_path)
@pytest.mark.flaky(reruns=5)
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_listener_delete_on_update_clear_async(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes old file when attribute is cleared using asynchronous SQLAlchemy session."""
setup_file_object_listeners()
set_async_context(True)
backend = storage_registry.get_backend("local_test_store")
old_content = b"File to clear sync"
old_path = "clear_me_sync.log"
# Save initial file and model
old_obj = FileObject(backend=backend, filename="clear.log", to_filename=old_path, content=old_content)
doc = Document(name="DocToClearSync", attachment=old_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify old file exists
assert await backend.get_content_async(old_path) == old_content
# Clear the attachment
doc.attachment = None
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify attachment is None
assert doc.attachment is None
# Verify the listener deleted the file from storage
with pytest.raises(FileNotFoundError):
await backend.get_content_async(old_path)
@pytest.mark.flaky(reruns=5)
@pytest.mark.xdist_group("file_object")
def test_obstore_backend_listener_update_file_object_sync(session: Session, storage_registry: StorageRegistry) -> None:
"""Test listener deletes old file when attribute is updated and session committed using synchronous SQLAlchemy session."""
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
old_content = b"Old file content sync"
new_content = b"New file content sync"
old_path = "old_file_sync_update.txt"
new_path = "new_file_sync_update.txt"
# Save initial file and model
old_obj = FileObject(
backend=backend, filename="old_file_sync_update.txt", to_filename=old_path, content=old_content
)
doc = Document(name="DocToUpdateSync", attachment=old_obj)
session.add(doc)
session.commit()
session.refresh(doc)
# Verify old file exists
assert backend.get_content(old_path) == old_content
# Update the document's attachment (inline creation)
new_obj = FileObject(
backend=backend, filename="new_file_sync_update.txt", to_filename=new_path, content=new_content
)
doc.attachment = new_obj
session.add(doc) # Add again as it's modified
session.commit() # Listener should save new_obj and queue deletion of old_obj
session.refresh(doc)
# Verify new file exists and attachment updated
assert backend.get_content(new_path) == new_content
assert doc.attachment is not None and doc.attachment.path == new_path # pyright: ignore
# Verify the listener deleted the old file from storage
with pytest.raises(FileNotFoundError):
backend.get_content(old_path)
@pytest.mark.flaky(reruns=5)
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_listener_update_file_object_async(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes old file when attribute is updated and session committed using asynchronous SQLAlchemy session."""
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
old_content = b"Old file content sync"
new_content = b"New file content sync"
old_path = "old_file_async_update.txt"
new_path = "new_file_async_update.txt"
# Save initial file and model
old_obj = FileObject(
backend=backend, filename="old_file_async_update.txt", to_filename=old_path, content=old_content
)
doc = Document(name="DocToUpdateSync", attachment=old_obj)
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify old file exists
assert backend.get_content(old_path) == old_content
# Update the document's attachment (inline creation)
new_obj = FileObject(
backend=backend, filename="new_file_async_update.txt", to_filename=new_path, content=new_content
)
doc.attachment = new_obj
async_session.add(doc) # Add again as it's modified
await async_session.commit() # Listener should save new_obj and queue deletion of old_obj
await async_session.refresh(doc)
assert backend.get_content(new_path) == new_content
assert doc.attachment is not None and doc.attachment.path == new_path # pyright: ignore
# Verify the listener deleted the old file from storage
with pytest.raises(FileNotFoundError):
backend.get_content(old_path)
@pytest.mark.flaky(reruns=5)
@pytest.mark.xdist_group("file_object")
def test_obstore_backend_listener_delete_multiple_removed_sync(
session: Session, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes files removed from a multiple list using synchronous SQLAlchemy session."""
set_async_context(False)
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
content1 = b"img1_sync_multi"
content2 = b"img2_sync_multi"
path1 = "multi_del_1_sync.dat"
path2 = "multi_del_2_sync.dat"
# Save files
obj1 = FileObject(backend=backend, filename=path1, content=content1)
obj2 = FileObject(backend=backend, filename=path2, content=content2)
# Create model with initial list
doc = Document(name="MultiDeleteSyncTest", images=[obj1, obj2])
session.add(doc)
session.commit()
session.refresh(doc)
# Verify all files exist
assert backend.get_content(path1) == content1
assert backend.get_content(path2) == content2
# Remove items from the list (triggers MutableList tracking)
assert doc.images is not None
current_images = list(doc.images) # Create standard list copy
removed_item = current_images.pop(1) # Mutate copy
assert removed_item.path == obj2.path
del current_images[0] # Mutate copy
assert len(current_images) == 0
doc.images = MutableList(current_images) # Wrap in MutableList before reassignment
session.add(doc)
# Commit the session to trigger listener
session.commit()
session.refresh(doc)
assert doc.images == []
# Verify the listener deleted the files
with pytest.raises(FileNotFoundError):
backend.get_content(path1)
with pytest.raises(FileNotFoundError):
backend.get_content(path2)
@pytest.mark.flaky(reruns=5)
@pytest.mark.xdist_group("file_object")
async def test_obstore_backend_listener_delete_multiple_removed_async(
async_session: AsyncSession, storage_registry: StorageRegistry
) -> None:
"""Test listener deletes files removed from a multiple list using asynchronous SQLAlchemy session."""
set_async_context(True)
setup_file_object_listeners()
backend = storage_registry.get_backend("local_test_store")
content1 = b"img1_async_multi"
content2 = b"img2_async_multi"
path1 = "multi_del_1_async.dat"
path2 = "multi_del_2_async.dat"
# Save files
obj1 = FileObject(backend=backend, filename=path1, content=content1)
obj2 = FileObject(backend=backend, filename=path2, content=content2)
# Create model with initial list
doc = Document(name="MultiDeleteAsyncTest", images=[obj1, obj2])
async_session.add(doc)
await async_session.commit()
await async_session.refresh(doc)
# Verify all files exist
assert await backend.get_content_async(path1) == content1
assert await backend.get_content_async(path2) == content2
# Remove items from the list (triggers MutableList tracking)
assert doc.images is not None
current_images = list(doc.images) # Create standard list copy
removed_item = current_images.pop(1) # Mutate copy
assert removed_item.path == obj2.path
del current_images[0] # Mutate copy
assert len(current_images) == 0
doc.images = MutableList(current_images) # Wrap in MutableList before reassignment
# Commit the session to trigger listener
await async_session.commit()
await async_session.refresh(doc)
# Verify the listener deleted the files
with pytest.raises(FileNotFoundError):
await backend.get_content_async(path1)
with pytest.raises(FileNotFoundError):
await backend.get_content_async(path2)
|