Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: Fix GBQ Tests #30478

Merged
merged 8 commits into from
Dec 27, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions pandas/tests/io/test_gbq.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import ExitStack as does_not_raise
from datetime import datetime
import os
import platform
Expand All @@ -21,7 +22,7 @@
DATASET_ID = "pydata_pandas_bq_testing_py3"

TABLE_ID = "new_test"
DESTINATION_TABLE = "{0}.{1}".format(DATASET_ID + "1", TABLE_ID)
DESTINATION_TABLE = f"{DATASET_ID + '1'}.{TABLE_ID}"

VERSION = platform.python_version()

Expand Down Expand Up @@ -149,33 +150,28 @@ def mock_read_gbq(sql, **kwargs):

@pytest.mark.single
class TestToGBQIntegrationWithServiceAccountKeyPath:
@classmethod
def setup_class(cls):
# - GLOBAL CLASS FIXTURES -
# put here any instruction you want to execute only *ONCE* *BEFORE*
# executing *ALL* tests described below.

@pytest.fixture()
def gbq_dataset(self):
# Setup Dataset
_skip_if_no_project_id()
_skip_if_no_private_key_path()

cls.client = _get_client()
cls.dataset = cls.client.dataset(DATASET_ID + "1")
self.client = _get_client()
jreback marked this conversation as resolved.
Show resolved Hide resolved
self.dataset = self.client.dataset(DATASET_ID + "1")
try:
# Clean-up previous test runs.
cls.client.delete_dataset(cls.dataset, delete_contents=True)
self.client.delete_dataset(self.dataset, delete_contents=True)
except api_exceptions.NotFound:
pass # It's OK if the dataset doesn't already exist.

cls.client.create_dataset(bigquery.Dataset(cls.dataset))
self.client.create_dataset(bigquery.Dataset(self.dataset))

yield

@classmethod
def teardown_class(cls):
# - GLOBAL CLASS FIXTURES -
# put here any instruction you want to execute only *ONCE* *AFTER*
# executing all tests.
cls.client.delete_dataset(cls.dataset, delete_contents=True)
# Teardown Dataset
self.client.delete_dataset(self.dataset, delete_contents=True)

def test_roundtrip(self):
def test_roundtrip(self, gbq_dataset):
destination_table = DESTINATION_TABLE + "1"

test_size = 20001
Expand All @@ -189,31 +185,38 @@ def test_roundtrip(self):
)

result = pd.read_gbq(
"SELECT COUNT(*) AS num_rows FROM {0}".format(destination_table),
f"SELECT COUNT(*) AS num_rows FROM {destination_table}",
project_id=_get_project_id(),
credentials=_get_credentials(),
dialect="standard",
)
assert result["num_rows"][0] == test_size

@pytest.mark.xfail(reason="Test breaking master")
@pytest.mark.xfail(reason="Test breaking master", strict=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to remove this xfail now

@pytest.mark.parametrize(
"if_exists, expected_num_rows",
[("append", 300), ("fail", 200), ("replace", 100)],
"if_exists, expected_num_rows, expectation",
[
("append", 300, does_not_raise()),
("fail", 200, pytest.raises(pandas_gbq.gbq.TableCreationError)),
("replace", 100, does_not_raise()),
],
)
def test_gbq_if_exists(self, if_exists, expected_num_rows):
def test_gbq_if_exists(
self, if_exists, expected_num_rows, expectation, gbq_dataset
):
# GH 29598
destination_table = DESTINATION_TABLE + "2"

test_size = 200
df = make_mixed_dataframe_v2(test_size)

df.to_gbq(
destination_table,
_get_project_id(),
chunksize=None,
credentials=_get_credentials(),
)
with expectation:
df.to_gbq(
destination_table,
_get_project_id(),
chunksize=None,
credentials=_get_credentials(),
)

df.iloc[:100].to_gbq(
destination_table,
Expand Down