32
32
from google .cloud .aiplatform import base
33
33
from google .cloud .aiplatform import initializer
34
34
from google .cloud .aiplatform import utils
35
+ from google .cloud .aiplatform .utils import resource_manager_utils
35
36
36
37
from google .cloud .aiplatform .utils import featurestore_utils
37
38
from google .cloud .aiplatform_v1 .services .featurestore_service import (
54
55
55
56
from google .cloud import bigquery
56
57
from google .cloud import bigquery_storage
58
+ from google .cloud import resourcemanager
57
59
58
60
from google .cloud .bigquery_storage_v1 .types import stream as gcbqs_stream
59
61
60
62
# project
61
63
_TEST_PROJECT = "test-project"
64
+ _TEST_PROJECT_DIFF = "test-project-diff"
62
65
_TEST_LOCATION = "us-central1"
63
66
_TEST_PARENT = f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } "
64
67
@@ -340,6 +343,18 @@ def uuid_mock():
340
343
return uuid .UUID (int = 1 )
341
344
342
345
346
+ # All Resource Manager Mocks
347
+ @pytest .fixture
348
+ def get_project_mock ():
349
+ with patch .object (
350
+ resourcemanager .ProjectsClient , "get_project"
351
+ ) as get_project_mock :
352
+ get_project_mock .return_value = resourcemanager .Project (
353
+ project_id = _TEST_PROJECT ,
354
+ )
355
+ yield get_project_mock
356
+
357
+
343
358
# All BigQuery Mocks
344
359
@pytest .fixture
345
360
def bq_client_mock ():
@@ -1286,10 +1301,13 @@ def test_batch_serve_to_gcs_with_invalid_gcs_destination_type(self):
1286
1301
"bq_delete_dataset_mock" ,
1287
1302
"bqs_init_client_mock" ,
1288
1303
"bqs_create_read_session" ,
1304
+ "get_project_mock" ,
1289
1305
)
1290
1306
@patch ("uuid.uuid4" , uuid_mock )
1291
1307
def test_batch_serve_to_df (self , batch_read_feature_values_mock ):
1292
- aiplatform .init (project = _TEST_PROJECT )
1308
+
1309
+ aiplatform .init (project = _TEST_PROJECT_DIFF )
1310
+
1293
1311
my_featurestore = aiplatform .Featurestore (
1294
1312
featurestore_name = _TEST_FEATURESTORE_NAME
1295
1313
)
@@ -1299,7 +1317,7 @@ def test_batch_serve_to_df(self, batch_read_feature_values_mock):
1299
1317
expected_temp_bq_dataset_name = f"temp_{ _TEST_FEATURESTORE_ID } _{ uuid .uuid4 ()} " .replace (
1300
1318
"-" , "_"
1301
1319
)
1302
- expecte_temp_bq_dataset_id = f"{ initializer . global_config . project } .{ expected_temp_bq_dataset_name } " [
1320
+ expecte_temp_bq_dataset_id = f"{ _TEST_PROJECT } .{ expected_temp_bq_dataset_name } " [
1303
1321
:1024
1304
1322
]
1305
1323
expected_temp_bq_read_instances_table_id = (
@@ -1695,6 +1713,7 @@ def test_ingest_from_gcs_with_invalid_gcs_source_type(self):
1695
1713
"bq_init_dataset_mock" ,
1696
1714
"bq_create_dataset_mock" ,
1697
1715
"bq_delete_dataset_mock" ,
1716
+ "get_project_mock" ,
1698
1717
)
1699
1718
@patch ("uuid.uuid4" , uuid_mock )
1700
1719
def test_ingest_from_df_using_column (
@@ -1704,7 +1723,7 @@ def test_ingest_from_df_using_column(
1704
1723
bq_init_schema_field_mock ,
1705
1724
):
1706
1725
1707
- aiplatform .init (project = _TEST_PROJECT )
1726
+ aiplatform .init (project = _TEST_PROJECT_DIFF )
1708
1727
1709
1728
my_entity_type = aiplatform .EntityType (entity_type_name = _TEST_ENTITY_TYPE_NAME )
1710
1729
df_source = pd .DataFrame ()
@@ -1717,7 +1736,7 @@ def test_ingest_from_df_using_column(
1717
1736
expected_temp_bq_dataset_name = f"temp_{ _TEST_FEATURESTORE_ID } _{ uuid .uuid4 ()} " .replace (
1718
1737
"-" , "_"
1719
1738
)
1720
- expecte_temp_bq_dataset_id = f"{ initializer . global_config . project } .{ expected_temp_bq_dataset_name } " [
1739
+ expecte_temp_bq_dataset_id = f"{ _TEST_PROJECT } .{ expected_temp_bq_dataset_name } " [
1721
1740
:1024
1722
1741
]
1723
1742
expected_temp_bq_table_id = (
@@ -1755,6 +1774,7 @@ def test_ingest_from_df_using_column(
1755
1774
"bq_init_dataset_mock" ,
1756
1775
"bq_create_dataset_mock" ,
1757
1776
"bq_delete_dataset_mock" ,
1777
+ "get_project_mock" ,
1758
1778
)
1759
1779
@patch ("uuid.uuid4" , uuid_mock )
1760
1780
def test_ingest_from_df_using_datetime (
@@ -1763,7 +1783,8 @@ def test_ingest_from_df_using_datetime(
1763
1783
bq_load_table_from_dataframe_mock ,
1764
1784
bq_init_schema_field_mock ,
1765
1785
):
1766
- aiplatform .init (project = _TEST_PROJECT )
1786
+
1787
+ aiplatform .init (project = _TEST_PROJECT_DIFF )
1767
1788
1768
1789
my_entity_type = aiplatform .EntityType (entity_type_name = _TEST_ENTITY_TYPE_NAME )
1769
1790
df_source = pd .DataFrame ()
@@ -1777,7 +1798,7 @@ def test_ingest_from_df_using_datetime(
1777
1798
expected_temp_bq_dataset_name = f"temp_{ _TEST_FEATURESTORE_ID } _{ uuid .uuid4 ()} " .replace (
1778
1799
"-" , "_"
1779
1800
)
1780
- expecte_temp_bq_dataset_id = f"{ initializer . global_config . project } .{ expected_temp_bq_dataset_name } " [
1801
+ expecte_temp_bq_dataset_id = f"{ _TEST_PROJECT } .{ expected_temp_bq_dataset_name } " [
1781
1802
:1024
1782
1803
]
1783
1804
expected_temp_bq_table_id = (
@@ -2431,3 +2452,10 @@ def test_create_feature(self, create_feature_mock, sync):
2431
2452
),
2432
2453
metadata = _TEST_REQUEST_METADATA ,
2433
2454
)
2455
+
2456
+
2457
+ class TestResourceManagerUtils :
2458
+ @pytest .mark .usefixtures ("get_project_mock" )
2459
+ def test_get_project_id (self ):
2460
+ project_id = resource_manager_utils .get_project_id (project_number = "123456" )
2461
+ assert project_id == _TEST_PROJECT
0 commit comments