def test_show_error(self): io = Interface(lambda x: 1 / x, "number", "number") app, _, _ = io.launch(show_error=True, prevent_thread_lock=True) client = TestClient(app) response = client.post("/api/predict/", json={"data": [0]}) self.assertEqual(response.status_code, 500) self.assertTrue("error" in response.json()) io.close()
def test_caching(self): io = Interface( lambda x: "Hello " + x, "text", "text", examples=[["World"], ["Dunya"], ["Monde"]], ) io.launch(prevent_thread_lock=True) process_examples.cache_interface_examples(io) prediction = process_examples.load_from_cache(io, 1) io.close() self.assertEquals(prediction[0], "Hello Dunya")
def test_interpretation(self): io = Interface(lambda x: len(x), "text", "label", interpretation="default", analytics_enabled=True) app, _, _ = io.launch(prevent_thread_lock=True) client = TestClient(app) aiohttp.ClientSession.post = mock.MagicMock() aiohttp.ClientSession.post.__aenter__ = None aiohttp.ClientSession.post.__aexit__ = None io.interpret = mock.MagicMock(return_value=(None, None)) response = client.post('/api/interpret/', json={"data": ["test test"]}) aiohttp.ClientSession.post.assert_called() self.assertEqual(response.status_code, 200) io.close()
class TestRoutes(unittest.TestCase): def setUp(self) -> None: self.io = Interface(lambda x: x, "text", "text") self.app, _, _ = self.io.launch(prevent_thread_lock=True) self.client = TestClient(self.app) def test_get_main_route(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) def test_get_api_route(self): response = self.client.get('/api/') self.assertEqual(response.status_code, 200) def test_static_files_served_safely(self): # Make sure things outside the static folder are not accessible response = self.client.get(r'/static/..%2findex.html') self.assertEqual(response.status_code, 404) response = self.client.get(r'/static/..%2f..%2fapi_docs.html') self.assertEqual(response.status_code, 404) def test_get_config_route(self): response = self.client.get('/config/') self.assertEqual(response.status_code, 200) def test_predict_route(self): response = self.client.post('/api/predict/', json={"data": ["test"]}) self.assertEqual(response.status_code, 200) output = dict(response.json()) self.assertEqual(output["data"], ["test"]) self.assertTrue("durations" in output) self.assertTrue("avg_durations" in output) # def test_queue_push_route(self): # networking.queue.push = mock.MagicMock(return_value=(None, None)) # response = self.client.post('/api/queue/push/', json={"data": "test", "action": "test"}) # self.assertEqual(response.status_code, 200) # def test_queue_push_route(self): # networking.queue.get_status = mock.MagicMock(return_value=(None, None)) # response = self.client.post('/api/queue/status/', json={"hash": "test"}) # self.assertEqual(response.status_code, 200) def tearDown(self) -> None: self.io.close() reset_all()
class TestAuthenticatedRoutes(unittest.TestCase): def setUp(self) -> None: self.io = Interface(lambda x: x, "text", "text") self.app, _, _ = self.io.launch(auth=("test", "correct_password"), prevent_thread_lock=True) self.client = TestClient(self.app) def test_post_login(self): response = self.client.post("/login", data=dict(username="******", password="******")) self.assertEqual(response.status_code, 302) response = self.client.post("/login", data=dict(username="******", password="******")) self.assertEqual(response.status_code, 400) def tearDown(self) -> None: self.io.close() reset_all()
def test_flagging_analytics(self): callback = flagging.CSVLogger() callback.flag = mock.MagicMock() aiohttp.ClientSession.post = mock.MagicMock() aiohttp.ClientSession.post.__aenter__ = None aiohttp.ClientSession.post.__aexit__ = None io = Interface(lambda x: x, "text", "text", analytics_enabled=True, flagging_callback=callback) app, _, _ = io.launch(show_error=True, prevent_thread_lock=True) client = TestClient(app) response = client.post( '/api/flag/', json={"data": { "input_data": ["test"], "output_data": ["test"] }}) aiohttp.ClientSession.post.assert_called() callback.flag.assert_called_once() self.assertEqual(response.status_code, 200) io.close()
class TestRoutes(unittest.TestCase): def setUp(self) -> None: self.io = Interface(lambda x: x + x, "text", "text") self.app, _, _ = self.io.launch(prevent_thread_lock=True) self.client = TestClient(self.app) def test_get_main_route(self): response = self.client.get("/") self.assertEqual(response.status_code, 200) # def test_get_api_route(self): # response = self.client.get("/api/") # self.assertEqual(response.status_code, 200) def test_static_files_served_safely(self): # Make sure things outside the static folder are not accessible response = self.client.get(r"/static/..%2findex.html") self.assertEqual(response.status_code, 404) response = self.client.get(r"/static/..%2f..%2fapi_docs.html") self.assertEqual(response.status_code, 404) def test_get_config_route(self): response = self.client.get("/config/") self.assertEqual(response.status_code, 200) def test_predict_route(self): response = self.client.post("/api/predict/", json={ "data": ["test"], "fn_index": 0 }) self.assertEqual(response.status_code, 200) output = dict(response.json()) self.assertEqual(output["data"], ["testtest"]) def test_state(self): def predict(input, history): if history is None: history = "" history += input return history, history io = Interface(predict, ["textbox", "state"], ["textbox", "state"]) app, _, _ = io.launch(prevent_thread_lock=True) client = TestClient(app) response = client.post( "/api/predict/", json={ "data": ["test", None], "fn_index": 0, "session_hash": "_" }, ) output = dict(response.json()) print("output", output) self.assertEqual(output["data"], ["test", None]) response = client.post( "/api/predict/", json={ "data": ["test", None], "fn_index": 0, "session_hash": "_" }, ) output = dict(response.json()) self.assertEqual(output["data"], ["testtest", None]) def test_queue_push_route(self): queueing.push = mock.MagicMock(return_value=(None, None)) response = self.client.post("/api/queue/push/", json={ "data": "test", "action": "test" }) self.assertEqual(response.status_code, 200) def test_queue_push_route_2(self): queueing.get_status = mock.MagicMock(return_value=(None, None)) response = self.client.post("/api/queue/status/", json={"hash": "test"}) self.assertEqual(response.status_code, 200) def tearDown(self) -> None: self.io.close() reset_all()