You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.9 KiB

  1. #!/usr/bin/env python3
  2. import autoupload
  3. from datetime import datetime
  4. import os
  5. import tempfile
  6. import unittest
  7. class TestVideoMethods(unittest.TestCase):
  8. platform1 = "platform"
  9. platform2 = "otherplatform"
  10. def tearDown(self):
  11. # Reset the kinda static variable between runs.
  12. autoupload._autouploadFiles = []
  13. def test_nextVideo(self):
  14. with TestFileContent(
  15. """[videos]
  16. Episode1 = {}
  17. """
  18. ) as videoFile:
  19. auto = autoupload.AutoUpload(videoFile.filename)
  20. nextVideo = auto.nextVideo(self.platform1)
  21. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  22. nextVideo = auto.nextVideo(self.platform2)
  23. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  24. def test_nextVideoSubfolder(self):
  25. with TestFileContent(
  26. """[videos]
  27. "subfolder/Episode1" = {}
  28. """
  29. ) as videoFile:
  30. auto = autoupload.AutoUpload(videoFile.filename)
  31. nextVideo = auto.nextVideo(self.platform1)
  32. self.assertEqual(nextVideo.videoPath, "/tmp/subfolder/Episode1.mp4")
  33. nextVideo = auto.nextVideo(self.platform2)
  34. self.assertEqual(nextVideo.videoPath, "/tmp/subfolder/Episode1.mp4")
  35. def test_success(self):
  36. with TestFileContent(
  37. """[videos]
  38. Episode1 = {}
  39. """
  40. ) as videoFile:
  41. auto = autoupload.AutoUpload(videoFile.filename)
  42. nextVideo = auto.nextVideo(self.platform1)
  43. nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31))
  44. nextVideo = auto.nextVideo(self.platform1)
  45. self.assertEqual(nextVideo, None)
  46. nextVideo = auto.nextVideo(self.platform2)
  47. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  48. # TODO: read file and check formatting and content?
  49. # print("content of file")
  50. # with open(videoFile.filename, "r") as file:
  51. # print(file.readlines())
  52. def test_error(self):
  53. with TestFileContent(
  54. """[videos]
  55. Episode1 = {}
  56. """
  57. ) as videoFile:
  58. auto = autoupload.AutoUpload(videoFile.filename)
  59. nextVideo = auto.nextVideo(self.platform1)
  60. nextVideo.error("Server not available")
  61. nextVideo = auto.nextVideo(self.platform1)
  62. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  63. # TODO: read file and check formatting and content?
  64. # print("content of file")
  65. # with open(videoFile.filename, "r") as file:
  66. # print(file.readlines())
  67. def test_errorThenSuccess(self):
  68. with TestFileContent(
  69. """[videos]
  70. Episode1 = {}
  71. """
  72. ) as videoFile:
  73. auto = autoupload.AutoUpload(videoFile.filename)
  74. nextVideo = auto.nextVideo(self.platform1)
  75. nextVideo.error("Server not available")
  76. nextVideo = auto.nextVideo(self.platform1)
  77. # TODO: read file and check formatting and content?
  78. # print("content of file")
  79. # with open(videoFile.filename, "r") as file:
  80. # print(file.readlines())
  81. nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31))
  82. nextVideo = auto.nextVideo(self.platform1)
  83. self.assertEqual(nextVideo, None)
  84. nextVideo = auto.nextVideo(self.platform2)
  85. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  86. # TODO: read file and check formatting and content?
  87. # TODO: check the key "platform-error" is not present after a success
  88. # print("content of file")
  89. # with open(videoFile.filename, "r") as file:
  90. # print(file.readlines())
  91. def test_subAutouploadFile(self):
  92. # We may split this test to first test that we can have reference another file
  93. # and then that we pass transparently between files.
  94. with TestFileContent(
  95. """[videos]
  96. Episode2 = {} # TODO: We should chek that the path of this file is from the base path of this autoupload config file
  97. """
  98. ) as autouploadFile:
  99. with TestFileContent(
  100. f"""autoupload = ["{autouploadFile.filename}"] # list of strings, one for each sub autoupload file
  101. [videos]
  102. Episode1 = {{}} # Double curly braces to escape them for f-string
  103. """
  104. ) as videoFile:
  105. auto = autoupload.AutoUpload(videoFile.filename)
  106. nextVideo = auto.nextVideo(self.platform1)
  107. nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31))
  108. nextVideo = auto.nextVideo(self.platform1)
  109. self.assertEqual(nextVideo.videoPath, "/tmp/Episode2.mp4")
  110. # TODO: read file and check formatting and content?
  111. nextVideo.success("https://platform/url", datetime(2020, 8, 28, 17, 54, 31))
  112. nextVideo = auto.nextVideo(self.platform1)
  113. self.assertEqual(nextVideo, None)
  114. nextVideo = auto.nextVideo(self.platform2)
  115. self.assertEqual(nextVideo.videoPath, "/tmp/Episode1.mp4")
  116. # TODO: read file and check formatting and content?
  117. # print("content of autoupload file")
  118. # with open(autouploadFile.filename, "r") as file:
  119. # print(file.readlines())
  120. #
  121. # print("content of video file")
  122. # with open(videoFile.filename, "r") as file:
  123. # print(file.readlines())
  124. # https://stackoverflow.com/a/54053967
  125. class TestFileContent:
  126. def __init__(self, content):
  127. self.file = tempfile.NamedTemporaryFile(mode="w", delete=False)
  128. with self.file as f:
  129. f.write(content)
  130. @property
  131. def filename(self):
  132. return self.file.name
  133. def __enter__(self):
  134. return self
  135. def __exit__(self, type, value, traceback):
  136. os.unlink(self.filename)
  137. if __name__ == '__main__':
  138. unittest.main()