Skip to content

Commit 1329f2d

Browse files
committed
Extract import patcher factory to test helpers
1 parent b323b20 commit 1329f2d

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

bigquery/tests/unit/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import mock
16+
import six
17+
1518

1619
def make_connection(*responses):
1720
import google.cloud.bigquery._http
@@ -22,3 +25,25 @@ def make_connection(*responses):
2225
mock_conn.user_agent = "testing 1.2.3"
2326
mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")]
2427
return mock_conn
28+
29+
30+
def maybe_fail_import(predicate):
31+
"""Create and return a patcher that conditionally makes an import fail.
32+
33+
Args:
34+
predicate (Callable[[...], bool]): A callable that, if it returns `True`,
35+
triggers an `ImportError`. It must accept the same arguments as the
36+
built-in `__import__` function.
37+
https://docs.python.org/3/library/functions.html#__import__
38+
39+
Returns:
40+
A mock patcher object that can be used to enable patched import behavior.
41+
"""
42+
orig_import = six.moves.builtins.__import__
43+
44+
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
45+
if predicate(name, globals, locals, fromlist, level):
46+
raise ImportError
47+
return orig_import(name, globals, locals, fromlist, level)
48+
49+
return mock.patch.object(six.moves.builtins, "__import__", new=custom_import)

bigquery/tests/unit/test_magics.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from google.cloud.bigquery import table
4343
from google.cloud.bigquery import magics
4444
from tests.unit.helpers import make_connection
45+
from tests.unit.helpers import maybe_fail_import
4546

4647

4748
pytestmark = pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
@@ -68,17 +69,14 @@ def ipython_interactive(request, ipython):
6869
@pytest.fixture(scope="session")
6970
def missing_bq_storage():
7071
"""Provide a patcher that can make the bigquery storage import to fail."""
71-
orig_import = six.moves.builtins.__import__
7272

73-
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
73+
def fail_if(name, globals, locals, fromlist, level):
7474
# NOTE: *very* simplified, assuming a straightforward absolute import
75-
if "bigquery_storage_v1beta1" in name or (
75+
return "bigquery_storage_v1beta1" in name or (
7676
fromlist is not None and "bigquery_storage_v1beta1" in fromlist
77-
):
78-
raise ImportError
79-
return orig_import(name, globals, locals, fromlist, level)
77+
)
8078

81-
return mock.patch.object(six.moves.builtins, "__import__", new=custom_import)
79+
return maybe_fail_import(predicate=fail_if)
8280

8381

8482
JOB_REFERENCE_RESOURCE = {"projectId": "its-a-project-eh", "jobId": "some-random-id"}

0 commit comments

Comments
 (0)