@@ -72,6 +72,36 @@ def test_constructor_with_both_cert_sources():
7272 )
7373
7474
75+ def test_constructor_with_api_key ():
76+
77+ options = client_options .ClientOptions (
78+ api_endpoint = "foo.googleapis.com" ,
79+ client_cert_source = get_client_cert ,
80+ quota_project_id = "quote-proj" ,
81+ api_key = "api-key" ,
82+ scopes = [
83+ "https://www.googleapis.com/auth/cloud-platform" ,
84+ "https://www.googleapis.com/auth/cloud-platform.read-only" ,
85+ ],
86+ )
87+
88+ assert options .api_endpoint == "foo.googleapis.com"
89+ assert options .client_cert_source () == (b"cert" , b"key" )
90+ assert options .quota_project_id == "quote-proj"
91+ assert options .api_key == "api-key"
92+ assert options .scopes == [
93+ "https://www.googleapis.com/auth/cloud-platform" ,
94+ "https://www.googleapis.com/auth/cloud-platform.read-only" ,
95+ ]
96+
97+
98+ def test_constructor_with_both_api_key_and_credentials_file ():
99+ with pytest .raises (ValueError ):
100+ client_options .ClientOptions (
101+ api_key = "api-key" , credentials_file = "path/to/credentials.json" ,
102+ )
103+
104+
75105def test_from_dict ():
76106 options = client_options .from_dict (
77107 {
@@ -94,6 +124,7 @@ def test_from_dict():
94124 "https://www.googleapis.com/auth/cloud-platform" ,
95125 "https://www.googleapis.com/auth/cloud-platform.read-only" ,
96126 ]
127+ assert options .api_key is None
97128
98129
99130def test_from_dict_bad_argument ():
@@ -112,6 +143,6 @@ def test_repr():
112143
113144 assert (
114145 repr (options )
115- == "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None}"
116- or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
146+ == "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None, 'api_key': None }"
147+ or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com', 'api_key': None }"
117148 )
0 commit comments