Skip to content

Commit

Permalink
TST: Fix pytest.raises usage for latest pytest. Fix warnings in tests. (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
tswast authored Jul 22, 2019
1 parent 9460ad6 commit 526ec32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
23 changes: 17 additions & 6 deletions tests/system/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,26 +868,27 @@ def test_array_agg(self, project_id):
),
)

def test_array_of_floats(self, private_key_path, project_id):
def test_array_of_floats(self, project_id):
query = """select [1.1, 2.2, 3.3] as a, 4 as b"""
df = gbq.read_gbq(
query,
project_id=project_id,
private_key=private_key_path,
credentials=self.credentials,
dialect="standard",
)
tm.assert_frame_equal(
df, DataFrame([[[1.1, 2.2, 3.3], 4]], columns=["a", "b"])
)

def test_tokyo(self, tokyo_dataset, tokyo_table, private_key_path):
def test_tokyo(self, tokyo_dataset, tokyo_table, project_id):
df = gbq.read_gbq(
"SELECT MAX(year) AS max_year FROM {}.{}".format(
tokyo_dataset, tokyo_table
),
dialect="standard",
location="asia-northeast1",
private_key=private_key_path,
project_id=project_id,
credentials=self.credentials,
)
assert df["max_year"][0] >= 2000

Expand Down Expand Up @@ -1401,7 +1402,17 @@ def test_upload_data_with_timestamp(self, project_id):
index=range(test_size),
columns=list("ABCD"),
)
df["times"] = np.datetime64("2018-03-13T05:40:45.348318Z")
df["times"] = pandas.Series(
[
"2018-03-13T05:40:45.348318",
"2018-04-13T05:40:45.348318",
"2018-05-13T05:40:45.348318",
"2018-06-13T05:40:45.348318",
"2018-07-13T05:40:45.348318",
"2018-08-13T05:40:45.348318",
],
dtype="datetime64[ns]",
).dt.tz_localize("UTC")

gbq.to_gbq(
df,
Expand All @@ -1421,7 +1432,7 @@ def test_upload_data_with_timestamp(self, project_id):

expected = df["times"].sort_values()
result = result_df["times"].sort_values()
tm.assert_numpy_array_equal(expected.values, result.values)
tm.assert_series_equal(expected, result)

def test_upload_data_with_different_df_and_user_schema(self, project_id):
df = tm.makeMixedDataFrame()
Expand Down
12 changes: 4 additions & 8 deletions tests/unit/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ def test_to_gbq_with_no_project_id_given_should_fail(monkeypatch):
pydata_google_auth, "default", mock_get_credentials_no_project
)

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


def test_to_gbq_with_verbose_new_pandas_warns_deprecation(min_bq_version):
Expand Down Expand Up @@ -280,9 +279,8 @@ def test_read_gbq_with_no_project_id_given_should_fail(monkeypatch):
pydata_google_auth, "default", mock_get_credentials_no_project
)

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


def test_read_gbq_with_inferred_project_id(monkeypatch):
Expand Down Expand Up @@ -311,13 +309,12 @@ def test_read_gbq_with_inferred_project_id_from_service_account_credentials(
def test_read_gbq_without_inferred_project_id_from_compute_engine_credentials(
mock_compute_engine_credentials
):
with pytest.raises(ValueError) as exception:
with pytest.raises(ValueError, match="Could not determine project ID"):
gbq.read_gbq(
"SELECT 1",
dialect="standard",
credentials=mock_compute_engine_credentials,
)
assert "Could not determine project ID" in str(exception)


def test_read_gbq_with_invalid_private_key_json_should_fail():
Expand Down Expand Up @@ -469,9 +466,8 @@ def test_read_gbq_with_private_key_old_pandas_no_warnings(


def test_read_gbq_with_invalid_dialect():
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError, match="is not valid for dialect"):
gbq.read_gbq("SELECT 1", dialect="invalid")
assert "is not valid for dialect" in str(excinfo.value)


def test_generate_bq_schema_deprecated():
Expand Down

0 comments on commit 526ec32

Please sign in to comment.