21
21
from google import auth
22
22
from google .api_core import operation
23
23
from google .auth import credentials as auth_credentials
24
+ from google .cloud import bigquery
24
25
from google .cloud import aiplatform
25
26
from google .cloud .aiplatform import base
26
27
from google .cloud .aiplatform import initializer
42
43
43
44
_TEST_PROJECT = "test-project"
44
45
_TEST_LOCATION = "us-central1"
46
+ _TEST_ALTERNATE_LOCATION = "europe-west6"
45
47
_TEST_ID = "1028944691210842416"
46
48
_TEST_PARENT = f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } "
47
49
_TEST_NAME = f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } /datasets/{ _TEST_ID } "
53
55
)
54
56
55
57
_TEST_SOURCE_URI_BQ = "bq://my-project.my-dataset.table"
58
+ _TEST_TARGET_BQ_DATASET = f"{ _TEST_PROJECT } .target-dataset"
59
+ _TEST_TARGET_BQ_TABLE = f"{ _TEST_TARGET_BQ_DATASET } .target-table"
56
60
_TEST_DISPLAY_NAME = "my_dataset_1234"
57
61
_TEST_METADATA_SCHEMA_URI_MULTIMODAL = (
58
62
"gs://google-cloud-aiplatform/schema/dataset/metadata/multimodal_1.0.0.yaml"
@@ -168,6 +172,24 @@ def bigframes_import_mock():
168
172
del sys .modules ["bigframes.pandas" ]
169
173
170
174
175
+ @pytest .fixture
176
+ def get_bq_dataset_mock ():
177
+ with mock .patch .object (bigquery .Client , "get_dataset" ) as get_bq_dataset_mock :
178
+ bq_dataset = mock .Mock ()
179
+ bq_dataset .location = _TEST_LOCATION
180
+ get_bq_dataset_mock .return_value = bq_dataset
181
+ yield get_bq_dataset_mock
182
+
183
+
184
+ @pytest .fixture
185
+ def get_bq_dataset_alternate_location_mock ():
186
+ with mock .patch .object (bigquery .Client , "get_dataset" ) as get_bq_dataset_mock :
187
+ bq_dataset = mock .Mock ()
188
+ bq_dataset .location = _TEST_ALTERNATE_LOCATION
189
+ get_bq_dataset_mock .return_value = bq_dataset
190
+ yield get_bq_dataset_mock
191
+
192
+
171
193
@pytest .fixture
172
194
def update_dataset_with_template_config_mock ():
173
195
with mock .patch .object (
@@ -259,7 +281,7 @@ def test_create_dataset_from_bigquery(self, create_dataset_mock, sync):
259
281
)
260
282
261
283
@pytest .mark .skip (reason = "flaky with other tests mocking bigframes" )
262
- @pytest .mark .usefixtures ("get_dataset_mock" )
284
+ @pytest .mark .usefixtures ("get_dataset_mock" , "get_bq_dataset_mock" )
263
285
def test_create_dataset_from_pandas (
264
286
self , create_dataset_mock , bigframes_import_mock
265
287
):
@@ -273,55 +295,100 @@ def test_create_dataset_from_pandas(
273
295
"answer" : ["answer" ],
274
296
}
275
297
)
276
- bq_table = "my-project.my-dataset.my-table"
277
298
ummd .MultimodalDataset .from_pandas (
278
299
dataframe = dataframe ,
279
- target_table_id = bq_table ,
300
+ target_table_id = _TEST_TARGET_BQ_TABLE ,
280
301
display_name = _TEST_DISPLAY_NAME ,
281
302
)
282
303
expected_dataset = gca_dataset .Dataset (
283
304
display_name = _TEST_DISPLAY_NAME ,
284
305
metadata_schema_uri = _TEST_METADATA_SCHEMA_URI_MULTIMODAL ,
285
- metadata = {"inputConfig" : {"bigquerySource" : {"uri" : f"bq://{ bq_table } " }}},
306
+ metadata = {
307
+ "inputConfig" : {
308
+ "bigquerySource" : {"uri" : f"bq://{ _TEST_TARGET_BQ_TABLE } " }
309
+ }
310
+ },
286
311
)
287
312
create_dataset_mock .assert_called_once_with (
288
313
dataset = expected_dataset ,
289
314
parent = _TEST_PARENT ,
290
315
timeout = None ,
291
316
)
292
317
bigframes_mock .to_gbq .assert_called_once_with (
293
- destination_table = bq_table ,
318
+ destination_table = _TEST_TARGET_BQ_TABLE ,
294
319
if_exists = "replace" ,
295
320
)
296
321
297
322
@pytest .mark .skip (reason = "flaky with other tests mocking bigframes" )
298
- @pytest .mark .usefixtures ("bigframes_import_mock" )
299
- @pytest .mark .usefixtures ("get_dataset_mock" )
323
+ @pytest .mark .usefixtures (
324
+ "bigframes_import_mock" , "get_dataset_mock" , "get_bq_dataset_mock"
325
+ )
300
326
def test_create_dataset_from_bigframes (self , create_dataset_mock ):
301
327
aiplatform .init (project = _TEST_PROJECT )
302
328
bigframes_df = mock .Mock ()
303
- bq_table = "my-project.my-dataset.my-table"
304
329
ummd .MultimodalDataset .from_bigframes (
305
330
dataframe = bigframes_df ,
306
- target_table_id = bq_table ,
331
+ target_table_id = _TEST_TARGET_BQ_TABLE ,
307
332
display_name = _TEST_DISPLAY_NAME ,
308
333
)
309
334
310
335
bigframes_df .to_gbq .assert_called_once_with (
311
- destination_table = bq_table ,
336
+ destination_table = _TEST_TARGET_BQ_TABLE ,
312
337
if_exists = "replace" ,
313
338
)
314
339
expected_dataset = gca_dataset .Dataset (
315
340
display_name = _TEST_DISPLAY_NAME ,
316
341
metadata_schema_uri = _TEST_METADATA_SCHEMA_URI_MULTIMODAL ,
317
- metadata = {"inputConfig" : {"bigquerySource" : {"uri" : f"bq://{ bq_table } " }}},
342
+ metadata = {
343
+ "inputConfig" : {
344
+ "bigquerySource" : {"uri" : f"bq://{ _TEST_TARGET_BQ_TABLE } " }
345
+ }
346
+ },
318
347
)
319
348
create_dataset_mock .assert_called_once_with (
320
349
dataset = expected_dataset ,
321
350
parent = _TEST_PARENT ,
322
351
timeout = None ,
323
352
)
324
353
354
+ @pytest .mark .skip (reason = "flaky with other tests mocking bigframes" )
355
+ @pytest .mark .usefixtures ("bigframes_import_mock" )
356
+ def test_create_dataset_from_bigframes_different_project_throws_error (self ):
357
+ aiplatform .init (project = _TEST_PROJECT )
358
+ bigframes_df = mock .Mock ()
359
+ with pytest .raises (ValueError ):
360
+ ummd .MultimodalDataset .from_bigframes (
361
+ dataframe = bigframes_df ,
362
+ target_table_id = "another_project.dataset.table" ,
363
+ display_name = _TEST_DISPLAY_NAME ,
364
+ )
365
+
366
+ @pytest .mark .skip (reason = "flaky with other tests mocking bigframes" )
367
+ @pytest .mark .usefixtures (
368
+ "bigframes_import_mock" , "get_bq_dataset_alternate_location_mock"
369
+ )
370
+ def test_create_dataset_from_bigframes_different_location_throws_error (self ):
371
+ aiplatform .init (project = _TEST_PROJECT )
372
+ bigframes_df = mock .Mock ()
373
+ with pytest .raises (ValueError ):
374
+ ummd .MultimodalDataset .from_bigframes (
375
+ dataframe = bigframes_df ,
376
+ target_table_id = _TEST_TARGET_BQ_TABLE ,
377
+ display_name = _TEST_DISPLAY_NAME ,
378
+ )
379
+
380
+ @pytest .mark .skip (reason = "flaky with other tests mocking bigframes" )
381
+ @pytest .mark .usefixtures ("bigframes_import_mock" )
382
+ def test_create_dataset_from_bigframes_invalid_target_table_id_throws_error (self ):
383
+ aiplatform .init (project = _TEST_PROJECT )
384
+ bigframes_df = mock .Mock ()
385
+ with pytest .raises (ValueError ):
386
+ ummd .MultimodalDataset .from_bigframes (
387
+ dataframe = bigframes_df ,
388
+ target_table_id = "invalid-table" ,
389
+ display_name = _TEST_DISPLAY_NAME ,
390
+ )
391
+
325
392
@pytest .mark .usefixtures ("get_dataset_mock" )
326
393
def test_update_dataset (self , update_dataset_mock ):
327
394
aiplatform .init (project = _TEST_PROJECT )
0 commit comments