11import unittest
22import os
33import threading
4+ from test .support import EnvironmentVarGuard
45
56from http .server import BaseHTTPRequestHandler , HTTPServer
67
78from google .cloud import bigquery
9+ from google .auth .exceptions import DefaultCredentialsError
810
911HOSTNAME = "127.0.0.1"
1012PORT = 8000
1113URL = "http://%s:%s" % (HOSTNAME , PORT )
1214
1315class 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