MCPcopy Create free account
hub / github.com/Kaggle/docker-python / TestBigQuery

Class TestBigQuery

tests/test_bigquery_proxy.py:18–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class TestBigQuery(unittest.TestCase):
19
20 def _test_proxy(self, client):
21 class HTTPHandler(BaseHTTPRequestHandler):
22 called = False
23 proxy_header_found = False
24
25 def do_HEAD(self):
26 self.send_response(200)
27
28 def do_GET(self):
29 HTTPHandler.called = True
30 HTTPHandler.proxy_header_found = any(
31 k for k in self.headers if k == "X-KAGGLE-PROXY-DATA" and self.headers[k] == "test-key")
32 self.send_response(200)
33 self.send_header("Content-type", "application/json")
34 self.end_headers()
35 sample_dataset = {
36 "id": "bigqueryproject:datasetname",
37 "datasetReference": {
38 "datasetId": "datasetname",
39 "projectId": "bigqueryproject"
40 }
41 }
42 self.wfile.write(json.dumps({"kind": "bigquery#datasetList", "datasets": [sample_dataset]}).encode("utf-8"))
43
44 server_address = urlparse(os.getenv('KAGGLE_DATA_PROXY_URL'))
45 with HTTPServer((server_address.hostname, server_address.port), HTTPHandler) as httpd:
46 threading.Thread(target=httpd.serve_forever).start()
47
48 for dataset in client.list_datasets():
49 self.assertEqual(dataset.dataset_id, "datasetname")
50
51 httpd.shutdown()
52 self.assertTrue(
53 HTTPHandler.called, msg="Fake server was not called from the BQ client, but should have been.")
54 self.assertTrue(
55 HTTPHandler.proxy_header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ proxy request.")
56
57 def test_proxy_using_library(self):
58 env = EnvironmentVarGuard()
59 env.unset('KAGGLE_USER_SECRETS_TOKEN')
60 with env:
61 client = PublicBigqueryClient()
62 self._test_proxy(client)
63
64 def test_proxy_no_project(self):
65 env = EnvironmentVarGuard()
66 env.unset('KAGGLE_USER_SECRETS_TOKEN')
67 with env:
68 client = bigquery.Client()
69 self._test_proxy(client)
70
71 def test_monkeypatching_idempotent(self):
72 env = EnvironmentVarGuard()
73 env.unset('KAGGLE_USER_SECRETS_TOKEN')
74 with env:
75 client1 = bigquery.Client

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected