12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import json
- from os import environ
- from unittest import TestCase
- from scriptshifter.rest_api import app
- from tests import TEST_DATA_DIR, reload_tables
- EP = "http://localhost:8000"
- class TestRestAPI(TestCase):
- """ Test REST API interaction. """
- def setUp(self):
- environ["TXL_CONFIG_TABLE_DIR"] = TEST_DATA_DIR
- # if "TXL_CONFIG_TABLE_DIR" in environ:
- # del environ["TXL_CONFIG_TABLE_DIR"]
- reload_tables()
- # Start webapp.
- app.testing = True
- def test_health(self):
- with app.test_client() as c:
- rsp = c.get("/health")
- self.assertEqual(rsp.status_code, 200)
- def test_language_list(self):
- with app.test_client() as c:
- rsp = c.get("/languages")
- self.assertEqual(rsp.status_code, 200)
- data = json.loads(rsp.get_data(as_text=True))
- self.assertIn("inherited", data)
- self.assertIn("name", data["inherited"])
- self.assertNotIn("_base1", data)
- self.assertNotIn("_base2", data)
- self.assertNotIn("_base3", data)
- def test_lang_table(self):
- with app.test_client() as c:
- rsp = c.get("/table/ordering")
- self.assertEqual(rsp.status_code, 200)
- data = json.loads(rsp.get_data(as_text=True))
- self.assertIn("general", data)
- self.assertIn("roman_to_script", data)
- self.assertIn("map", data["roman_to_script"])
- self.assertEqual(data["roman_to_script"]["map"][0], ["ABCD", ""])
- def test_trans_api_s2r(self):
- with app.test_client() as c:
- rsp = c.post("/trans", data={"lang": "rot3", "text": "defg"})
- self.assertEqual(rsp.status_code, 200)
- data = json.loads(rsp.get_data(as_text=True))
- self.assertEqual(data["output"], "abcd")
- def test_trans_api_r2s(self):
- with app.test_client() as c:
- rsp = c.post(
- "/trans", data={
- "lang": "rot3",
- "text": "abcd",
- "t_dir": "r2s"
- }
- )
- self.assertEqual(rsp.status_code, 200)
- data = json.loads(rsp.get_data(as_text=True))
- self.assertEqual(data["output"], "defg")
- def test_trans_api_capitalize(self):
- with app.test_client() as c:
- rsp = c.post(
- "/trans",
- data={
- "lang": "rot3",
- "capitalize": "first",
- "text": "bcde",
- "t_dir": "r2s"
- }
- )
- self.assertEqual(rsp.status_code, 200)
- data = json.loads(rsp.get_data(as_text=True))
- self.assertEqual(data["output"], "Efgh")
|