Skip to content

Commit f8f4219

Browse files
committed
HK: Tidying and minor fixes.
1 parent 372cb01 commit f8f4219

File tree

8 files changed

+66
-21
lines changed

8 files changed

+66
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pytest-bq"
3-
version = "0.0.3"
3+
version = "0.0.4"
44
description = "BigQuery fixtures and fixture factories for Pytest."
55
readme = "README.md"
66
keywords = ["tests", "pytest", "fixture", "bq"]

pytest_bq/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
"""pytest-bq plugin."""
2-
__version__ = "0.0.3"
2+
3+
__version__ = "0.0.4"

pytest_bq/executor/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
"""Plugin executors."""
2+
23
from .process import BQExecutor # noqa

pytest_bq/executor/process.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Starts a local BQ server using the targeted configuration.
44
"""
5+
56
from pathlib import Path
67
from typing import List, Optional
78

@@ -17,15 +18,20 @@ def __init__(
1718
project_id: str,
1819
port: int,
1920
grpc_port: int,
21+
database_file_path: Path,
2022
data_from_yaml: Optional[str] = None,
2123
loglevel: Optional[str] = None,
2224
) -> None:
2325
"""Start up local BQ.
2426
2527
Args:
2628
executable: executable to call.
27-
host: host address fixture will be started on.
28-
port: port fixture will listen on.
29+
project_id: google project ID to start local emulator with
30+
port: api port fixture will listen on.
31+
grpc_port: port storage api fixture will listen on.
32+
33+
Kwargs:
34+
database_file_path: bigquery-emulator config file path.
2935
loglevel: log level passed to `fake-BQ-server` binary.
3036
3137
Returns:
@@ -39,6 +45,8 @@ def __init__(
3945
str(port),
4046
"--grpc-port",
4147
str(grpc_port),
48+
"--database",
49+
str(database_file_path),
4250
]
4351

4452
if loglevel:
@@ -49,12 +57,17 @@ def __init__(
4957
command.extend(["--data-from-yaml", data_from_yaml])
5058

5159
self._project_id = project_id
60+
self._port = port
61+
self._grpc_port = grpc_port
5262
self._starting_command = command
5363
self.executable = executable
5464

5565
super().__init__(
5666
# TODO: Figure out why this returns a 500?
57-
command, url=f"http://localhost:{port}/bigquery/v2/projects", timeout=5, status="500"
67+
command,
68+
url=f"http://localhost:{port}/bigquery/v2/projects",
69+
timeout=5,
70+
status="500",
5871
)
5972

6073
def start(self) -> "BQExecutor":
@@ -64,5 +77,15 @@ def start(self) -> "BQExecutor":
6477

6578
@property
6679
def project_id(self) -> str:
67-
"""Return the configured project_id the emulator is running under."""
80+
"""Configured project_id the emulator is running under."""
6881
return self._project_id
82+
83+
@property
84+
def bq_api_port(self) -> int:
85+
"""Configured port the bigquery api emulator is running on."""
86+
return self._port
87+
88+
@property
89+
def storage_api_port(self) -> int:
90+
"""Configured port the storage api is running under."""
91+
return self._grpc_port

pytest_bq/factories/proc.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""bq process factory."""
2+
3+
import logging
24
from pathlib import Path
35
from typing import Callable, Generator, Optional
46

@@ -10,6 +12,8 @@
1012
from pytest_bq.config import get_config
1113
from pytest_bq.executor import BQExecutor
1214

15+
log = logging.getLogger(__name__)
16+
1317

1418
def bq_proc(
1519
executable: Optional[str] = None,
@@ -47,22 +51,26 @@ def bq_proc_fixture(
4751
bq_project_id = project_id or config["project_id"]
4852
assert bq_project_id, "Project ID is required."
4953

50-
bq_port = (
51-
get_port(port) if port else get_port(config["port"]) or get_port(None)
52-
)
54+
bq_port = get_port(port) if port else get_port(config["port"]) or get_port(None)
5355
assert bq_port, "Unable to find a port available."
5456
bq_grpc_port = (
55-
get_port(grpc_port) if grpc_port else get_port(config["grpc_port"]) or get_port(None)
57+
get_port(grpc_port)
58+
if grpc_port
59+
else get_port(config["grpc_port"]) or get_port(None)
5660
)
5761
assert bq_grpc_port, "Unable to find a port available."
5862

63+
database_file = Path(tmp_path_factory.mktemp("db")) / "sqllite.db"
64+
log.debug("Using sqllite db at path %r", database_file)
65+
5966
bq_executor = BQExecutor(
6067
executable=Path(bq_exec),
6168
project_id=bq_project_id,
6269
data_from_yaml=data_from_yaml,
6370
port=bq_port,
6471
grpc_port=bq_grpc_port,
6572
loglevel=loglevel or config["loglevel"],
73+
database_file_path=database_file,
6674
)
6775
with bq_executor:
6876
yield bq_executor

pytest_bq/plugin.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""pytest-bq plugin declaration."""
2+
23
from shutil import which
34

45
from pytest import Parser
@@ -20,9 +21,7 @@
2021

2122
def pytest_addoption(parser: Parser) -> None:
2223
"""Plugin configuration options."""
23-
parser.addini(
24-
name="bq_executable", help=_HELP_EXEC, default=which("bigquery")
25-
)
24+
parser.addini(name="bq_executable", help=_HELP_EXEC, default=which("bigquery"))
2625
parser.addini(name="bq_port", help=_HELP_PORT)
2726
parser.addini(name="bq_grpc_port", help=_HELP_GRPC_PORT)
2827
parser.addini(name="bq_loglevel", help=_HELP_LOGLEVEL)
@@ -33,13 +32,18 @@ def pytest_addoption(parser: Parser) -> None:
3332
"--bq-executable", action="store", dest="bq_executable", help=_HELP_EXEC
3433
)
3534
parser.addoption("--bq-port", action="store", dest="bq_port", help=_HELP_PORT)
36-
parser.addoption("--bq-grpc-port", action="store", dest="bq_grpc_port", help=_HELP_GRPC_PORT)
35+
parser.addoption(
36+
"--bq-grpc-port", action="store", dest="bq_grpc_port", help=_HELP_GRPC_PORT
37+
)
3738
parser.addoption(
3839
"--bq-loglevel", action="store", dest="bq_loglevel", help=_HELP_LOGLEVEL
3940
)
4041
parser.addoption(
4142
"--bq-project-id", action="store", dest="bq_project_id", help=_HELP_PROJECT_ID
4243
)
4344
parser.addoption(
44-
"--bq-data-from-yaml", action="store", dest="bq_data_from_yaml", help=_HELP_DATA_FROM_YAML
45+
"--bq-data-from-yaml",
46+
action="store",
47+
dest="bq_data_from_yaml",
48+
help=_HELP_DATA_FROM_YAML,
4549
)

