Skip to content

Commit e688b0e

Browse files
authored
Move maybe_fail_import() to top level test utils (googleapis#8840)
* Move maybe_fail_import() to top level test utils * Install local test utils in multiple nox sessions
1 parent ab3179d commit e688b0e

4 files changed

Lines changed: 44 additions & 27 deletions

File tree

bigquery/noxfile.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import nox
2121

2222

23-
LOCAL_DEPS = (os.path.join("..", "api_core[grpc]"), os.path.join("..", "core"))
23+
LOCAL_DEPS = (
24+
os.path.join("..", "api_core[grpc]"),
25+
os.path.join("..", "core"),
26+
os.path.join("..", "test_utils"),
27+
)
2428

2529
BLACK_PATHS = ("docs", "google", "samples", "tests", "noxfile.py", "setup.py")
2630

bigquery/tests/unit/helpers.py

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

15-
import mock
16-
import six
17-
1815

1916
def make_connection(*responses):
2017
import google.cloud.bigquery._http
@@ -25,25 +22,3 @@ def make_connection(*responses):
2522
mock_conn.user_agent = "testing 1.2.3"
2623
mock_conn.api_request.side_effect = list(responses) + [NotFound("miss")]
2724
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +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
45+
from test_utils.imports import maybe_fail_import
4646

4747

4848
pytestmark = pytest.mark.skipif(IPython is None, reason="Requires `ipython`")

test_utils/test_utils/imports.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import mock
16+
import six
17+
18+
19+
def maybe_fail_import(predicate):
20+
"""Create and return a patcher that conditionally makes an import fail.
21+
22+
Args:
23+
predicate (Callable[[...], bool]): A callable that, if it returns `True`,
24+
triggers an `ImportError`. It must accept the same arguments as the
25+
built-in `__import__` function.
26+
https://docs.python.org/3/library/functions.html#__import__
27+
28+
Returns:
29+
A mock patcher object that can be used to enable patched import behavior.
30+
"""
31+
orig_import = six.moves.builtins.__import__
32+
33+
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
34+
if predicate(name, globals, locals, fromlist, level):
35+
raise ImportError
36+
return orig_import(name, globals, locals, fromlist, level)
37+
38+
return mock.patch.object(six.moves.builtins, "__import__", new=custom_import)

0 commit comments

Comments
 (0)