test04_rest_api.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json
  2. from os import environ
  3. from unittest import TestCase
  4. from scriptshifter.rest_api import app
  5. from tests import TEST_DATA_DIR, reload_tables
  6. EP = "http://localhost:8000"
  7. class TestRestAPI(TestCase):
  8. """ Test REST API interaction. """
  9. def setUp(self):
  10. environ["TXL_CONFIG_TABLE_DIR"] = TEST_DATA_DIR
  11. # if "TXL_CONFIG_TABLE_DIR" in environ:
  12. # del environ["TXL_CONFIG_TABLE_DIR"]
  13. reload_tables()
  14. # Start webapp.
  15. app.testing = True
  16. def test_health(self):
  17. with app.test_client() as c:
  18. rsp = c.get("/health")
  19. self.assertEqual(rsp.status_code, 200)
  20. def test_language_list(self):
  21. with app.test_client() as c:
  22. rsp = c.get("/languages")
  23. self.assertEqual(rsp.status_code, 200)
  24. data = json.loads(rsp.get_data(as_text=True))
  25. self.assertIn("inherited", data)
  26. self.assertIn("name", data["inherited"])
  27. self.assertNotIn("_base1", data)
  28. self.assertNotIn("_base2", data)
  29. self.assertNotIn("_base3", data)
  30. def test_lang_table(self):
  31. with app.test_client() as c:
  32. rsp = c.get("/table/ordering")
  33. self.assertEqual(rsp.status_code, 200)
  34. data = json.loads(rsp.get_data(as_text=True))
  35. self.assertIn("general", data)
  36. self.assertIn("roman_to_script", data)
  37. self.assertIn("map", data["roman_to_script"])
  38. self.assertEqual(data["roman_to_script"]["map"][0], ["ABCD", ""])
  39. def test_trans_api_s2r(self):
  40. with app.test_client() as c:
  41. rsp = c.post("/trans", data={"lang": "rot3", "text": "defg"})
  42. self.assertEqual(rsp.status_code, 200)
  43. data = json.loads(rsp.get_data(as_text=True))
  44. self.assertEqual(data["output"], "abcd")
  45. def test_trans_api_r2s(self):
  46. with app.test_client() as c:
  47. rsp = c.post(
  48. "/trans", data={
  49. "lang": "rot3",
  50. "text": "abcd",
  51. "t_dir": "r2s"
  52. }
  53. )
  54. self.assertEqual(rsp.status_code, 200)
  55. data = json.loads(rsp.get_data(as_text=True))
  56. self.assertEqual(data["output"], "defg")
  57. def test_trans_api_capitalize(self):
  58. with app.test_client() as c:
  59. rsp = c.post(
  60. "/trans",
  61. data={
  62. "lang": "rot3",
  63. "capitalize": "first",
  64. "text": "bcde",
  65. "t_dir": "r2s"
  66. }
  67. )
  68. self.assertEqual(rsp.status_code, 200)
  69. data = json.loads(rsp.get_data(as_text=True))
  70. self.assertEqual(data["output"], "Efgh")