Skip to content

Commit cddb18c

Browse files
committed
Move get_ping() and post_ping() out of the GraphServer class, into the unit test
1 parent 8f3100f commit cddb18c

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

bigquery_magics/graph_server.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,7 @@ def stop_server():
168168
if GraphServer._server:
169169
GraphServer._server.shutdown()
170170
print("BigQuery-magics graph server shutting down...")
171-
172-
@staticmethod
173-
def get_ping():
174-
route = GraphServer.build_route(GraphServer.endpoints["get_ping"])
175-
response = requests.get(route)
176-
177-
assert (
178-
response.status_code == 200
179-
) # Guaranteed by GraphServerHandler implementation
180-
return response.json()
181-
182-
@staticmethod
183-
def post_ping(data):
184-
route = GraphServer.build_route(GraphServer.endpoints["post_ping"])
185-
response = requests.post(route, json=data)
186-
187-
if response.status_code == 200:
188-
return response.json()
189-
else:
190-
print(f"Request failed with status code {response.status_code}")
191-
return False
171+
GraphServer._server = None
192172

193173

194174
class GraphServerHandler(http.server.SimpleHTTPRequestHandler):

tests/unit/test_graph_server.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import json
16+
import requests
1617
import unittest
1718

1819
import pytest
@@ -320,15 +321,20 @@ def tearDown(self):
320321
GraphServer.stop_server() # Stop the server after each test
321322
self.server_thread.join() # Wait for the thread to finish
322323

323-
def test_ping(self):
324+
def test_get_ping(self):
324325
self.assertTrue(self.server_thread.is_alive())
325326

326-
response = GraphServer.get_ping()
327-
self.assertEqual(response, {"message": "pong"})
327+
route = GraphServer.build_route(GraphServer.endpoints["get_ping"])
328+
response = requests.get(route)
329+
self.assertEqual(response.status_code, 200)
330+
self.assertEqual(response.json(), {"message": "pong"})
328331

329-
request = {"data": "ping"}
330-
response = GraphServer.post_ping(request)
331-
self.assertEqual(response, {"your_request": request})
332+
def test_post_ping(self):
333+
self.assertTrue(self.server_thread.is_alive())
334+
route = GraphServer.build_route(GraphServer.endpoints["post_ping"])
335+
response = requests.post(route, json={"data": "ping"})
336+
self.assertEqual(response.status_code, 200)
337+
self.assertEqual(response.json(), {"your_request": {"data": "ping"}})
332338

333339

334340
def test_stop_server_never_started():

0 commit comments

Comments
 (0)