FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docker-python/tests/test_bigquery.py at master · HelWireless/docker-python · GitHub
HelWireless
/
docker-python
Public
forked from
Kaggle/docker-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
docker-python
/
tests
/
test_bigquery.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
86 lines (71 loc) · 3.51 KB
Breadcrumbs
docker-python
/
tests
/
test_bigquery.py
Copy path
File metadata and controls
86 lines (71 loc) · 3.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
unittest
import
os
import
threading
from
test
.
support
import
EnvironmentVarGuard
from
urllib
.
parse
import
urlparse
from
http
.
server
import
BaseHTTPRequestHandler
,
HTTPServer
from
google
.
cloud
import
bigquery
from
google
.
auth
.
exceptions
import
DefaultCredentialsError
from
kaggle_gcp
import
KaggleKernelCredentials
,
PublicBigqueryClient
class
TestBigQuery
(
unittest
.
TestCase
):
def
_test_proxy
(
self
,
client
,
should_use_proxy
):
class
HTTPHandler
(
BaseHTTPRequestHandler
):
called
=
False
header_found
=
False
def
do_HEAD
(
s
):
s
.
send_response
(
200
)
def
do_GET
(
s
):
HTTPHandler
.
called
=
True
HTTPHandler
.
header_found
=
any
(
k
for
k
in
s
.
headers
if
k
==
"X-KAGGLE-PROXY-DATA"
and
s
.
headers
[
k
]
==
"test-key"
)
s
.
send_response
(
200
)
server_address
=
urlparse
(
os
.
getenv
(
'KAGGLE_DATA_PROXY_URL'
))
with
HTTPServer
((
server_address
.
hostname
,
server_address
.
port
),
HTTPHandler
)
as
httpd
:
threading
.
Thread
(
target
=
httpd
.
serve_forever
).
start
()
try
:
for
ds
in
client
.
list_datasets
():
pass
except
:
pass
httpd
.
shutdown
()
if
should_use_proxy
:
self
.
assertTrue
(
HTTPHandler
.
called
,
msg
=
"Fake server did not receive a request from the BQ client."
)
self
.
assertTrue
(
HTTPHandler
.
header_found
,
msg
=
"X-KAGGLE-PROXY-DATA header was missing from the BQ request."
)
else
:
self
.
assertFalse
(
HTTPHandler
.
called
,
msg
=
"Fake server was called from the BQ client, but should not have been."
)
def
test_proxy_using_library
(
self
):
env
=
EnvironmentVarGuard
()
env
.
unset
(
'KAGGLE_BQ_USER_JWT'
)
with
env
:
client
=
PublicBigqueryClient
()
self
.
_test_proxy
(
client
,
should_use_proxy
=
True
)
def
test_proxy_no_project
(
self
):
env
=
EnvironmentVarGuard
()
env
.
unset
(
'KAGGLE_BQ_USER_JWT'
)
with
env
:
client
=
bigquery
.
Client
()
self
.
_test_proxy
(
client
,
should_use_proxy
=
True
)
def
test_project_with_connected_account
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_BQ_USER_JWT'
,
'foobar'
)
with
env
:
client
=
bigquery
.
Client
(
project
=
'ANOTHER_PROJECT'
,
credentials
=
KaggleKernelCredentials
())
self
.
_test_proxy
(
client
,
should_use_proxy
=
False
)
def
test_simultaneous_clients
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_BQ_USER_JWT'
,
'foobar'
)
with
env
:
proxy_client
=
bigquery
.
Client
()
self
.
_test_proxy
(
proxy_client
,
should_use_proxy
=
True
)
bq_client
=
bigquery
.
Client
(
project
=
'ANOTHER_PROJECT'
,
credentials
=
KaggleKernelCredentials
())
self
.
_test_proxy
(
bq_client
,
should_use_proxy
=
False
)
# Verify that proxy client is still going to proxy to ensure global Connection
# isn't being modified.
self
.
_test_proxy
(
proxy_client
,
should_use_proxy
=
True
)
def
test_no_project_with_connected_account
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_BQ_USER_JWT'
,
'foobar'
)
with
env
:
with
self
.
assertRaises
(
DefaultCredentialsError
):
# TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project
# by the user or throw a custom exception.
client
=
bigquery
.
Client
(
credentials
=
KaggleKernelCredentials
())
self
.
_test_proxy
(
client
,
should_use_proxy
=
False
)
Back
|
FazBrowse Home
|
New Git URL