test_basic.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from larigira.filters.basic import maxwait, percentwait
  2. def matchval(d):
  3. def mocked(input_):
  4. if input_ in d:
  5. return d[input_]
  6. for k in d:
  7. if k in input_: # string matching
  8. return d[k]
  9. raise Exception("This test case is bugged! No value for %s" % input_)
  10. return mocked
  11. durations = dict(one=60, two=120, three=180, four=240, ten=600, twenty=1200,
  12. thirty=1800, nonexist=None)
  13. dur = matchval(durations)
  14. def normalize_ret(ret):
  15. if type(ret) is bool:
  16. return ret, ''
  17. return ret
  18. def mw(*args, **kwargs):
  19. return normalize_ret(maxwait(*args, **kwargs))
  20. def pw(*args, **kwargs):
  21. kwargs['getdur'] = dur
  22. return normalize_ret(percentwait(*args, **kwargs))
  23. def test_maxwait_nonpresent_disabled():
  24. ret = mw([], {}, {})
  25. assert ret[0] is True
  26. def test_maxwait_explicitly_disabled():
  27. ret = mw([], {}, {'EF_MAXWAIT_SEC': 0})
  28. assert ret[0] is True
  29. def test_maxwait_ok():
  30. ret = mw([], {'status': {'time': '250:300'}}, {'EF_MAXWAIT_SEC': 100})
  31. assert ret[0] is True
  32. def test_maxwait_exceeded():
  33. ret = mw([], {'status': {'time': '100:300'}}, {'EF_MAXWAIT_SEC': 100})
  34. assert ret[0] is False
  35. def test_maxwait_limit():
  36. ret = mw([], {'status': {'time': '199:300'}}, {'EF_MAXWAIT_SEC': 100})
  37. assert ret[0] is False
  38. ret = mw([], {'status': {'time': '200:300'}}, {'EF_MAXWAIT_SEC': 100})
  39. assert ret[0] is True
  40. ret = mw([], {'status': {'time': '201:300'}}, {'EF_MAXWAIT_SEC': 100})
  41. assert ret[0] is True
  42. def test_percentwait_nonpresent_disabled():
  43. ret = pw([], {}, {})
  44. assert ret[0] is True
  45. def test_percentwait_explicitly_disabled():
  46. ret = pw([], {}, {'EF_MAXWAIT_PERC': 0})
  47. assert ret[0] is True
  48. def test_percentwait_ok():
  49. # less than one minute missing
  50. ret = pw(dict(uris=['file:///oneminute.ogg']),
  51. {'status': {'time': '250:300'}},
  52. {'EF_MAXWAIT_PERC': 100})
  53. assert ret[0] is True
  54. # more than one minute missing
  55. ret = pw(dict(uris=['file:///oneminute.ogg']),
  56. {'status': {'time': '220:300'}},
  57. {'EF_MAXWAIT_PERC': 100})
  58. assert ret[0] is False
  59. def test_percentwait_morethan100():
  60. # requiring 5*10 = 50mins = 3000sec
  61. ret = pw(dict(uris=['file:///tenminute.ogg']),
  62. {'status': {'time': '4800:6000'}},
  63. {'EF_MAXWAIT_PERC': 500})
  64. assert ret[0] is True
  65. ret = pw(dict(uris=['file:///oneminute.ogg']),
  66. {'status': {'time': '2000:6000'}},
  67. {'EF_MAXWAIT_PERC': 500})
  68. assert ret[0] is False