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

chore: Reduce duplicated code betweem tests/unit and tests/unit/job #940

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 3 additions & 19 deletions tests/unit/job/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,20 @@

import unittest

import mock
from google.api_core import exceptions


def _make_credentials():
import google.auth.credentials

return mock.Mock(spec=google.auth.credentials.Credentials)
from ..helpers import make_connection, make_client as __make_client


def _make_client(project="test-project", connection=None):
from google.cloud.bigquery.client import Client

client = __make_client(project)
if connection is None:
connection = _make_connection()
connection = make_connection()

client = Client(project=project, credentials=_make_credentials(), _http=object())
client._connection = connection
return client


def _make_connection(*responses):
import google.cloud.bigquery._http
from google.cloud.exceptions import NotFound

mock_conn = mock.create_autospec(google.cloud.bigquery._http.Connection)
mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")]
return mock_conn


def _make_retriable_exception():
return exceptions.TooManyRequests(
"retriable exception", errors=[{"reason": "rateLimitExceeded"}]
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/job/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import mock
import pytest

from ..helpers import make_connection

from .helpers import _make_client
from .helpers import _make_connection
from .helpers import _make_retriable_exception
from .helpers import _make_job_resource

Expand Down Expand Up @@ -740,7 +741,7 @@ def test_cancel_defaults(self):
response = {"job": resource}
job = self._set_properties_job()
job._properties["jobReference"]["location"] = self.LOCATION
connection = job._client._connection = _make_connection(response)
connection = job._client._connection = make_connection(response)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
Expand Down Expand Up @@ -769,7 +770,7 @@ def test_cancel_explicit(self):
response = {"job": resource}
job = self._set_properties_job()
client = _make_client(project=other_project)
connection = client._connection = _make_connection(response)
connection = client._connection = make_connection(response)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
Expand Down Expand Up @@ -930,7 +931,7 @@ def test_result_default_wo_state(self):
started=True,
ended=True,
)
conn = _make_connection(
conn = make_connection(
_make_retriable_exception(),
begun_job_resource,
_make_retriable_exception(),
Expand Down Expand Up @@ -968,7 +969,7 @@ def test_result_w_retry_wo_state(self):
started=True,
ended=True,
)
conn = _make_connection(
conn = make_connection(
exceptions.NotFound("not normally retriable"),
begun_job_resource,
exceptions.NotFound("not normally retriable"),
Expand Down Expand Up @@ -1008,7 +1009,7 @@ def test_result_w_retry_wo_state(self):
)

def test_result_explicit_w_state(self):
conn = _make_connection()
conn = make_connection()
client = _make_client(project=self.PROJECT, connection=conn)
job = self._make_one(self.JOB_ID, client)
# Use _set_properties() instead of directly modifying _properties so
Expand Down
21 changes: 11 additions & 10 deletions tests/unit/job/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import mock

from ..helpers import make_connection

from .helpers import _Base
from .helpers import _make_client
from .helpers import _make_connection


class TestCopyJobConfig(_Base):
Expand Down Expand Up @@ -333,7 +334,7 @@ def test_begin_w_bound_client(self):
del RESOURCE["etag"]
del RESOURCE["selfLink"]
del RESOURCE["user_email"]
conn = _make_connection(RESOURCE)
conn = make_connection(RESOURCE)
client = _make_client(project=self.PROJECT, connection=conn)
source = self._table_ref(self.SOURCE_TABLE)
destination = self._table_ref(self.DESTINATION_TABLE)
Expand Down Expand Up @@ -396,9 +397,9 @@ def test_begin_w_alternate_client(self):
"writeDisposition": WriteDisposition.WRITE_TRUNCATE,
}
RESOURCE["configuration"]["copy"] = COPY_CONFIGURATION
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection(RESOURCE)
conn2 = make_connection(RESOURCE)
client2 = _make_client(project=self.PROJECT, connection=conn2)
source = self._table_ref(self.SOURCE_TABLE)
destination = self._table_ref(self.DESTINATION_TABLE)
Expand Down Expand Up @@ -427,7 +428,7 @@ def test_begin_w_alternate_client(self):

def test_exists_miss_w_bound_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
conn = _make_connection()
conn = make_connection()
client = _make_client(project=self.PROJECT, connection=conn)

source = self._table_ref(self.SOURCE_TABLE)
Expand All @@ -446,9 +447,9 @@ def test_exists_miss_w_bound_client(self):

def test_exists_hit_w_alternate_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection({})
conn2 = make_connection({})
client2 = _make_client(project=self.PROJECT, connection=conn2)
source = self._table_ref(self.SOURCE_TABLE)
destination = self._table_ref(self.DESTINATION_TABLE)
Expand All @@ -468,7 +469,7 @@ def test_exists_hit_w_alternate_client(self):
def test_reload_w_bound_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
RESOURCE = self._make_resource()
conn = _make_connection(RESOURCE)
conn = make_connection(RESOURCE)
client = _make_client(project=self.PROJECT, connection=conn)
source = self._table_ref(self.SOURCE_TABLE)
destination = self._table_ref(self.DESTINATION_TABLE)
Expand All @@ -488,9 +489,9 @@ def test_reload_w_bound_client(self):
def test_reload_w_alternate_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
RESOURCE = self._make_resource()
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection(RESOURCE)
conn2 = make_connection(RESOURCE)
client2 = _make_client(project=self.PROJECT, connection=conn2)
source = self._table_ref(self.SOURCE_TABLE)
destination = self._table_ref(self.DESTINATION_TABLE)
Expand Down
21 changes: 11 additions & 10 deletions tests/unit/job/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import mock

from ..helpers import make_connection

from .helpers import _Base
from .helpers import _make_client
from .helpers import _make_connection


class TestExtractJobConfig(_Base):
Expand Down Expand Up @@ -265,7 +266,7 @@ def test_begin_w_bound_client(self):
del RESOURCE["etag"]
del RESOURCE["selfLink"]
del RESOURCE["user_email"]
conn = _make_connection(RESOURCE)
conn = make_connection(RESOURCE)
client = _make_client(project=self.PROJECT, connection=conn)
source_dataset = DatasetReference(self.PROJECT, self.DS_ID)
source = source_dataset.table(self.SOURCE_TABLE)
Expand Down Expand Up @@ -318,9 +319,9 @@ def test_begin_w_alternate_client(self):
"printHeader": False,
}
RESOURCE["configuration"]["extract"] = EXTRACT_CONFIGURATION
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection(RESOURCE)
conn2 = make_connection(RESOURCE)
client2 = _make_client(project=self.PROJECT, connection=conn2)
source_dataset = DatasetReference(self.PROJECT, self.DS_ID)
source = source_dataset.table(self.SOURCE_TABLE)
Expand Down Expand Up @@ -353,7 +354,7 @@ def test_begin_w_alternate_client(self):

