test_audiogen_mostrecent.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import tempfile
  2. import os
  3. import time
  4. import pytest
  5. from larigira.audiogen_mostrecent import recent_choose
  6. @pytest.fixture
  7. def now(request):
  8. return int(time.time())
  9. @pytest.fixture
  10. def empty_dir():
  11. dirpath = tempfile.mkdtemp(prefix='mostrecent.')
  12. yield dirpath
  13. os.removedirs(dirpath)
  14. @pytest.fixture
  15. def dir_with_old_file(empty_dir):
  16. fd, fname = tempfile.mkstemp(prefix='old.', dir=empty_dir)
  17. os.close(fd)
  18. os.utime(fname, times=(0, 0))
  19. yield empty_dir
  20. os.unlink(fname)
  21. @pytest.fixture
  22. def dir_with_new_file(dir_with_old_file, now):
  23. fd, fname = tempfile.mkstemp(prefix='new.', dir=dir_with_old_file)
  24. os.close(fd)
  25. os.utime(fname, times=(now, now))
  26. yield dir_with_old_file
  27. os.unlink(fname)
  28. def test_empty_is_empty(empty_dir, now):
  29. '''nothing can be picked from a empty dir'''
  30. picked = recent_choose([empty_dir], 1, now)
  31. assert len(picked) == 0
  32. def test_old_files(dir_with_old_file, now):
  33. picked = recent_choose([dir_with_old_file], 1, now)
  34. assert len(picked) == 0
  35. def test_new_files_found(dir_with_new_file):
  36. picked = recent_choose([dir_with_new_file], 1, 1)
  37. assert len(picked) == 1
  38. assert os.path.basename(picked[0]).startswith('new.')
  39. def test_only_new_files_found(dir_with_new_file):
  40. picked = recent_choose([dir_with_new_file], 2, 1)
  41. assert len(picked) == 1
  42. assert os.path.basename(picked[0]).startswith('new.')