-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathtest_rag_data.py
More file actions
1318 lines (1141 loc) · 53.3 KB
/
test_rag_data.py
File metadata and controls
1318 lines (1141 loc) · 53.3 KB
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
# -*- coding: utf-8 -*-
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import importlib
from google.api_core import operation as ga_operation
from vertexai import rag
from vertexai.rag.utils._gapic_utils import (
prepare_import_files_request,
set_embedding_model_config,
)
from vertexai.rag.utils.resources import (
ChunkingConfig,
TransformationConfig,
)
from google.cloud.aiplatform_v1 import (
VertexRagDataServiceAsyncClient,
VertexRagDataServiceClient,
ListRagCorporaResponse,
ListRagFilesResponse,
)
from google.cloud import aiplatform
from unittest.mock import patch
from unittest import mock
import pytest
import test_rag_constants
@pytest.fixture
def create_rag_corpus_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS
)
create_rag_corpus_mock.return_value = create_rag_corpus_lro_mock
yield create_rag_corpus_mock
@pytest.fixture
def create_rag_corpus_mock_cmek():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_cmek:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_CMEK_RAG_CORPUS
)
create_rag_corpus_mock_cmek.return_value = create_rag_corpus_lro_mock
yield create_rag_corpus_mock_cmek
@pytest.fixture
def create_rag_corpus_mock_vertex_vector_search():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_vertex_vector_search:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_VERTEX_VECTOR_SEARCH
)
create_rag_corpus_mock_vertex_vector_search.return_value = (
create_rag_corpus_lro_mock
)
yield create_rag_corpus_mock_vertex_vector_search
@pytest.fixture
def create_rag_corpus_mock_pinecone():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_pinecone:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_PINECONE
)
create_rag_corpus_mock_pinecone.return_value = create_rag_corpus_lro_mock
yield create_rag_corpus_mock_pinecone
@pytest.fixture
def create_rag_corpus_mock_vertex_ai_engine_search_config():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_vertex_ai_engine_search_config:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_VERTEX_AI_ENGINE_SEARCH_CONFIG
)
create_rag_corpus_mock_vertex_ai_engine_search_config.return_value = (
create_rag_corpus_lro_mock
)
yield create_rag_corpus_mock_vertex_ai_engine_search_config
@pytest.fixture
def create_rag_corpus_mock_vertex_ai_datastore_search_config():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_vertex_ai_datastore_search_config:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_VERTEX_AI_DATASTORE_SEARCH_CONFIG
)
create_rag_corpus_mock_vertex_ai_datastore_search_config.return_value = (
create_rag_corpus_lro_mock
)
yield create_rag_corpus_mock_vertex_ai_datastore_search_config
@pytest.fixture
def update_rag_corpus_mock_vertex_ai_engine_search_config():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_corpus",
) as update_rag_corpus_mock_vertex_ai_engine_search_config:
update_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_corpus_lro_mock.done.return_value = True
update_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_VERTEX_AI_ENGINE_SEARCH_CONFIG
)
update_rag_corpus_mock_vertex_ai_engine_search_config.return_value = (
update_rag_corpus_lro_mock
)
yield update_rag_corpus_mock_vertex_ai_engine_search_config
@pytest.fixture
def update_rag_corpus_mock_vertex_vector_search():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_corpus",
) as update_rag_corpus_mock_vertex_vector_search:
update_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_corpus_lro_mock.done.return_value = True
update_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_VERTEX_VECTOR_SEARCH
)
update_rag_corpus_mock_vertex_vector_search.return_value = (
update_rag_corpus_lro_mock
)
yield update_rag_corpus_mock_vertex_vector_search
@pytest.fixture
def update_rag_corpus_mock_pinecone():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_corpus",
) as update_rag_corpus_mock_pinecone:
update_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_corpus_lro_mock.done.return_value = True
update_rag_corpus_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_CORPUS_PINECONE
)
update_rag_corpus_mock_pinecone.return_value = update_rag_corpus_lro_mock
yield update_rag_corpus_mock_pinecone
@pytest.fixture
def list_rag_corpora_pager_mock():
import inspect
real_signature = inspect.signature(VertexRagDataServiceClient.list_rag_corpora)
with mock.patch.object(
VertexRagDataServiceClient,
"list_rag_corpora",
) as list_rag_corpora_pager_mock:
list_rag_corpora_pager_mock.return_value = [
ListRagCorporaResponse(
rag_corpora=[
test_rag_constants.TEST_GAPIC_RAG_CORPUS,
],
next_page_token=test_rag_constants.TEST_PAGE_TOKEN,
),
]
# this is needed because metadata wrapper inspects the signature
list_rag_corpora_pager_mock.__signature__ = real_signature
yield list_rag_corpora_pager_mock
@pytest.fixture()
def update_rag_engine_config_basic_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_engine_config",
) as update_rag_engine_config_basic_mock:
update_rag_engine_config_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_engine_config_lro_mock.done.return_value = True
update_rag_engine_config_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_BASIC
)
update_rag_engine_config_basic_mock.return_value = (
update_rag_engine_config_lro_mock
)
yield update_rag_engine_config_basic_mock
@pytest.fixture()
def update_rag_engine_config_scaled_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_engine_config",
) as update_rag_engine_config_scaled_mock:
update_rag_engine_config_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_engine_config_lro_mock.done.return_value = True
update_rag_engine_config_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_SCALED
)
update_rag_engine_config_scaled_mock.return_value = (
update_rag_engine_config_lro_mock
)
yield update_rag_engine_config_scaled_mock
@pytest.fixture()
def update_rag_engine_config_unprovisioned_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_engine_config",
) as update_rag_engine_config_unprovisioned_mock:
update_rag_engine_config_lro_mock = mock.Mock(ga_operation.Operation)
update_rag_engine_config_lro_mock.done.return_value = True
update_rag_engine_config_lro_mock.result.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_UNPROVISIONED
)
update_rag_engine_config_unprovisioned_mock.return_value = (
update_rag_engine_config_lro_mock
)
yield update_rag_engine_config_unprovisioned_mock
@pytest.fixture()
def update_rag_engine_config_mock_exception():
with mock.patch.object(
VertexRagDataServiceClient,
"update_rag_engine_config",
) as update_rag_engine_config_mock_exception:
update_rag_engine_config_mock_exception.side_effect = Exception
yield update_rag_engine_config_mock_exception
@pytest.fixture()
def get_rag_engine_basic_config_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"get_rag_engine_config",
) as get_rag_engine_basic_config_mock:
get_rag_engine_basic_config_mock.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_BASIC
)
yield get_rag_engine_basic_config_mock
@pytest.fixture()
def get_rag_engine_scaled_config_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"get_rag_engine_config",
) as get_rag_engine_scaled_config_mock:
get_rag_engine_scaled_config_mock.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_SCALED
)
yield get_rag_engine_scaled_config_mock
@pytest.fixture()
def get_rag_engine_unprovisioned_config_mock():
with mock.patch.object(
VertexRagDataServiceClient,
"get_rag_engine_config",
) as get_rag_engine_unprovisioned_config_mock:
get_rag_engine_unprovisioned_config_mock.return_value = (
test_rag_constants.TEST_GAPIC_RAG_ENGINE_CONFIG_UNPROVISIONED
)
yield get_rag_engine_unprovisioned_config_mock
@pytest.fixture()
def get_rag_engine_config_mock_exception():
with mock.patch.object(
VertexRagDataServiceClient,
"get_rag_engine_config",
) as get_rag_engine_config_mock_exception:
get_rag_engine_config_mock_exception.side_effect = Exception
yield get_rag_engine_config_mock_exception
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(self):
return self.json_data
@pytest.fixture
def upload_file_mock(authorized_session_mock):
with patch.object(authorized_session_mock, "post") as mock_post:
mock_post.return_value = MockResponse(
test_rag_constants.TEST_RAG_FILE_JSON, 200
)
yield mock_post
@pytest.fixture
def upload_file_with_upload_config_mock(authorized_session_mock):
with patch.object(authorized_session_mock, "post") as mock_post:
mock_post.return_value = MockResponse(
test_rag_constants.TEST_RAG_FILE_JSON_WITH_UPLOAD_CONFIG, 200
)
yield mock_post
@pytest.fixture
def upload_file_not_found_mock(authorized_session_mock):
with patch.object(authorized_session_mock, "post") as mock_post:
mock_post.return_value = MockResponse(None, 404)
yield mock_post
@pytest.fixture
def upload_file_error_mock(authorized_session_mock):
with patch.object(authorized_session_mock, "post") as mock_post:
mock_post.return_value = MockResponse(
test_rag_constants.TEST_RAG_FILE_JSON_ERROR, 200
)
yield mock_post
@pytest.fixture
def open_file_mock():
with mock.patch("builtins.open") as open_file_mock:
yield open_file_mock
@pytest.fixture
def import_files_mock():
with mock.patch.object(
VertexRagDataServiceClient, "import_rag_files"
) as import_files_mock:
import_files_lro_mock = mock.Mock(ga_operation.Operation)
import_files_lro_mock.result.return_value = (
test_rag_constants.TEST_IMPORT_RESPONSE
)
import_files_mock.return_value = import_files_lro_mock
yield import_files_mock
@pytest.fixture
def import_files_async_mock():
with mock.patch.object(
VertexRagDataServiceAsyncClient, "import_rag_files"
) as import_files_async_mock:
import_files_lro_mock = mock.Mock(ga_operation.Operation)
import_files_lro_mock.result.return_value = (
test_rag_constants.TEST_IMPORT_RESPONSE
)
import_files_async_mock.return_value = import_files_lro_mock
yield import_files_async_mock
@pytest.fixture
def list_rag_files_pager_mock():
with mock.patch.object(
VertexRagDataServiceClient, "list_rag_files"
) as list_rag_files_pager_mock:
list_rag_files_pager_mock.return_value = [
ListRagFilesResponse(
rag_files=[
test_rag_constants.TEST_GAPIC_RAG_FILE,
],
next_page_token=test_rag_constants.TEST_PAGE_TOKEN,
),
]
yield list_rag_files_pager_mock
def create_transformation_config(
chunk_size: int = test_rag_constants.TEST_CHUNK_SIZE,
chunk_overlap: int = test_rag_constants.TEST_CHUNK_OVERLAP,
):
return TransformationConfig(
chunking_config=ChunkingConfig(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
),
)
def rag_corpus_eq(returned_corpus, expected_corpus):
assert returned_corpus.name == expected_corpus.name
assert returned_corpus.display_name == expected_corpus.display_name
assert returned_corpus.backend_config.__eq__(expected_corpus.backend_config)
assert returned_corpus.vertex_ai_search_config.__eq__(
expected_corpus.vertex_ai_search_config
)
def rag_file_eq(returned_file, expected_file):
assert returned_file.name == expected_file.name
assert returned_file.display_name == expected_file.display_name
def import_files_request_eq(returned_request, expected_request):
assert returned_request.parent == expected_request.parent
assert (
returned_request.import_rag_files_config.gcs_source.uris
== expected_request.import_rag_files_config.gcs_source.uris
)
assert (
returned_request.import_rag_files_config.google_drive_source.resource_ids
== expected_request.import_rag_files_config.google_drive_source.resource_ids
)
assert (
returned_request.import_rag_files_config.slack_source.channels
== expected_request.import_rag_files_config.slack_source.channels
)
assert (
returned_request.import_rag_files_config.jira_source.jira_queries
== expected_request.import_rag_files_config.jira_source.jira_queries
)
assert (
returned_request.import_rag_files_config.rag_file_transformation_config
== expected_request.import_rag_files_config.rag_file_transformation_config
)
assert (
returned_request.import_rag_files_config.import_result_gcs_sink
== expected_request.import_rag_files_config.import_result_gcs_sink
)
assert (
returned_request.import_rag_files_config.import_result_bigquery_sink
== expected_request.import_rag_files_config.import_result_bigquery_sink
)
def rag_engine_config_eq(returned_config, expected_config):
assert returned_config.name == expected_config.name
assert returned_config.rag_managed_db_config.__eq__(
expected_config.rag_managed_db_config
)
@pytest.mark.usefixtures("google_auth_mock")
class TestRagDataManagement:
def setup_method(self):
importlib.reload(aiplatform.initializer)
importlib.reload(aiplatform)
aiplatform.init(
project=test_rag_constants.TEST_PROJECT,
location=test_rag_constants.TEST_REGION,
)
def teardown_method(self):
aiplatform.initializer.global_pool.shutdown(wait=True)
@pytest.mark.usefixtures("create_rag_corpus_mock")
def test_create_corpus_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS)
@pytest.mark.usefixtures("create_rag_corpus_mock_vertex_vector_search")
def test_create_corpus_vertex_vector_search_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG,
)
rag_corpus_eq(
rag_corpus, test_rag_constants.TEST_RAG_CORPUS_VERTEX_VECTOR_SEARCH
)
@pytest.mark.usefixtures("create_rag_corpus_mock_cmek")
def test_create_corpus_cmek_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
encryption_spec=test_rag_constants.TEST_ENCRYPTION_SPEC,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_CMEK_RAG_CORPUS)
@pytest.mark.usefixtures("create_rag_corpus_mock_pinecone")
def test_create_corpus_pinecone_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS_PINECONE)
@pytest.mark.usefixtures("create_rag_corpus_mock_vertex_ai_engine_search_config")
def test_create_corpus_vais_engine_search_config_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_ENGINE,
)
rag_corpus_eq(
rag_corpus,
test_rag_constants.TEST_RAG_CORPUS_VERTEX_AI_ENGINE_SEARCH_CONFIG,
)
@pytest.mark.usefixtures("create_rag_corpus_mock_vertex_ai_datastore_search_config")
def test_create_corpus_vais_datastore_search_config_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_DATASTORE,
)
rag_corpus_eq(
rag_corpus,
test_rag_constants.TEST_RAG_CORPUS_VERTEX_AI_DATASTORE_SEARCH_CONFIG,
)
def test_create_corpus_vais_datastore_search_config_with_backend_config_failure(
self,
):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_DATASTORE,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG,
)
e.match("Only one of vertex_ai_search_config or backend_config can be set.")
def test_set_vertex_ai_search_config_with_invalid_serving_config_failure(self):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_INVALID,
)
e.match(
"serving_config must be of the format `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}/servingConfigs/{serving_config}` or `projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store}/servingConfigs/{serving_config}`"
)
def test_set_vertex_ai_search_config_with_empty_serving_config_failure(self):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_EMPTY,
)
e.match("serving_config must be set.")
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_create_corpus_failure(self):
with pytest.raises(RuntimeError) as e:
rag.create_corpus(display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME)
e.match("Failed in RagCorpus creation due to")
@pytest.mark.usefixtures("update_rag_corpus_mock_vertex_ai_engine_search_config")
def test_update_corpus_vais_engine_search_config_success(self):
rag_corpus = rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_ENGINE,
)
rag_corpus_eq(
rag_corpus,
test_rag_constants.TEST_RAG_CORPUS_VERTEX_AI_ENGINE_SEARCH_CONFIG,
)
def test_update_corpus_vais_datastore_search_config_with_backend_config_failure(
self,
):
with pytest.raises(ValueError) as e:
rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants.TEST_VERTEX_AI_SEARCH_CONFIG_DATASTORE,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG,
)
e.match("Only one of vertex_ai_search_config or backend_config can be set.")
@pytest.mark.usefixtures("update_rag_corpus_mock_pinecone")
def test_update_corpus_pinecone_success(self):
rag_corpus = rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS_PINECONE)
@pytest.mark.usefixtures("update_rag_corpus_mock_pinecone")
def test_update_corpus_pinecone_no_display_name_success(self):
rag_corpus = rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS_PINECONE)
@pytest.mark.usefixtures("update_rag_corpus_mock_pinecone")
def test_update_corpus_pinecone_with_description_success(self):
rag_corpus = rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
description=test_rag_constants.TEST_CORPUS_DISCRIPTION,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS_PINECONE)
@pytest.mark.usefixtures("update_rag_corpus_mock_pinecone")
def test_update_corpus_pinecone_with_description_and_display_name_success(self):
rag_corpus = rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
description=test_rag_constants.TEST_CORPUS_DISCRIPTION,
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS_PINECONE)
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_update_corpus_failure(self):
with pytest.raises(RuntimeError) as e:
rag.update_corpus(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=test_rag_constants.TEST_CORPUS_DISPLAY_NAME,
)
e.match("Failed in RagCorpus update due to")
@pytest.mark.usefixtures("rag_data_client_mock")
@pytest.mark.parametrize("corpus_name", ["Capital-Corpus", "123-Corpus"])
def test_get_corpus_with_valid_resource_name_success(self, corpus_name):
# Tests regex allowing names starting with uppercase or digits.
rag_corpus = rag.get_corpus(corpus_name)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS)
@pytest.mark.usefixtures("rag_data_client_mock")
@pytest.mark.parametrize("file_name", ["Capital-File", "123-File"])
def test_get_file_with_valid_resource_name_success(self, file_name):
# This test covers the regex change for RAG file names.
rag_file = rag.get_file(
name=file_name,
corpus_name=test_rag_constants.TEST_RAG_CORPUS_ID,
)
rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE)
def test_get_corpus_with_invalid_name_format_failure(self):
# Verify names starting with invalid characters (like underscore) still fail.
with pytest.raises(ValueError) as e:
rag.get_corpus("_invalid-name")
e.match(
"name must be of the format `projects/{project}/locations/{location}/"
"ragCorpora/{rag_corpus}` or `{rag_corpus}`"
)
@pytest.mark.usefixtures("rag_data_client_mock")
def test_get_corpus_success(self):
rag_corpus = rag.get_corpus(test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS)
@pytest.mark.usefixtures("rag_data_client_mock")
def test_get_corpus_id_success(self):
rag_corpus = rag.get_corpus(test_rag_constants.TEST_RAG_CORPUS_ID)
rag_corpus_eq(rag_corpus, test_rag_constants.TEST_RAG_CORPUS)
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_get_corpus_failure(self):
with pytest.raises(RuntimeError) as e:
rag.get_corpus(test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME)
e.match("Failed in getting the RagCorpus due to")
def test_list_corpora_pager_success(self, list_rag_corpora_pager_mock):
aiplatform.init(
project=test_rag_constants.TEST_PROJECT,
location=test_rag_constants.TEST_REGION,
)
pager = rag.list_corpora(page_size=1)
list_rag_corpora_pager_mock.assert_called_once()
assert pager[0].next_page_token == test_rag_constants.TEST_PAGE_TOKEN
def test_list_corpora_with_metadata(self, list_rag_corpora_pager_mock):
aiplatform.init(
project=test_rag_constants.TEST_PROJECT,
location=test_rag_constants.TEST_REGION,
request_metadata=[("key", "value")],
)
pager = rag.list_corpora(page_size=1)
call_args = list_rag_corpora_pager_mock.call_args
assert call_args.kwargs["metadata"] == [("key", "value")]
assert pager[0].next_page_token == test_rag_constants.TEST_PAGE_TOKEN
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_list_corpora_failure(self):
with pytest.raises(RuntimeError) as e:
rag.list_corpora()
e.match("Failed in listing the RagCorpora due to")
def test_delete_corpus_success(self, rag_data_client_mock):
rag.delete_corpus(test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME)
assert rag_data_client_mock.call_count == 2
def test_delete_corpus_id_success(self, rag_data_client_mock):
rag.delete_corpus(test_rag_constants.TEST_RAG_CORPUS_ID)
assert rag_data_client_mock.call_count == 2
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_delete_corpus_failure(self):
with pytest.raises(RuntimeError) as e:
rag.delete_corpus(test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME)
e.match("Failed in RagCorpus deletion due to")
@pytest.mark.usefixtures("open_file_mock")
def test_upload_file_success(
self,
upload_file_mock,
):
aiplatform.init(
project=test_rag_constants.TEST_PROJECT,
location=test_rag_constants.TEST_REGION,
)
rag_file = rag.upload_file(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
path=test_rag_constants.TEST_PATH,
display_name=test_rag_constants.TEST_FILE_DISPLAY_NAME,
)
upload_file_mock.assert_called_once()
_, mock_kwargs = upload_file_mock.call_args
assert mock_kwargs["url"] == test_rag_constants.TEST_UPLOAD_REQUEST_URI
assert mock_kwargs["headers"] == test_rag_constants.TEST_HEADERS
rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE)
@pytest.mark.usefixtures("open_file_mock")
def test_upload_file_success_with_transformation_config(
self,
upload_file_with_upload_config_mock,
):
aiplatform.init(
project=test_rag_constants.TEST_PROJECT,
location=test_rag_constants.TEST_REGION,
)
rag_file = rag.upload_file(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
path=test_rag_constants.TEST_PATH,
display_name=test_rag_constants.TEST_FILE_DISPLAY_NAME,
transformation_config=create_transformation_config(),
)
upload_file_with_upload_config_mock.assert_called_once()
_, mock_kwargs = upload_file_with_upload_config_mock.call_args
assert mock_kwargs["url"] == test_rag_constants.TEST_UPLOAD_REQUEST_URI
assert mock_kwargs["headers"] == test_rag_constants.TEST_HEADERS
rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE)
@pytest.mark.usefixtures("rag_data_client_mock_exception", "open_file_mock")
def test_upload_file_failure(self):
with pytest.raises(RuntimeError) as e:
rag.upload_file(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
path=test_rag_constants.TEST_PATH,
display_name=test_rag_constants.TEST_FILE_DISPLAY_NAME,
)
e.match("Failed in uploading the RagFile due to")
@pytest.mark.usefixtures("open_file_mock", "upload_file_not_found_mock")
def test_upload_file_not_found(self):
with pytest.raises(ValueError) as e:
rag.upload_file(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
path=test_rag_constants.TEST_PATH,
display_name=test_rag_constants.TEST_FILE_DISPLAY_NAME,
)
e.match("is not found")
@pytest.mark.usefixtures("open_file_mock", "upload_file_error_mock")
def test_upload_file_error(self):
with pytest.raises(RuntimeError) as e:
rag.upload_file(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
path=test_rag_constants.TEST_PATH,
display_name=test_rag_constants.TEST_FILE_DISPLAY_NAME,
)
e.match("Failed in indexing the RagFile due to")
def test_import_files(self, import_files_mock):
response = rag.import_files(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
)
import_files_mock.assert_called_once()
assert response.imported_rag_files_count == 2
def test_import_files_with_import_result_gcs_sink(self, import_files_mock):
response = rag.import_files(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
import_result_sink=test_rag_constants.TEST_IMPORT_RESULT_GCS_SINK,
)
import_files_mock.assert_called_once()
assert response.imported_rag_files_count == 2
def test_import_files_with_import_result_bigquery_sink(self, import_files_mock):
response = rag.import_files(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
import_result_sink=test_rag_constants.TEST_IMPORT_RESULT_BIGQUERY_SINK,
)
import_files_mock.assert_called_once()
assert response.imported_rag_files_count == 2
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_import_files_failure(self):
with pytest.raises(RuntimeError) as e:
rag.import_files(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
)
e.match("Failed in importing the RagFiles due to")
@pytest.mark.asyncio
async def test_import_files_async(self, import_files_async_mock):
response = await rag.import_files_async(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
)
import_files_async_mock.assert_called_once()
assert response.result().imported_rag_files_count == 2
@pytest.mark.asyncio
async def test_import_files_with_import_result_gcs_sink_async(
self, import_files_async_mock
):
response = await rag.import_files_async(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
import_result_sink=test_rag_constants.TEST_IMPORT_RESULT_GCS_SINK,
)
import_files_async_mock.assert_called_once()
assert response.result().imported_rag_files_count == 2
@pytest.mark.asyncio
async def test_import_files_with_import_result_bigquery_sink_async(
self, import_files_async_mock
):
response = await rag.import_files_async(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
import_result_sink=test_rag_constants.TEST_IMPORT_RESULT_BIGQUERY_SINK,
)
import_files_async_mock.assert_called_once()
assert response.result().imported_rag_files_count == 2
@pytest.mark.asyncio
@pytest.mark.usefixtures("rag_data_async_client_mock_exception")
async def test_import_files_async_failure(self):
with pytest.raises(RuntimeError) as e:
await rag.import_files_async(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[test_rag_constants.TEST_GCS_PATH],
)
e.match("Failed in importing the RagFiles due to")
@pytest.mark.usefixtures("rag_data_client_mock")
def test_get_file_success(self):
rag_file = rag.get_file(test_rag_constants.TEST_RAG_FILE_RESOURCE_NAME)
rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE)
@pytest.mark.usefixtures("rag_data_client_mock")
def test_get_file_id_success(self):
rag_file = rag.get_file(
name=test_rag_constants.TEST_RAG_FILE_ID,
corpus_name=test_rag_constants.TEST_RAG_CORPUS_ID,
)
rag_file_eq(rag_file, test_rag_constants.TEST_RAG_FILE)
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_get_file_failure(self):
with pytest.raises(RuntimeError) as e:
rag.get_file(test_rag_constants.TEST_RAG_FILE_RESOURCE_NAME)
e.match("Failed in getting the RagFile due to")
def test_list_files_pager_success(self, list_rag_files_pager_mock):
files = rag.list_files(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
page_size=1,
)
list_rag_files_pager_mock.assert_called_once()
assert files[0].next_page_token == test_rag_constants.TEST_PAGE_TOKEN
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_list_files_failure(self):
with pytest.raises(RuntimeError) as e:
rag.list_files(corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME)
e.match("Failed in listing the RagFiles due to")
def test_delete_file_success(self, rag_data_client_mock):
rag.delete_file(test_rag_constants.TEST_RAG_FILE_RESOURCE_NAME)
assert rag_data_client_mock.call_count == 2
def test_delete_file_id_success(self, rag_data_client_mock):
rag.delete_file(
name=test_rag_constants.TEST_RAG_FILE_ID,
corpus_name=test_rag_constants.TEST_RAG_CORPUS_ID,
)
# Passing corpus_name will result in 3 calls to rag_data_client
assert rag_data_client_mock.call_count == 3
@pytest.mark.usefixtures("rag_data_client_mock_exception")
def test_delete_file_failure(self):
with pytest.raises(RuntimeError) as e:
rag.delete_file(test_rag_constants.TEST_RAG_FILE_RESOURCE_NAME)
e.match("Failed in RagFile deletion due to")
@pytest.mark.usefixtures("rag_data_client_mock")
def test_inline_citations_and_references_success(self):
response = rag.add_inline_citations_and_references(
original_text_str=test_rag_constants.TEST_ORIGINAL_TEXT,
grounding_supports=test_rag_constants.TEST_GROUNDING_SUPPORTS,
grounding_chunks=test_rag_constants.TEST_GROUNDING_CHUNKS,
)
assert response.cited_text == test_rag_constants.TEST_CITED_TEXT
assert response.final_bibliography == test_rag_constants.TEST_FINAL_BIBLIOGRAPHY
def test_prepare_import_files_request_list_gcs_uris(self):
paths = [test_rag_constants.TEST_GCS_PATH]
request = prepare_import_files_request(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=paths,
transformation_config=create_transformation_config(),
)
import_files_request_eq(request, test_rag_constants.TEST_IMPORT_REQUEST_GCS)
@pytest.mark.parametrize(
"path",
[
test_rag_constants.TEST_DRIVE_FOLDER,
test_rag_constants.TEST_DRIVE_FOLDER_2,
],
)
def test_prepare_import_files_request_drive_folders(self, path):
request = prepare_import_files_request(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[path],
transformation_config=create_transformation_config(),
)
import_files_request_eq(
request, test_rag_constants.TEST_IMPORT_REQUEST_DRIVE_FOLDER
)
@pytest.mark.parametrize(
"path",
[
test_rag_constants.TEST_DRIVE_FOLDER,
test_rag_constants.TEST_DRIVE_FOLDER_2,
],
)
def test_prepare_import_files_request_drive_folders_with_pdf_parsing(self, path):
request = prepare_import_files_request(
corpus_name=test_rag_constants.TEST_RAG_CORPUS_RESOURCE_NAME,
paths=[path],
transformation_config=create_transformation_config(),
)
import_files_request_eq(
request, test_rag_constants.TEST_IMPORT_REQUEST_DRIVE_FOLDER