def test_exists_miss_w_bound_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
conn = _make_connection()
conn = make_connection()
client = _make_client(project=self.PROJECT, connection=conn)
job = self._make_one(
self.JOB_ID, self.TABLE_REF, [self.DESTINATION_URI], client
Expand All @@ -371,9 +372,9 @@ def test_exists_miss_w_bound_client(self):

def test_exists_hit_w_alternate_client(self):
PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection({})
conn2 = make_connection({})
client2 = _make_client(project=self.PROJECT, connection=conn2)
job = self._make_one(
self.JOB_ID, self.TABLE_REF, [self.DESTINATION_URI], client1
Expand All @@ -395,7 +396,7 @@ def test_reload_w_bound_client(self):

PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
RESOURCE = self._make_resource()
conn = _make_connection(RESOURCE)
conn = make_connection(RESOURCE)
client = _make_client(project=self.PROJECT, connection=conn)
source_dataset = DatasetReference(self.PROJECT, self.DS_ID)
source = source_dataset.table(self.SOURCE_TABLE)
Expand All @@ -416,9 +417,9 @@ def test_reload_w_alternate_client(self):

PATH = "/projects/%s/jobs/%s" % (self.PROJECT, self.JOB_ID)
RESOURCE = self._make_resource()
conn1 = _make_connection()
conn1 = make_connection()
client1 = _make_client(project=self.PROJECT, connection=conn1)
conn2 = _make_connection(RESOURCE)
conn2 = make_connection(RESOURCE)
client2 = _make_client(project=self.PROJECT, connection=conn2)
source_dataset = DatasetReference(self.PROJECT, self.DS_ID)
source = source_dataset.table(self.SOURCE_TABLE)
Expand Down
Loading