|
16 | 16 | #
|
17 | 17 |
|
18 | 18 |
|
| 19 | +from contextlib import redirect_stdout |
19 | 20 | import datetime
|
20 |
| -import pytest |
| 21 | +import io |
| 22 | +import json |
21 | 23 | import mock
|
| 24 | +import pytest |
22 | 25 | from vertexai.preview import caching
|
23 | 26 | from google.cloud.aiplatform import initializer
|
24 | 27 | import vertexai
|
@@ -50,6 +53,33 @@ def create_cached_content(self, request):
|
50 | 53 | response = GapicCachedContent(
|
51 | 54 | name=f"{request.parent}/cachedContents/{_CREATED_CONTENT_ID}",
|
52 | 55 | model=f"{request.cached_content.model}",
|
| 56 | + create_time=datetime.datetime( |
| 57 | + year=2024, |
| 58 | + month=2, |
| 59 | + day=1, |
| 60 | + hour=1, |
| 61 | + minute=1, |
| 62 | + second=1, |
| 63 | + tzinfo=datetime.timezone.utc, |
| 64 | + ), |
| 65 | + update_time=datetime.datetime( |
| 66 | + year=2024, |
| 67 | + month=2, |
| 68 | + day=1, |
| 69 | + hour=1, |
| 70 | + minute=1, |
| 71 | + second=1, |
| 72 | + tzinfo=datetime.timezone.utc, |
| 73 | + ), |
| 74 | + expire_time=datetime.datetime( |
| 75 | + year=2024, |
| 76 | + month=2, |
| 77 | + day=1, |
| 78 | + hour=2, |
| 79 | + minute=1, |
| 80 | + second=1, |
| 81 | + tzinfo=datetime.timezone.utc, |
| 82 | + ), |
53 | 83 | )
|
54 | 84 | return response
|
55 | 85 |
|
@@ -207,3 +237,32 @@ def test_list(self, mock_list_cached_contents):
|
207 | 237 | for i, cached_content in enumerate(cached_contents):
|
208 | 238 | assert cached_content.name == f"cached_content{i + 1}_from_list_request"
|
209 | 239 | assert cached_content.model_name == f"model-name{i + 1}"
|
| 240 | + |
| 241 | + def test_print_a_cached_content( |
| 242 | + self, mock_create_cached_content, mock_get_cached_content |
| 243 | + ): |
| 244 | + cached_content = caching.CachedContent.create( |
| 245 | + model_name="model-name", |
| 246 | + system_instruction="Please answer my questions with cool", |
| 247 | + tools=[], |
| 248 | + tool_config=GapicToolConfig(), |
| 249 | + contents=["user content"], |
| 250 | + ttl=datetime.timedelta(days=1), |
| 251 | + ) |
| 252 | + f = io.StringIO() |
| 253 | + with redirect_stdout(f): |
| 254 | + print(cached_content) |
| 255 | + output = f.getvalue() |
| 256 | + assert ( |
| 257 | + json.dumps( |
| 258 | + { |
| 259 | + "name": f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/cachedContents/{_CREATED_CONTENT_ID}", |
| 260 | + "model": "projects/test-project/locations/us-central1/publishers/google/models/model-name", |
| 261 | + "createTime": "2024-02-01T01:01:01Z", |
| 262 | + "updateTime": "2024-02-01T01:01:01Z", |
| 263 | + "expireTime": "2024-02-01T02:01:01Z", |
| 264 | + }, |
| 265 | + indent=2, |
| 266 | + ) |
| 267 | + in output |
| 268 | + ) |
0 commit comments