testCommon.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # For convenience, just run the tests from its own directory
  2. import os
  3. import sys
  4. os.chdir(os.path.dirname(sys.argv[0]))
  5. sys.path.append("../lib")
  6. import common
  7. import url
  8. class Settings:
  9. def __init__(self, **kwargs):
  10. for k, v in kwargs.items():
  11. setattr(self, k, v)
  12. def assert_equal(v1, v2):
  13. if v1 != v2:
  14. print("mismatch")
  15. print(v1)
  16. print(v2)
  17. raise AssertionError
  18. def test_sanitize():
  19. assert_equal(
  20. url.sanitize_stream(stream_name="foo bar", stream_id=7),
  21. "7-foo-bar",
  22. )
  23. assert_equal(
  24. url.sanitize_stream(stream_name="foo/bar/turtle[🐢]", stream_id=7),
  25. "7-foo.2Fbar.2Fturtle.5B.F0.9F.90.A2.5D",
  26. )
  27. assert_equal(
  28. url.sanitize("pick a place for lunch *"),
  29. "pick.20a.20place.20for.20lunch.20.2A",
  30. )
  31. assert_equal(
  32. url.sanitize("!!cute-turlte/tortoise (🐢)?"),
  33. ".21.21cute-turlte.2Ftortoise.20.28.F0.9F.90.A2.29.3F",
  34. )
  35. assert_equal(
  36. url.sanitize('"the mighty turtle 🐢"'),
  37. ".22the.20mighty.20turtle.20.F0.9F.90.A2.22",
  38. )
  39. def test_validator():
  40. validator = common.stream_validator(Settings(included_streams=["*"]))
  41. assert_equal(validator("foo"), True)
  42. validator = common.stream_validator(Settings(included_streams=["foo", "bar"]))
  43. assert_equal(validator("foo"), True)
  44. assert_equal(validator("bar"), True)
  45. assert_equal(validator("baz"), False)
  46. validator = common.stream_validator(
  47. Settings(included_streams=["*"], excluded_streams=["bad", "worse"])
  48. )
  49. assert_equal(validator("good"), True)
  50. assert_equal(validator("bad"), False)
  51. assert_equal(validator("worse"), False)
  52. # edge case: excluded takes precedence over included
  53. validator = common.stream_validator(
  54. Settings(included_streams=["foo"], excluded_streams=["foo"])
  55. )
  56. assert_equal(validator("foo"), False)
  57. if __name__ == "__main__":
  58. test_sanitize()
  59. test_validator()