test_timesince.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from datetime import datetime, timedelta
  17. from mediagoblin.tools.timesince import timesince
  18. def test_timesince():
  19. test_time = datetime.now()
  20. # it should ignore second and microseconds
  21. assert timesince(test_time, test_time + timedelta(microseconds=1)) == "0 minutes"
  22. assert timesince(test_time, test_time + timedelta(seconds=1)) == "0 minutes"
  23. # test minutes, hours, days, weeks, months and years (singular and plural)
  24. assert timesince(test_time, test_time + timedelta(minutes=1)) == "1 minute"
  25. assert timesince(test_time, test_time + timedelta(minutes=2)) == "2 minutes"
  26. assert timesince(test_time, test_time + timedelta(hours=1)) == "1 hour"
  27. assert timesince(test_time, test_time + timedelta(hours=2)) == "2 hours"
  28. assert timesince(test_time, test_time + timedelta(days=1)) == "1 day"
  29. assert timesince(test_time, test_time + timedelta(days=2)) == "2 days"
  30. assert timesince(test_time, test_time + timedelta(days=7)) == "1 week"
  31. assert timesince(test_time, test_time + timedelta(days=14)) == "2 weeks"
  32. assert timesince(test_time, test_time + timedelta(days=30)) == "1 month"
  33. assert timesince(test_time, test_time + timedelta(days=60)) == "2 months"
  34. assert timesince(test_time, test_time + timedelta(days=365)) == "1 year"
  35. assert timesince(test_time, test_time + timedelta(days=730)) == "2 years"
  36. # okay now we want to test combinations
  37. # e.g. 1 hour, 5 days
  38. assert timesince(test_time, test_time + timedelta(days=5, hours=1)) == "5 days, 1 hour"
  39. assert timesince(test_time, test_time + timedelta(days=15)) == "2 weeks, 1 day"
  40. assert timesince(test_time, test_time + timedelta(days=97)) == "3 months, 1 week"
  41. assert timesince(test_time, test_time + timedelta(days=2250)) == "6 years, 2 months"