-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_bigquery.py
More file actions
97 lines (76 loc) · 3.15 KB
/
Copy pathtest_bigquery.py
File metadata and controls
97 lines (76 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import pytest
def test_plugin_imports_without_google_cloud_bigquery(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
import builtins
def test_import() -> None:
original_import = builtins.__import__
blocked = {
"google.cloud.bigquery",
"google.api_core.client_options",
"google.auth.credentials",
}
def blocked_import(name, globals=None, locals=None, fromlist=(), level=0):
if name in blocked:
raise ModuleNotFoundError(name)
return original_import(name, globals, locals, fromlist, level)
builtins.__import__ = blocked_import
try:
import pytest_databases.docker.bigquery
finally:
builtins.__import__ = original_import
""")
result = pytester.runpytest_subprocess("-p", "pytest_databases", "-vv")
result.assert_outcomes(passed=1)
def test_service_fixture(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
import json
import urllib.request
pytest_plugins = ["pytest_databases.docker.bigquery"]
def run_bigquery(service, sql):
body = json.dumps({"query": sql, "useLegacySql": False}).encode("utf-8")
request = urllib.request.Request(
f"{service.endpoint}/bigquery/v2/projects/{service.project}/queries",
data=body,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(request, timeout=10) as response:
return json.loads(response.read().decode("utf-8"))
def test(bigquery_service) -> None:
payload = run_bigquery(bigquery_service, "SELECT 1 as one")
assert payload.get("jobComplete") is True
assert payload["rows"][0]["f"][0]["v"] == "1"
""")
result = pytester.runpytest_subprocess("-p", "pytest_databases")
result.assert_outcomes(passed=1)
def test_xdist(pytester: pytest.Pytester) -> None:
pytester.makepyfile("""
import json
import urllib.request
pytest_plugins = ["pytest_databases.docker.bigquery"]
def run_bigquery(service, sql):
body = json.dumps({"query": sql, "useLegacySql": False}).encode("utf-8")
request = urllib.request.Request(
f"{service.endpoint}/bigquery/v2/projects/{service.project}/queries",
data=body,
headers={"Content-Type": "application/json"},
method="POST",
)
with urllib.request.urlopen(request, timeout=30) as response:
return json.loads(response.read().decode("utf-8"))
def test_one(bigquery_service) -> None:
run_bigquery(
bigquery_service,
f"CREATE TABLE `{bigquery_service.dataset}.test_one` AS SELECT 1 AS the_value",
)
def test_two(bigquery_service) -> None:
run_bigquery(
bigquery_service,
f"CREATE TABLE `{bigquery_service.dataset}.test_two` AS SELECT 1 AS the_value",
)
""")
result = pytester.runpytest_subprocess("-p", "pytest_databases", "-n", "2")
result.assert_outcomes(passed=2)