1818from unittest import mock
1919
2020import google .cloud .bigquery as bigquery
21+ import google .cloud .bigquery .job
22+ import google .cloud .bigquery .table
2123import pytest
2224
2325import bigframes
2426from bigframes .core import log_adapter
2527import bigframes .core .events
2628import bigframes .pandas as bpd
29+ import bigframes .session ._io .bigquery
2730import bigframes .session ._io .bigquery as io_bq
2831from bigframes .testing import mocks
2932
3235def mock_bq_client ():
3336 mock_client = mock .create_autospec (bigquery .Client )
3437 mock_query_job = mock .create_autospec (bigquery .QueryJob )
35- mock_row_iterator = mock .create_autospec (bigquery .table .RowIterator )
38+ mock_row_iterator = mock .create_autospec (google . cloud . bigquery .table .RowIterator )
3639
3740 mock_query_job .result .return_value = mock_row_iterator
3841
@@ -98,14 +101,12 @@ def test_create_job_configs_labels_log_adaptor_call_method_under_length_limit():
98101 cur_labels = {
99102 "source" : "bigquery-dataframes-temp" ,
100103 }
101- df = bpd .DataFrame (
102- {"col1" : [1 , 2 ], "col2" : [3 , 4 ]}, session = mocks .create_bigquery_session ()
103- )
104- # Test running two methods
105- df .head ()
106- df .max ()
107- df .columns
108- api_methods = log_adapter ._api_methods
104+ api_methods = [
105+ "dataframe-columns" ,
106+ "dataframe-max" ,
107+ "dataframe-head" ,
108+ "dataframe-__init__" ,
109+ ]
109110
110111 labels = io_bq .create_job_configs_labels (
111112 job_configs_labels = cur_labels , api_methods = api_methods
@@ -123,17 +124,13 @@ def test_create_job_configs_labels_log_adaptor_call_method_under_length_limit():
123124
124125def test_create_job_configs_labels_length_limit_met_and_labels_is_none ():
125126 log_adapter .get_and_reset_api_methods ()
126- df = bpd .DataFrame (
127- {"col1" : [1 , 2 ], "col2" : [3 , 4 ]}, session = mocks .create_bigquery_session ()
128- )
129127 # Test running methods more than the labels' length limit
130- for i in range (100 ):
131- df .head ()
132- api_methods = log_adapter ._api_methods
128+ api_methods = list (["dataframe-head" ] * 100 )
133129
134- labels = io_bq .create_job_configs_labels (
135- job_configs_labels = None , api_methods = api_methods
136- )
130+ with bpd .option_context ("compute.extra_query_labels" , {}):
131+ labels = io_bq .create_job_configs_labels (
132+ job_configs_labels = None , api_methods = api_methods
133+ )
137134 assert labels is not None
138135 assert len (labels ) == log_adapter .MAX_LABELS_COUNT
139136 assert "dataframe-head" in labels .values ()
@@ -150,17 +147,14 @@ def test_create_job_configs_labels_length_limit_met():
150147 value = f"test{ i } "
151148 cur_labels [key ] = value
152149 # If cur_labels length is 62, we can only add one label from api_methods
153- df = bpd .DataFrame (
154- {"col1" : [1 , 2 ], "col2" : [3 , 4 ]}, session = mocks .create_bigquery_session ()
155- )
156150 # Test running two methods
157- df .head ()
158- df .max ()
159- api_methods = log_adapter ._api_methods
151+ api_methods = ["dataframe-max" , "dataframe-head" ]
152+
153+ with bpd .option_context ("compute.extra_query_labels" , {}):
154+ labels = io_bq .create_job_configs_labels (
155+ job_configs_labels = cur_labels , api_methods = api_methods
156+ )
160157
161- labels = io_bq .create_job_configs_labels (
162- job_configs_labels = cur_labels , api_methods = api_methods
163- )
164158 assert labels is not None
165159 assert len (labels ) == 56
166160 assert "dataframe-max" in labels .values ()
@@ -184,7 +178,7 @@ def test_add_and_trim_labels_length_limit_met():
184178 {"col1" : [1 , 2 ], "col2" : [3 , 4 ]}, session = mocks .create_bigquery_session ()
185179 )
186180
187- job_config = bigquery .job .QueryJobConfig ()
181+ job_config = google . cloud . bigquery .job .QueryJobConfig ()
188182 job_config .labels = cur_labels
189183
190184 df .max ()
@@ -221,7 +215,7 @@ def test_start_query_with_client_labels_length_limit_met(
221215 {"col1" : [1 , 2 ], "col2" : [3 , 4 ]}, session = mocks .create_bigquery_session ()
222216 )
223217
224- job_config = bigquery .job .QueryJobConfig ()
218+ job_config = google . cloud . bigquery .job .QueryJobConfig ()
225219 job_config .labels = cur_labels
226220
227221 df .max ()
0 commit comments