1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import os
1516from typing import Optional
1617import unittest .mock as mock
1718
@@ -99,6 +100,33 @@ def assert_clients_w_user_agent(
99100 assert_constructed_w_user_agent (provider .resourcemanagerclient , expected_user_agent )
100101
101102
103+ def assert_constructed_wo_user_agent (
104+ mock_client : mock .Mock , not_expected_user_agent : str
105+ ):
106+ assert (
107+ not_expected_user_agent
108+ not in mock_client .call_args .kwargs ["client_info" ].to_user_agent ()
109+ )
110+
111+
112+ def assert_clients_wo_user_agent (
113+ provider : clients .ClientsProvider , not_expected_user_agent : str
114+ ):
115+ assert_constructed_wo_user_agent (provider .bqclient , not_expected_user_agent )
116+ assert_constructed_wo_user_agent (
117+ provider .bqconnectionclient , not_expected_user_agent
118+ )
119+ assert_constructed_wo_user_agent (
120+ provider .bqstoragereadclient , not_expected_user_agent
121+ )
122+ assert_constructed_wo_user_agent (
123+ provider .cloudfunctionsclient , not_expected_user_agent
124+ )
125+ assert_constructed_wo_user_agent (
126+ provider .resourcemanagerclient , not_expected_user_agent
127+ )
128+
129+
102130def test_user_agent_default (monkeypatch ):
103131 monkeypatch_client_constructors (monkeypatch )
104132 provider = create_clients_provider (application_name = None )
@@ -113,3 +141,43 @@ def test_user_agent_custom(monkeypatch):
113141 # We still need to include attribution to bigframes, even if there's also a
114142 # partner using the package.
115143 assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
144+
145+
146+ @mock .patch .dict (os .environ , {}, clear = True )
147+ def test_user_agent_not_in_vscode (monkeypatch ):
148+ monkeypatch_client_constructors (monkeypatch )
149+ provider = create_clients_provider ()
150+ assert_clients_wo_user_agent (provider , "vscode" )
151+
152+ # We still need to include attribution to bigframes
153+ assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
154+
155+
156+ @mock .patch .dict (os .environ , {"VSCODE_PID" : "12345" }, clear = True )
157+ def test_user_agent_in_vscode (monkeypatch ):
158+ monkeypatch_client_constructors (monkeypatch )
159+ provider = create_clients_provider ()
160+ assert_clients_w_user_agent (provider , "vscode" )
161+
162+ # We still need to include attribution to bigframes
163+ assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
164+
165+
166+ @mock .patch .dict (os .environ , {}, clear = True )
167+ def test_user_agent_not_in_jupyter (monkeypatch ):
168+ monkeypatch_client_constructors (monkeypatch )
169+ provider = create_clients_provider ()
170+ assert_clients_wo_user_agent (provider , "jupyter" )
171+
172+ # We still need to include attribution to bigframes
173+ assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
174+
175+
176+ @mock .patch .dict (os .environ , {"JPY_PARENT_PID" : "12345" }, clear = True )
177+ def test_user_agent_in_jupyter (monkeypatch ):
178+ monkeypatch_client_constructors (monkeypatch )
179+ provider = create_clients_provider ()
180+ assert_clients_w_user_agent (provider , "jupyter" )
181+
182+ # We still need to include attribution to bigframes
183+ assert_clients_w_user_agent (provider , f"bigframes/{ bigframes .version .__version__ } " )
0 commit comments