test04_rest_api.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/rot3", data={"text": "defg"})
  42. self.assertEqual(rsp.status_code, 200)
  43. data = rsp.get_data(as_text=True)
  44. self.assertEqual(data, "abcd")
  45. def test_trans_api_r2s(self):
  46. with app.test_client() as c:
  47. rsp = c.post("/trans/rot3/r2s", data={"text": "abcd"})
  48. self.assertEqual(rsp.status_code, 200)
  49. data = rsp.get_data(as_text=True)
  50. self.assertEqual(data, "defg")
  51. def test_trans_api_capitalize(self):
  52. with app.test_client() as c:
  53. rsp = c.post(
  54. "/trans/rot3/r2s",
  55. data={"capitalize": "first", "text": "bcde"})
  56. self.assertEqual(rsp.status_code, 200)
  57. data = rsp.get_data(as_text=True)
  58. self.assertEqual(data, "Efgh")
  59. def test_trans_form(self):
  60. with app.test_client() as c:
  61. rsp = c.post(
  62. "/transliterate", data={
  63. "text": "abcd",
  64. "r2s": "true",
  65. "lang": "rot3",
  66. })
  67. self.assertEqual(rsp.status_code, 200)
  68. data = rsp.get_data(as_text=True)
  69. self.assertEqual(data, "defg")