tests/conftest.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests main conftest file."""
2+
23
import subprocess
34
import warnings
45

@@ -11,15 +12,21 @@
1112
module="(_pytest|pytest|google|path|mirakuru).*",
1213
)
1314
executable_path = (
14-
subprocess.run("which bigquery-emulator", capture_output=True, shell=True).stdout
15-
.decode()
16-
.strip('\n')
15+
subprocess.run("which bigquery-emulator", capture_output=True, shell=True)
16+
.stdout.decode()
17+
.strip("\n")
1718
)
1819

1920

20-
bq_proc = pytest_bq.factories.proc.bq_proc(executable=executable_path, project_id='test-base')
21+
bq_proc = pytest_bq.factories.proc.bq_proc(
22+
executable=executable_path, project_id="test-base"
23+
)
2124
bqlocal = pytest_bq.factories.client.bqlocal("bq_proc")
2225

2326

24-
bq_proc1 = pytest_bq.factories.proc.bq_proc(executable=executable_path, project_id='test-data', data_from_yaml='./tests/resources/test_data.yaml')
27+
bq_proc1 = pytest_bq.factories.proc.bq_proc(
28+
executable=executable_path,
29+
project_id="test-data",
30+
data_from_yaml="./tests/resources/test_data.yaml",
31+
)
2532
bqlocal1 = pytest_bq.factories.client.bqlocal("bq_proc1")

tests/test_bq.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""General plugin tests."""
2+
23
import pytest
34
from google.cloud import bigquery
45

@@ -32,4 +33,4 @@ def test_can_load_test_data_yaml(bqlocal1: bigquery.Client) -> None:
3233
job_config=bigquery.QueryJobConfig(),
3334
)
3435
results = [x.values() for x in job.result()]
35-
assert results == [(1, ), (2, )]
36+
assert results == [(1,), (2,)]

0 commit comments

Comments
 (0)