Skip to content

Commit 6f9ddce

Browse files
committed
Add more unit tests for convert_graph_data() to boost code coverage.
1 parent 942f7c1 commit 6f9ddce

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

bigquery_magics/graph_server.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,19 @@ def convert_graph_data(query_results: Dict[str, Dict[str, str]]):
6464
for key, value in query_results.items():
6565
if column_name is None:
6666
if not isinstance(key, str):
67-
raise ValueError(f"Expected key to be str, got {type(key)}")
67+
raise ValueError(f"Expected outer key to be str, got {type(key)}")
6868
if not isinstance(value, dict):
69-
raise ValueError(f"Expected value to be dict, got {type(value)}")
69+
raise ValueError(f"Expected outer value to be dict, got {type(value)}")
7070
column_name = key
7171
column_value = value
7272
else:
73+
# TODO: Implement multi-column support.
7374
raise ValueError(
7475
"Query has multiple columns - graph visualization not supported"
7576
)
7677
if column_name is None or column_value is None:
7778
raise ValueError(
78-
"Unable to get column name or value - how is this possible???"
79+
"query result with no columns is not supported for graph visualization"
7980
)
8081

8182
fields: List[StructType.Field] = [
@@ -85,9 +86,9 @@ def convert_graph_data(query_results: Dict[str, Dict[str, str]]):
8586
rows = []
8687
for value_key, value_value in column_value.items():
8788
if not isinstance(value_key, str):
88-
raise ValueError(f"Expected key to be str, got {type(key)}")
89+
raise ValueError(f"Expected inner key to be str, got {type(value_key)}")
8990
if not isinstance(value_value, str):
90-
raise ValueError(f"Expected value to be str, got {type(value)}")
91+
raise ValueError(f"Expected inner value to be str, got {type(value_value)}")
9192
row_index = int(value_key)
9293
row_json = json.loads(value_value)
9394

tests/unit/test_graph_server.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_convert_one_column_no_rows():
148148
@pytest.mark.skipif(
149149
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
150150
)
151-
def test_convert_one_column_one_row():
151+
def test_convert_one_column_one_row_one_column():
152152
result = convert_graph_data(
153153
{
154154
"result": {
@@ -217,6 +217,82 @@ def test_convert_nongraph_json():
217217
assert result["response"]["schema"] is None
218218

219219

220+
@pytest.mark.skipif(
221+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
222+
)
223+
def test_convert_outer_key_not_string():
224+
result = convert_graph_data(
225+
{
226+
0: {
227+
'0': json.dumps({"foo": 1, "bar": 2}),
228+
}
229+
})
230+
assert result == {"error": "Expected outer key to be str, got <class 'int'>"}
231+
232+
233+
@pytest.mark.skipif(
234+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
235+
)
236+
def test_convert_outer_value_not_dict():
237+
result = convert_graph_data(
238+
{
239+
'result': 0
240+
})
241+
assert result == {"error": "Expected outer value to be dict, got <class 'int'>"}
242+
243+
244+
@pytest.mark.skipif(
245+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
246+
)
247+
def test_convert_inner_key_not_string():
248+
result = convert_graph_data(
249+
{
250+
'result': {
251+
0: json.dumps({"foo": 1, "bar": 2}),
252+
}
253+
})
254+
assert result == {"error": "Expected inner key to be str, got <class 'int'>"}
255+
256+
257+
@pytest.mark.skipif(
258+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
259+
)
260+
def test_convert_inner_value_not_string():
261+
result = convert_graph_data(
262+
{
263+
'result': {
264+
'0': 1,
265+
}
266+
})
267+
assert result == {"error": "Expected inner value to be str, got <class 'int'>"}
268+
269+
270+
@pytest.mark.skipif(
271+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
272+
)
273+
def test_convert_one_column_one_row_two_columns():
274+
result = convert_graph_data(
275+
{
276+
"result1": {
277+
"0": json.dumps(row_alex_owns_account),
278+
},
279+
"result2": {
280+
"0": json.dumps(row_alex_owns_account),
281+
}
282+
283+
}
284+
)
285+
assert result == {"error": "Query has multiple columns - graph visualization not supported"}
286+
287+
288+
@pytest.mark.skipif(
289+
graph_visualization is None, reason="Requires `spanner-graph-notebook`"
290+
)
291+
def test_convert_empty_dict():
292+
result = convert_graph_data({})
293+
assert result == {"error": "query result with no columns is not supported for graph visualization"}
294+
295+
220296
class TestGraphServer(unittest.TestCase):
221297
def setUp(self):
222298
self.server_thread = GraphServer.init()

0 commit comments

Comments
 (0)