Skip to content

Commit 526ec32

Browse files
authored
TST: Fix pytest.raises usage for latest pytest. Fix warnings in tests. (#282)
Tests were failing because the `str` method for the context returned by `pytest.raises` no longer prints the contained exception. Instead, use `match=regex_value` to check for the desired error message.
1 parent 9460ad6 commit 526ec32

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

tests/system/test_gbq.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,26 +868,27 @@ def test_array_agg(self, project_id):
868868
),
869869
)
870870

871-
def test_array_of_floats(self, private_key_path, project_id):
871+
def test_array_of_floats(self, project_id):
872872
query = """select [1.1, 2.2, 3.3] as a, 4 as b"""
873873
df = gbq.read_gbq(
874874
query,
875875
project_id=project_id,
876-
private_key=private_key_path,
876+
credentials=self.credentials,
877877
dialect="standard",
878878
)
879879
tm.assert_frame_equal(
880880
df, DataFrame([[[1.1, 2.2, 3.3], 4]], columns=["a", "b"])
881881
)
882882

883-
def test_tokyo(self, tokyo_dataset, tokyo_table, private_key_path):
883+
def test_tokyo(self, tokyo_dataset, tokyo_table, project_id):
884884
df = gbq.read_gbq(
885885
"SELECT MAX(year) AS max_year FROM {}.{}".format(
886886
tokyo_dataset, tokyo_table
887887
),
888888
dialect="standard",
889889
location="asia-northeast1",
890-
private_key=private_key_path,
890+
project_id=project_id,
891+
credentials=self.credentials,
891892
)
892893
assert df["max_year"][0] >= 2000
893894

@@ -1401,7 +1402,17 @@ def test_upload_data_with_timestamp(self, project_id):
14011402
index=range(test_size),
14021403
columns=list("ABCD"),
14031404
)
1404-
df["times"] = np.datetime64("2018-03-13T05:40:45.348318Z")
1405+
df["times"] = pandas.Series(
1406+
[
1407+
"2018-03-13T05:40:45.348318",
1408+
"2018-04-13T05:40:45.348318",
1409+
"2018-05-13T05:40:45.348318",
1410+
"2018-06-13T05:40:45.348318",
1411+
"2018-07-13T05:40:45.348318",
1412+
"2018-08-13T05:40:45.348318",
1413+
],
1414+
dtype="datetime64[ns]",
1415+
).dt.tz_localize("UTC")
14051416

14061417
gbq.to_gbq(
14071418
df,
@@ -1421,7 +1432,7 @@ def test_upload_data_with_timestamp(self, project_id):
14211432

14221433
expected = df["times"].sort_values()
14231434
result = result_df["times"].sort_values()
1424-
tm.assert_numpy_array_equal(expected.values, result.values)
1435+
tm.assert_series_equal(expected, result)
14251436

14261437
def test_upload_data_with_different_df_and_user_schema(self, project_id):
14271438
df = tm.makeMixedDataFrame()

tests/unit/test_gbq.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ def test_to_gbq_with_no_project_id_given_should_fail(monkeypatch):
121121
pydata_google_auth, "default", mock_get_credentials_no_project
122122
)
123123

124-
with pytest.raises(ValueError) as exception:
124+
with pytest.raises(ValueError, match="Could not determine project ID"):
125125
gbq.to_gbq(DataFrame([[1]]), "dataset.tablename")
126-
assert "Could not determine project ID" in str(exception)
127126

128127

129128
def test_to_gbq_with_verbose_new_pandas_warns_deprecation(min_bq_version):
@@ -280,9 +279,8 @@ def test_read_gbq_with_no_project_id_given_should_fail(monkeypatch):
280279
pydata_google_auth, "default", mock_get_credentials_no_project
281280
)
282281

283-
with pytest.raises(ValueError) as exception:
282+
with pytest.raises(ValueError, match="Could not determine project ID"):
284283
gbq.read_gbq("SELECT 1", dialect="standard")
285-
assert "Could not determine project ID" in str(exception)
286284

287285

288286
def test_read_gbq_with_inferred_project_id(monkeypatch):
@@ -311,13 +309,12 @@ def test_read_gbq_with_inferred_project_id_from_service_account_credentials(
311309
def test_read_gbq_without_inferred_project_id_from_compute_engine_credentials(
312310
mock_compute_engine_credentials
313311
):
314-
with pytest.raises(ValueError) as exception:
312+
with pytest.raises(ValueError, match="Could not determine project ID"):
315313
gbq.read_gbq(
316314
"SELECT 1",
317315
dialect="standard",
318316
credentials=mock_compute_engine_credentials,
319317
)
320-
assert "Could not determine project ID" in str(exception)
321318

322319

323320
def test_read_gbq_with_invalid_private_key_json_should_fail():
@@ -469,9 +466,8 @@ def test_read_gbq_with_private_key_old_pandas_no_warnings(
469466

470467

471468
def test_read_gbq_with_invalid_dialect():
472-
with pytest.raises(ValueError) as excinfo:
469+
with pytest.raises(ValueError, match="is not valid for dialect"):
473470
gbq.read_gbq("SELECT 1", dialect="invalid")
474-
assert "is not valid for dialect" in str(excinfo.value)
475471

476472

477473
def test_generate_bq_schema_deprecated():

0 commit comments

Comments
 (0)