Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.

Commit bbd0741

Browse files
committed
Fix unit tests
1 parent 78e1d85 commit bbd0741

3 files changed

Lines changed: 10 additions & 39 deletions

File tree

bigquery_magics/graph_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def handle_post_ping(self):
251251
self.do_data_response({"your_request": data})
252252

253253
def handle_post_query(self):
254+
self.parse_post_data()
254255
query_results = json.loads(graph_server.query_result.to_json())
255256
response = convert_graph_data(query_results=query_results)
256257
self.do_data_response(response)

tests/unit/bigquery/test_bigquery.py

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -680,18 +680,10 @@ def test_bigquery_graph_json_result(monkeypatch):
680680
html_content = display_mock.call_args_list[0][0][0].data
681681
assert "<script>" in html_content
682682
assert "</script>" in html_content
683-
# Verify that the query results are embedded into the HTML, allowing them to be visualized.
684-
# Due to escaping, it is not possible check for graph_json_rows exactly, so we check for a few
685-
# sentinel strings within the query results, instead.
683+
# Sanity check that query results are not embedded into the HTML.
686684
assert (
687-
"mUZpbkdyYXBoLlBlcnNvbgB4kQI=" in html_content
685+
"mUZpbkdyYXBoLlBlcnNvbgB4kQI=" not in html_content
688686
) # identifier in 1st row of query result
689-
assert (
690-
"mUZpbkdyYXBoLlBlcnNvbgB4kQY=" in html_content
691-
) # identifier in 2nd row of query result
692-
assert (
693-
"mUZpbkdyYXBoLlBlcnNvbgB4kQQ=" in html_content
694-
) # identifier in 3rd row of query result
695687

696688
# Make sure we can run a second graph query, after the graph server is already running.
697689
try:
@@ -704,18 +696,6 @@ def test_bigquery_graph_json_result(monkeypatch):
704696
html_content = display_mock.call_args_list[0][0][0].data
705697
assert "<script>" in html_content
706698
assert "</script>" in html_content
707-
# Verify that the query results are embedded into the HTML, allowing them to be visualized.
708-
# Due to escaping, it is not possible check for graph_json_rows exactly, so we check for a few
709-
# sentinel strings within the query results, instead.
710-
assert (
711-
"mUZpbkdyYXBoLlBlcnNvbgB4kQI=" in html_content
712-
) # identifier in 1st row of query result
713-
assert (
714-
"mUZpbkdyYXBoLlBlcnNvbgB4kQY=" in html_content
715-
) # identifier in 2nd row of query result
716-
assert (
717-
"mUZpbkdyYXBoLlBlcnNvbgB4kQQ=" in html_content
718-
) # identifier in 3rd row of query result
719699

720700
assert bqstorage_mock.called # BQ storage client was used
721701
assert isinstance(return_value, pandas.DataFrame)
@@ -791,18 +771,10 @@ def test_bigquery_graph_colab(monkeypatch):
791771
html_content = display_mock.call_args_list[0][0][0].data
792772
assert "<script>" in html_content
793773
assert "</script>" in html_content
794-
# Verify that the query results are embedded into the HTML, allowing them to be visualized.
795-
# Due to escaping, it is not possible check for graph_json_rows exactly, so we check for a few
796-
# sentinel strings within the query results, instead.
774+
# Verify that the query results are not embedded into the HTML.
797775
assert (
798-
"mUZpbkdyYXBoLlBlcnNvbgB4kQI=" in html_content
776+
"mUZpbkdyYXBoLlBlcnNvbgB4kQI=" not in html_content
799777
) # identifier in 1st row of query result
800-
assert (
801-
"mUZpbkdyYXBoLlBlcnNvbgB4kQY=" in html_content
802-
) # identifier in 2nd row of query result
803-
assert (
804-
"mUZpbkdyYXBoLlBlcnNvbgB4kQQ=" in html_content
805-
) # identifier in 3rd row of query result
806778

807779
# Make sure we actually used colab path, not GraphServer path.
808780
assert sys.modules["google.colab"].output.register_callback.called
@@ -818,6 +790,7 @@ def test_bigquery_graph_colab(monkeypatch):
818790
reason="Requires `spanner-graph-notebook` and `google-cloud-bigquery-storage`",
819791
)
820792
def test_colab_query_callback():
793+
graph_server.graph_server.query_result = pandas.DataFrame([], columns=["result"])
821794
result = bigquery_magics.bigquery._colab_query_callback(
822795
"query", json.dumps({"result": {}})
823796
)

tests/unit/test_graph_server.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616
import unittest
1717

18+
import pandas as pd
1819
import pytest
1920
import requests
2021

@@ -469,18 +470,14 @@ def test_post_ping(self):
469470
@pytest.mark.skipif(
470471
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
471472
)
472-
def test_post_query(self):
473+
def test_post_query(self):
473474
self.assertTrue(self.server_thread.is_alive())
475+
graph_server.graph_server.query_result = pd.DataFrame([json.dumps(row_alex_owns_account)], columns=["result"])
474476
route = graph_server.graph_server.build_route(
475477
graph_server.GraphServer.endpoints["post_query"]
476478
)
477479

478-
data = {
479-
"result": {
480-
"0": json.dumps(row_alex_owns_account),
481-
}
482-
}
483-
response = requests.post(route, json={"params": json.dumps(data)})
480+
response = requests.post(route, json={"params": "{}"})
484481
self.assertEqual(response.status_code, 200)
485482
response_data = response.json()["response"]
486483

0 commit comments

Comments
 (0)