2020import concurrent .futures
2121import logging
2222import os
23- import requests
2423import ssl
2524from unittest import mock
2625
2928from google .auth import credentials
3029import httpx
3130import pytest
31+ import requests
3232
3333from ... import _api_client as api_client
3434from ... import _base_url as base_url
3535from ... import _replay_api_client as replay_api_client
3636from ... import Client
3737from ... import types
38+
3839try :
3940 import aiohttp
41+
4042 AIOHTTP_NOT_INSTALLED = False
4143except ImportError :
4244 AIOHTTP_NOT_INSTALLED = True
@@ -331,6 +333,84 @@ def test_vertexai_from_env_true(monkeypatch):
331333 assert client .models ._api_client .location == location
332334
333335
336+ def test_enterprise_constructor_true ():
337+ client = Client (
338+ enterprise = True , project = "fake_project_id" , location = "fake-location"
339+ )
340+ assert client .models ._api_client .vertexai
341+
342+
343+ def test_enterprise_constructor_false ():
344+ client = Client (enterprise = False , api_key = "fake_api_key" )
345+ assert not client .models ._api_client .vertexai
346+
347+
348+ def test_enterprise_constructor_conflict ():
349+ with pytest .raises (
350+ ValueError ,
351+ match = (
352+ "enterprise and vertexai flags have conflicting values, please set"
353+ " enterprise value only."
354+ ),
355+ ):
356+ Client (enterprise = True , vertexai = False )
357+
358+
359+ def test_enterprise_env_true (monkeypatch ):
360+ monkeypatch .setenv ("GOOGLE_GENAI_USE_ENTERPRISE" , "true" )
361+ client = Client (project = "fake_project_id" , location = "fake-location" )
362+ assert client .models ._api_client .vertexai
363+
364+
365+ def test_enterprise_env_false (monkeypatch ):
366+ monkeypatch .setenv ("GOOGLE_GENAI_USE_ENTERPRISE" , "false" )
367+ client = Client (api_key = "fake_api_key" )
368+ assert not client .models ._api_client .vertexai
369+
370+
371+ def test_enterprise_env_conflict_warning (monkeypatch ):
372+ monkeypatch .setenv ("GOOGLE_GENAI_USE_ENTERPRISE" , "true" )
373+ monkeypatch .setenv ("GOOGLE_GENAI_USE_VERTEXAI" , "false" )
374+
375+ with pytest .warns (
376+ UserWarning ,
377+ match = (
378+ "Warning: Both GOOGLE_GENAI_USE_ENTERPRISE and"
379+ " GOOGLE_GENAI_USE_VERTEXAI are set with conflicting values. The"
380+ " value of GOOGLE_GENAI_USE_ENTERPRISE will be used."
381+ ),
382+ ):
383+ # In BaseApiClient, resolving this warning.
384+ client = Client (project = "fake_project_id" , location = "fake-location" )
385+
386+ assert client .models ._api_client .vertexai
387+
388+
389+ def test_enterprise_constructor_precedence (monkeypatch ):
390+ monkeypatch .setenv ("GOOGLE_GENAI_USE_ENTERPRISE" , "false" )
391+ client = Client (
392+ enterprise = True , project = "fake_project_id" , location = "fake-location"
393+ )
394+ assert client .models ._api_client .vertexai
395+
396+
397+ def test_enterprise_precedence_over_vertexai_constructor ():
398+ client = Client (
399+ enterprise = True ,
400+ vertexai = True ,
401+ project = "fake_project_id" ,
402+ location = "fake-location" ,
403+ )
404+ assert client .models ._api_client .vertexai
405+
406+
407+ def test_enterprise_env_precedence_over_vertexai_env (monkeypatch ):
408+ monkeypatch .setenv ("GOOGLE_GENAI_USE_ENTERPRISE" , "false" )
409+ monkeypatch .setenv ("GOOGLE_GENAI_USE_VERTEXAI" , "true" )
410+ client = Client (api_key = "fake_api_key" )
411+ assert not client .models ._api_client .vertexai
412+
413+
334414def test_vertexai_from_constructor ():
335415 project_id = "fake_project_id"
336416 location = "fake-location"
@@ -373,7 +453,9 @@ def mock_auth_default(scopes=None):
373453 monkeypatch .setattr (google .auth , "default" , mock_auth_default )
374454 # Including a base_url override skips the check for having proj/location or
375455 # api_key set.
376- client = Client (vertexai = True , http_options = {"base_url" : "https://override.com/" })
456+ client = Client (
457+ vertexai = True , http_options = {"base_url" : "https://override.com/" }
458+ )
377459 assert client .models ._api_client .location is None
378460
379461
@@ -461,7 +543,7 @@ def test_vertexai_default_location_to_global_with_vertexai_base_url(
461543 m .setenv ("GOOGLE_CLOUD_PROJECT" , project_id )
462544 client = Client (
463545 vertexai = True ,
464- http_options = {' base_url' : ' https://fake-url.googleapis.com' },
546+ http_options = {" base_url" : " https://fake-url.googleapis.com" },
465547 )
466548 # Implicit project takes precedence over implicit api_key
467549 assert client .models ._api_client .location == "global"
@@ -479,7 +561,7 @@ def test_vertexai_default_location_to_global_with_arbitrary_base_url(
479561 m .setenv ("GOOGLE_CLOUD_PROJECT" , project_id )
480562 client = Client (
481563 vertexai = True ,
482- http_options = {' base_url' : ' https://fake-url.com' },
564+ http_options = {" base_url" : " https://fake-url.com" },
483565 )
484566 # Implicit project takes precedence over implicit api_key
485567 assert not client .models ._api_client .location
@@ -602,10 +684,7 @@ def test_vertexai_explicit_credentials(monkeypatch):
602684 monkeypatch .setenv ("GOOGLE_CLOUD_LOCATION" , "fake-location" )
603685 monkeypatch .setenv ("GOOGLE_API_KEY" , "env_api_key" )
604686
605- client = Client (
606- vertexai = True ,
607- credentials = creds
608- )
687+ client = Client (vertexai = True , credentials = creds )
609688
610689 assert client .models ._api_client .vertexai
611690 assert client .models ._api_client .project
@@ -1399,7 +1478,9 @@ def refresh_side_effect(request):
13991478 )
14001479 mock_request = mock .Mock (return_value = mock_http_response )
14011480 monkeypatch .setattr (
1402- google .auth .transport .requests .AuthorizedSession , "request" , mock_request
1481+ google .auth .transport .requests .AuthorizedSession ,
1482+ "request" ,
1483+ mock_request ,
14031484 )
14041485 else :
14051486 # Non-cloud environment w/o certificates uses httpx.Response
0 commit comments