test_libtn.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import unittest
  2. import libtn
  3. class LibTest(unittest.TestCase):
  4. def test_get_followed_channels(self):
  5. api = libtn.NotifyApi('test_account123321', None, None, False)
  6. list_of_chans = api.get_followed_channels({'offset': 0, 'limit': 1})
  7. self.assertEqual(list_of_chans[0], 'xangold')
  8. self.assertEqual(len(list_of_chans), 1)
  9. list_of_chans = api.get_followed_channels({'offset': 1, 'limit': 1})
  10. self.assertEqual(len(list_of_chans), 0)
  11. list_of_chans = api.get_followed_channels({'offset': 0, 'limit': 100})
  12. self.assertEqual(list_of_chans[0], 'xangold')
  13. self.assertEqual(len(list_of_chans), 1)
  14. list_of_chans = api.get_followed_channels({'offset': 1, 'limit': 100})
  15. self.assertEqual(len(list_of_chans), 0)
  16. api = libtn.NotifyApi('test_account5555666', None, None, False)
  17. list_of_chans = api.get_followed_channels({'offset': 0, 'limit': 1})
  18. self.assertEqual(len(list_of_chans), 0)
  19. list_of_chans = api.get_followed_channels({'offset': 1, 'limit': 1})
  20. self.assertEqual(len(list_of_chans), 0)
  21. api = libtn.NotifyApi('metasigma', None, None, False)
  22. list_of_chans = api.get_followed_channels({'offset': 0, 'limit': 100})
  23. list_of_chans2 = api.get_followed_channels({'offset': 100, 'limit': 100})
  24. self.assertEqual(len(list_of_chans)+len(list_of_chans2) > 100, True)
  25. def test_check_if_online(self):
  26. settings = libtn.Settings('/tmp/doesn\'t_exist')
  27. acc = 'test_account123321'
  28. api = libtn.NotifyApi(acc, settings, None, False)
  29. self.assertEqual(api.check_if_online([acc])[acc][0], False)
  30. def test_diff(self):
  31. api = libtn.NotifyApi('test_account123321', None, None, False)
  32. output = {}
  33. def assign_output(online, _, name):
  34. output[name] = online
  35. api.inform_user = assign_output
  36. # off -> on
  37. example = {'abc': (False, {})}
  38. api.diff(example)
  39. example = {'abc': (True, {})}
  40. api.diff(example)
  41. self.assertEqual(output['abc'], True)
  42. # on -> off
  43. example = {'abc': (False, {})}
  44. api.diff(example)
  45. self.assertEqual(output['abc'], False)
  46. # off -> error
  47. example = {'abc': (None, {})}
  48. api.diff(example)
  49. self.assertEqual(output['abc'], False)
  50. # error -> on
  51. example = {'abc': (True, {})}
  52. api.diff(example)
  53. self.assertEqual(output['abc'], True)
  54. # on -> error
  55. example = {'abc': (None, {})}
  56. api.diff(example)
  57. self.assertEqual(output['abc'], True)
  58. # error -> off
  59. example = {'abc': (False, {})}
  60. api.diff(example)
  61. self.assertEqual(output['abc'], False)
  62. # off -> error
  63. example = {'abc': (None, {})}
  64. api.diff(example)
  65. self.assertEqual(output['abc'], False)
  66. # error -> error
  67. example = {'abc': (None, {})}
  68. api.diff(example)
  69. self.assertEqual(output['abc'], False)
  70. def test_repl(self):
  71. chan = 'foo'
  72. ret = libtn.repl(None, chan, '$1 $2')
  73. self.assertEqual(ret, chan + ' offline')
  74. ret = libtn.repl(None, chan, '$3$4$5$6$7$8$9')
  75. self.assertEqual(ret, '$3$4$5$6$7$8$9')
  76. ret = libtn.repl(None, chan, '${}')
  77. self.assertNotEqual(ret, '${}')
  78. ret = libtn.repl(None, chan, '$%3$@4$^5$&6$(7$&8$+9')
  79. self.assertEqual(ret, '$%3$@4$^5$&6$(7$&8$+9')
  80. stream = {'foo': 'bar'}
  81. ret = libtn.repl(stream, chan, '$1 $2')
  82. self.assertEqual(ret, chan + ' online')
  83. ret = libtn.repl(stream, chan, '$3$4$5$6$7$8$9')
  84. self.assertEqual(ret, '')
  85. stream = {'game': 'test', 'viewers': 123, 'average_fps': 24.2}
  86. ret = libtn.repl(stream, chan, '$3$4$7')
  87. self.assertEqual(ret, 'test' + '123' + '24.2')
  88. if __name__ == '__main__':
  89. unittest.main()