Skip to content

Commit

Permalink
chore: copy %%bigquery magics tests from google-cloud-bigquery (#8)
Browse files Browse the repository at this point in the history
* chore: copy magics unit tests from google-cloud-bigquery

* update tests to use bigquery_magics module

* remove auth from unit tests

* fix coverage
  • Loading branch information
tswast authored and ashleyxuu committed Apr 12, 2024
1 parent 7ddb606 commit 48c5fda
Show file tree
Hide file tree
Showing 12 changed files with 525 additions and 6 deletions.
9 changes: 3 additions & 6 deletions bigquery_magics/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@
from google.api_core import client_info
from google.api_core.exceptions import NotFound
from google.cloud import bigquery
from google.cloud.bigquery import exceptions
from google.cloud.bigquery import _versions_helpers, exceptions
from google.cloud.bigquery.dbapi import _helpers
from google.cloud.bigquery import _versions_helpers

from bigquery_magics import line_arg_parser as lap
import bigquery_magics.config
Expand Down Expand Up @@ -611,9 +610,7 @@ def _make_bqstorage_client(client, use_bqstorage_api, client_options):
return None

try:
_versions_helpers.BQ_STORAGE_VERSIONS.try_import(
raise_if_error=True
)
_versions_helpers.BQ_STORAGE_VERSIONS.try_import(raise_if_error=True)
except exceptions.BigQueryStorageNotFoundError as err:
customized_error = ImportError(
"The default BigQuery Storage API client cannot be used, install "
Expand Down Expand Up @@ -652,4 +649,4 @@ def _close_transports(client, bqstorage_client):
"""
client.close()
if bqstorage_client is not None:
bqstorage_client._transport.grpc_channel.close()
bqstorage_client._transport.grpc_channel.close()
13 changes: 13 additions & 0 deletions samples/snippets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
21 changes: 21 additions & 0 deletions samples/snippets/_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def strip_region_tags(sample_text: str) -> str:
"""Remove blank lines and region tags from sample text"""
magic_lines = [
line for line in sample_text.split("\n") if len(line) > 0 and "# [" not in line
]
return "\n".join(magic_lines)
44 changes: 44 additions & 0 deletions samples/snippets/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import typing
from typing import Iterator

import pytest

if typing.TYPE_CHECKING:
from IPython.core.interactiveshell import TerminalInteractiveShell

interactiveshell = pytest.importorskip("IPython.terminal.interactiveshell")
tools = pytest.importorskip("IPython.testing.tools")


@pytest.fixture(scope="session")
def ipython() -> "TerminalInteractiveShell":
config = tools.default_config()
config.TerminalInteractiveShell.simple_prompt = True
shell = interactiveshell.TerminalInteractiveShell.instance(config=config)
return shell


@pytest.fixture(autouse=True)
def ipython_interactive(
ipython: "TerminalInteractiveShell",
) -> Iterator["TerminalInteractiveShell"]:
"""Activate IPython's builtin hooks
for the duration of the test scope.
"""
with ipython.builtin_trap:
yield ipython
8 changes: 8 additions & 0 deletions samples/snippets/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mypy]
; We require type annotations in all samples.
strict = True
exclude = noxfile\.py
warn_unused_configs = True

[mypy-IPython.*,nox,noxfile_config,pandas]
ignore_missing_imports = True
Loading

0 comments on commit 48c5fda

Please sign in to comment.