Skip to content

Commit e193ccc

Browse files
committed
Make BigQuery tests more robust: use scoped environment variables and scoped HTTPRequestHandler so tests don't affect each other.
1 parent 4bd7c38 commit e193ccc

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

patches/sitecustomize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
4343
if kwargs.get('credentials') is not None:
4444
# The user wants to use their own credentials scheme, don't try to interfere.
4545
return bq_client(*args, **kwargs)
46-
print("Using connected BigQuery Account.")
46+
print("Using enabled BigQuery integration.")
4747
kwargs['credentials'] = KaggleKernelCredentials()
4848
kwargs['project'] = kwargs.get('project')
4949
return bq_client(

tests/test_bigquery.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
import unittest
22
import os
33
import threading
4+
from test.support import EnvironmentVarGuard
45

56
from http.server import BaseHTTPRequestHandler, HTTPServer
67

78
from google.cloud import bigquery
9+
from google.auth.exceptions import DefaultCredentialsError
810

911
HOSTNAME = "127.0.0.1"
1012
PORT = 8000
1113
URL = "http://%s:%s" % (HOSTNAME, PORT)
1214

1315
class TestBigQuery(unittest.TestCase):
14-
def _test_proxy(self, client):
16+
def _test_proxy(self, client, should_use_proxy):
17+
class HTTPHandler(BaseHTTPRequestHandler):
18+
called = False
19+
header_found = False
20+
21+
def do_HEAD(s):
22+
s.send_response(200)
23+
24+
def do_GET(s):
25+
HTTPHandler.called = True
26+
HTTPHandler.header_found = any(k for k in s.headers if k == "X-KAGGLE-PROXY-DATA" and s.headers[k] == "test-key")
27+
s.send_response(200)
28+
1529
with HTTPServer((HOSTNAME, PORT), HTTPHandler) as httpd:
1630
threading.Thread(target=httpd.serve_forever).start()
1731

@@ -21,25 +35,33 @@ def _test_proxy(self, client):
2135
pass
2236

2337
httpd.shutdown()
24-
self.assertTrue(HTTPHandler.called, msg="Fake server did not recieve a request from the BQ client.")
25-
self.assertTrue(HTTPHandler.header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ request.")
38+
if should_use_proxy:
39+
self.assertTrue(HTTPHandler.called, msg="Fake server did not recieve a request from the BQ client.")
40+
self.assertTrue(HTTPHandler.header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ request.")
41+
else:
42+
self.assertFalse(HTTPHandler.called, msg="Fake server was called from the BQ client, but should not have been.")
2643

2744
def test_proxy_kaggle_project(self):
2845
client = bigquery.Client(project='KAGGLE')
29-
self._test_proxy(client)
46+
self._test_proxy(client, should_use_proxy=True)
3047

3148
def test_proxy_no_project(self):
3249
client = bigquery.Client()
33-
self._test_proxy(client)
34-
35-
class HTTPHandler(BaseHTTPRequestHandler):
36-
called = False
37-
header_found = False
50+
self._test_proxy(client, should_use_proxy=True)
3851

39-
def do_HEAD(s):
40-
s.send_response(200)
52+
def test_project_with_connected_account(self):
53+
env = EnvironmentVarGuard()
54+
env.set('KAGGLE_BQ_USER_JWT', 'foobar')
55+
with env:
56+
client = bigquery.Client(project='ANOTHER_PROJECT')
57+
self._test_proxy(client, should_use_proxy=False)
4158

42-
def do_GET(s):
43-
HTTPHandler.called = True
44-
HTTPHandler.header_found = any(k for k in s.headers if k == "X-KAGGLE-PROXY-DATA" and s.headers[k] == "test-key")
45-
s.send_response(200)
59+
def test_no_project_with_connected_account(self):
60+
env = EnvironmentVarGuard()
61+
env.set('KAGGLE_BQ_USER_JWT', 'foobar')
62+
with env:
63+
with self.assertRaises(DefaultCredentialsError):
64+
# TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project
65+
# by the user or throw a custom exception.
66+
client = bigquery.Client()
67+
self._test_proxy(client, should_use_proxy=False)

0 commit comments

Comments
 (0)