test_DownloadFolderMonitoring.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from baangt.base.DownloadFolderMonitoring import DownloadFolderMonitoring
  2. from datetime import datetime
  3. import os
  4. directoryToMonitor = os.path.join(os.getcwd(), "tests/0TestInput")
  5. newFileName = "DownloadFolderMonitoring_test.txt"
  6. newFile = os.path.join(directoryToMonitor, newFileName)
  7. if os.path.exists(newFile):
  8. os.remove(newFile)
  9. def createFile(file):
  10. # Create file for testing
  11. with open(file, 'w')as file_object:
  12. file_object.write(str(datetime.now()))
  13. def removeFile(file):
  14. # Remove file after testing
  15. try:
  16. os.remove(file)
  17. except Exception as e:
  18. pass
  19. def test_getNewFiles():
  20. # Creates a file in Monitored Folder after creating DownloadFolderMonitoring instance and check getNewFiles Method.
  21. lDownloadFolderMonitoring = DownloadFolderMonitoring(directoryToMonitor)
  22. createFile(newFile)
  23. newFiles = lDownloadFolderMonitoring.getNewFiles()
  24. assert newFile == newFiles[0][0]
  25. print("Success. New File detected.")
  26. removeFile(newFile)
  27. def test_creation_date():
  28. # Creates file in Monitored Folder and verify if creation_date method of DownloadFolderMonitoring return true value.
  29. test_file_creation = int(datetime.now().timestamp())
  30. file = os.path.join(directoryToMonitor, str(test_file_creation)+".txt")
  31. createFile(file)
  32. creation_date = DownloadFolderMonitoring.creation_date(file)
  33. assert test_file_creation-int(creation_date) < 1
  34. print("Creation time is correct. Test Successful!")
  35. removeFile(file)