|
14 | 14 |
|
15 | 15 | """Tests for the artifact service.""" |
16 | 16 |
|
| 17 | +from datetime import datetime |
17 | 18 | import enum |
18 | 19 | from typing import Optional |
19 | 20 | from typing import Union |
20 | 21 | from unittest import mock |
| 22 | +from unittest.mock import patch |
21 | 23 |
|
| 24 | +from google.adk.artifacts.base_artifact_service import ArtifactVersion |
22 | 25 | from google.adk.artifacts.gcs_artifact_service import GcsArtifactService |
23 | 26 | from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService |
24 | 27 | from google.genai import types |
25 | 28 | import pytest |
26 | 29 |
|
27 | 30 | Enum = enum.Enum |
28 | 31 |
|
| 32 | +# Define a fixed datetime object to be returned by datetime.now() |
| 33 | +FIXED_DATETIME = datetime(2025, 1, 1, 12, 0, 0) |
| 34 | + |
29 | 35 |
|
30 | 36 | class ArtifactServiceType(Enum): |
31 | 37 | IN_MEMORY = "IN_MEMORY" |
@@ -195,6 +201,15 @@ async def test_save_load_delete(service_type): |
195 | 201 | == artifact |
196 | 202 | ) |
197 | 203 |
|
| 204 | + # Attempt to load a version that doesn't exist |
| 205 | + assert not await artifact_service.load_artifact( |
| 206 | + app_name=app_name, |
| 207 | + user_id=user_id, |
| 208 | + session_id=session_id, |
| 209 | + filename=filename, |
| 210 | + version=3, |
| 211 | + ) |
| 212 | + |
198 | 213 | await artifact_service.delete_artifact( |
199 | 214 | app_name=app_name, |
200 | 215 | user_id=user_id, |
@@ -322,3 +337,171 @@ async def test_list_keys_preserves_user_prefix(): |
322 | 337 | # Should contain prefixed names and session file |
323 | 338 | expected_keys = ["user:document.pdf", "user:image.png", "session_file.txt"] |
324 | 339 | assert sorted(artifact_keys) == sorted(expected_keys) |
| 340 | + |
| 341 | + |
| 342 | +@pytest.mark.asyncio |
| 343 | +async def test_list_artifact_versions_and_get_artifact_version(): |
| 344 | + """Tests listing artifact versions and getting a specific version.""" |
| 345 | + artifact_service = InMemoryArtifactService() |
| 346 | + app_name = "app0" |
| 347 | + user_id = "user0" |
| 348 | + session_id = "123" |
| 349 | + filename = "filename" |
| 350 | + versions = [ |
| 351 | + types.Part.from_bytes( |
| 352 | + data=i.to_bytes(2, byteorder="big"), mime_type="text/plain" |
| 353 | + ) |
| 354 | + for i in range(4) |
| 355 | + ] |
| 356 | + |
| 357 | + with patch( |
| 358 | + "google.adk.artifacts.base_artifact_service.datetime" |
| 359 | + ) as mock_datetime: |
| 360 | + mock_datetime.now.return_value = FIXED_DATETIME |
| 361 | + |
| 362 | + for i in range(4): |
| 363 | + await artifact_service.save_artifact( |
| 364 | + app_name=app_name, |
| 365 | + user_id=user_id, |
| 366 | + session_id=session_id, |
| 367 | + filename=filename, |
| 368 | + artifact=versions[i], |
| 369 | + custom_metadata={"key": "value" + str(i)}, |
| 370 | + ) |
| 371 | + |
| 372 | + artifact_versions = await artifact_service.list_artifact_versions( |
| 373 | + app_name=app_name, |
| 374 | + user_id=user_id, |
| 375 | + session_id=session_id, |
| 376 | + filename=filename, |
| 377 | + ) |
| 378 | + |
| 379 | + expected_artifact_versions = [ |
| 380 | + ArtifactVersion( |
| 381 | + version=i, |
| 382 | + canonical_uri=( |
| 383 | + f"memory://apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{filename}/versions/{i}" |
| 384 | + ), |
| 385 | + custom_metadata={"key": "value" + str(i)}, |
| 386 | + mime_type="text/plain", |
| 387 | + create_time=FIXED_DATETIME.timestamp(), |
| 388 | + ) |
| 389 | + for i in range(4) |
| 390 | + ] |
| 391 | + assert artifact_versions == expected_artifact_versions |
| 392 | + |
| 393 | + # Get latest artifact version when version is not specified |
| 394 | + assert ( |
| 395 | + await artifact_service.get_artifact_version( |
| 396 | + app_name=app_name, |
| 397 | + user_id=user_id, |
| 398 | + session_id=session_id, |
| 399 | + filename=filename, |
| 400 | + ) |
| 401 | + == expected_artifact_versions[-1] |
| 402 | + ) |
| 403 | + |
| 404 | + # Get artifact version by version number |
| 405 | + assert ( |
| 406 | + await artifact_service.get_artifact_version( |
| 407 | + app_name=app_name, |
| 408 | + user_id=user_id, |
| 409 | + session_id=session_id, |
| 410 | + filename=filename, |
| 411 | + version=2, |
| 412 | + ) |
| 413 | + == expected_artifact_versions[2] |
| 414 | + ) |
| 415 | + |
| 416 | + |
| 417 | +@pytest.mark.asyncio |
| 418 | +async def test_list_artifact_versions_with_user_prefix(): |
| 419 | + """Tests listing artifact versions with user prefix.""" |
| 420 | + artifact_service = InMemoryArtifactService() |
| 421 | + app_name = "app0" |
| 422 | + user_id = "user0" |
| 423 | + session_id = "123" |
| 424 | + user_scoped_filename = "user:document.pdf" |
| 425 | + versions = [ |
| 426 | + types.Part.from_bytes( |
| 427 | + data=i.to_bytes(2, byteorder="big"), mime_type="text/plain" |
| 428 | + ) |
| 429 | + for i in range(4) |
| 430 | + ] |
| 431 | + |
| 432 | + with patch( |
| 433 | + "google.adk.artifacts.base_artifact_service.datetime" |
| 434 | + ) as mock_datetime: |
| 435 | + mock_datetime.now.return_value = FIXED_DATETIME |
| 436 | + |
| 437 | + for i in range(4): |
| 438 | + # Save artifacts with "user:" prefix (cross-session artifacts) |
| 439 | + await artifact_service.save_artifact( |
| 440 | + app_name=app_name, |
| 441 | + user_id=user_id, |
| 442 | + session_id=session_id, |
| 443 | + filename=user_scoped_filename, |
| 444 | + artifact=versions[i], |
| 445 | + custom_metadata={"key": "value" + str(i)}, |
| 446 | + ) |
| 447 | + |
| 448 | + artifact_versions = await artifact_service.list_artifact_versions( |
| 449 | + app_name=app_name, |
| 450 | + user_id=user_id, |
| 451 | + session_id=session_id, |
| 452 | + filename=user_scoped_filename, |
| 453 | + ) |
| 454 | + |
| 455 | + expected_artifact_versions = [ |
| 456 | + ArtifactVersion( |
| 457 | + version=i, |
| 458 | + canonical_uri=( |
| 459 | + f"memory://apps/{app_name}/users/{user_id}/artifacts/{user_scoped_filename}/versions/{i}" |
| 460 | + ), |
| 461 | + custom_metadata={"key": "value" + str(i)}, |
| 462 | + mime_type="text/plain", |
| 463 | + create_time=FIXED_DATETIME.timestamp(), |
| 464 | + ) |
| 465 | + for i in range(4) |
| 466 | + ] |
| 467 | + assert artifact_versions == expected_artifact_versions |
| 468 | + |
| 469 | + |
| 470 | +@pytest.mark.asyncio |
| 471 | +async def test_get_artifact_version_artifact_does_not_exist(): |
| 472 | + """Tests getting an artifact version when artifact does not exist.""" |
| 473 | + artifact_service = InMemoryArtifactService() |
| 474 | + assert not await artifact_service.get_artifact_version( |
| 475 | + app_name="test_app", |
| 476 | + user_id="test_user", |
| 477 | + session_id="session_id", |
| 478 | + filename="filename", |
| 479 | + ) |
| 480 | + |
| 481 | + |
| 482 | +@pytest.mark.asyncio |
| 483 | +async def test_get_artifact_version_out_of_index(): |
| 484 | + """Tests loading an artifact with an out-of-index version.""" |
| 485 | + artifact_service = InMemoryArtifactService() |
| 486 | + app_name = "app0" |
| 487 | + user_id = "user0" |
| 488 | + session_id = "123" |
| 489 | + filename = "filename" |
| 490 | + artifact = types.Part.from_bytes(data=b"test_data", mime_type="text/plain") |
| 491 | + |
| 492 | + await artifact_service.save_artifact( |
| 493 | + app_name=app_name, |
| 494 | + user_id=user_id, |
| 495 | + session_id=session_id, |
| 496 | + filename=filename, |
| 497 | + artifact=artifact, |
| 498 | + ) |
| 499 | + |
| 500 | + # Attempt to get a version that doesn't exist |
| 501 | + assert not await artifact_service.get_artifact_version( |
| 502 | + app_name=app_name, |
| 503 | + user_id=user_id, |
| 504 | + session_id=session_id, |
| 505 | + filename=filename, |
| 506 | + version=3, |
| 507 | + ) |
0 commit comments