11# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
22# SPDX-License-Identifier: Apache-2.0
33
4- """
5- Scenario 2 – Collection Lifecycle
6-
7- Follows the Go reference by showing both an inline lifecycle walkthrough and a helper-based scenario.
8- """
94from __future__ import annotations
105
116import os
127import time
13-
14- import pytest
8+ import json
159
1610from vikingdb import IAM
1711from vikingdb .vector import (
2317 VikingVector ,
2418)
2519
26- from .guide_helpers import (
27- Clients ,
28- assign_chapter_ids_via_search ,
29- build_clients ,
30- build_request_options ,
31- build_story_chapters ,
32- chapters_to_upsert ,
33- cleanup_chapters ,
34- load_config ,
35- new_session_tag ,
36- )
37-
3820
39- def test_snippet_collection_lifecycle () -> None :
40- """
41- Inline lifecycle flow mirroring the Go snippet:
42- 1. Upsert a chapter.
43- 2. Search to hydrate the chapter ID.
44- 3. Update scalar fields.
45- 4. Fetch to verify.
46- 5. Delete the record.
47- """
21+ def main () -> None :
22+ """Inline lifecycle flow: upsert → search → update → fetch → delete."""
4823 auth = IAM (
4924 ak = os .environ ["VIKINGDB_AK" ],
5025 sk = os .environ ["VIKINGDB_SK" ],
@@ -71,6 +46,7 @@ def test_snippet_collection_lifecycle() -> None:
7146 }
7247 upsert_resp = collection_client .upsert (UpsertDataRequest (data = [chapter ]))
7348 print (f"Upsert request_id={ upsert_resp .request_id } " )
49+ print (upsert_resp .model_dump_json (indent = 2 , by_alias = True ) if hasattr (upsert_resp , "model_dump_json" ) else json .dumps (upsert_resp .model_dump (by_alias = True , mode = "json" ), ensure_ascii = False , indent = 2 , sort_keys = True ))
7450
7551 time .sleep (2 )
7652
@@ -81,6 +57,7 @@ def test_snippet_collection_lifecycle() -> None:
8157 output_fields = ["title" , "score" ],
8258 )
8359 search_resp = index_client .search_by_multi_modal (search_req )
60+ print (search_resp .model_dump_json (indent = 2 , by_alias = True ) if hasattr (search_resp , "model_dump_json" ) else json .dumps (search_resp .model_dump (by_alias = True , mode = "json" ), ensure_ascii = False , indent = 2 , sort_keys = True ))
8461 if not search_resp .result or not search_resp .result .data :
8562 print ("SearchByMultiModal returned no hits" )
8663 return
@@ -94,59 +71,19 @@ def test_snippet_collection_lifecycle() -> None:
9471 UpdateDataRequest (data = [{"__AUTO_ID__" : chapter_id , "score" : new_score }])
9572 )
9673 print (f"Update request_id={ update_resp .request_id } " )
74+ print (update_resp .model_dump_json (indent = 2 , by_alias = True ) if hasattr (update_resp , "model_dump_json" ) else json .dumps (update_resp .model_dump (by_alias = True , mode = "json" ), ensure_ascii = False , indent = 2 , sort_keys = True ))
9775
9876 fetch_resp = collection_client .fetch (FetchDataInCollectionRequest (ids = [chapter_id ]))
77+ print (fetch_resp .model_dump_json (indent = 2 , by_alias = True ) if hasattr (fetch_resp , "model_dump_json" ) else json .dumps (fetch_resp .model_dump (by_alias = True , mode = "json" ), ensure_ascii = False , indent = 2 , sort_keys = True ))
9978 if fetch_resp .result and fetch_resp .result .items :
10079 fetched = fetch_resp .result .items [0 ].fields .get ("score" )
10180 fetched_score = float (fetched ) if fetched is not None else None
10281 print (f"Fetch request_id={ fetch_resp .request_id } score={ fetched_score } " )
10382
10483 delete_resp = collection_client .delete (DeleteDataRequest (ids = [chapter_id ]))
10584 print (f"Delete request_id={ delete_resp .request_id } " )
85+ print (delete_resp .model_dump_json (indent = 2 , by_alias = True ) if hasattr (delete_resp , "model_dump_json" ) else json .dumps (delete_resp .model_dump (by_alias = True , mode = "json" ), ensure_ascii = False , indent = 2 , sort_keys = True ))
10686
10787
108- @pytest .fixture (scope = "module" )
109- def collection_clients () -> Clients :
110- return build_clients (load_config ())
111-
112-
113- def test_scenario_collection_lifecycle (collection_clients : Clients ) -> None :
114- session_tag = new_session_tag ("collection-lifecycle" )
115- request_options = build_request_options (session_tag )
116- base_paragraph = int (time .time ()) % 1_000_000
117- chapters = build_story_chapters (session_tag , base_paragraph )
118-
119- try :
120- for payload in chapters_to_upsert (chapters ):
121- response = collection_clients .collection .upsert (
122- UpsertDataRequest (data = [payload ]),
123- request_options = request_options ,
124- )
125- assert response .request_id
126-
127- assign_chapter_ids_via_search (
128- collection_clients .index ,
129- chapters ,
130- output_fields = ["title" , "paragraph" , "score" ],
131- request_options = request_options ,
132- )
133-
134- target = next (ch for ch in chapters if ch .key == "retrieval-lab" )
135- assert target .chapter_id is not None
136-
137- new_score = target .score + 4.25
138- update_resp = collection_clients .collection .update (
139- UpdateDataRequest (data = [{"__AUTO_ID__" : target .chapter_id , "score" : new_score }]),
140- request_options = request_options ,
141- )
142- assert update_resp .request_id
143-
144- fetch_resp = collection_clients .collection .fetch (
145- FetchDataInCollectionRequest (ids = [target .chapter_id ]),
146- request_options = request_options ,
147- )
148- assert fetch_resp .result and fetch_resp .result .items
149- fetched_score = fetch_resp .result .items [0 ].fields .get ("score" )
150- assert fetched_score == pytest .approx (new_score )
151- finally :
152- cleanup_chapters (collection_clients .collection , chapters , request_options = request_options )
88+ if __name__ == "__main__" :
89+ main ()
0 commit comments