test_duration.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from ioex.datetimeex import Duration
  4. @pytest.mark.parametrize(('years'), [
  5. 0,
  6. 13,
  7. ])
  8. def test_init(years):
  9. d = Duration(years = years)
  10. assert d.years == years
  11. def test_init_default():
  12. d = Duration()
  13. assert d.years == 0
  14. None,
  15. @pytest.mark.parametrize(('years', 'exception_type'), [
  16. [-2, ValueError],
  17. ['1', TypeError],
  18. ])
  19. def test_init_fail(years, exception_type):
  20. with pytest.raises(exception_type):
  21. Duration(years = years)
  22. @pytest.mark.parametrize(('years'), [
  23. 0,
  24. 13,
  25. ])
  26. def test_set_years(years):
  27. d = Duration()
  28. d.years = years
  29. assert d.years == years
  30. @pytest.mark.parametrize(('years', 'exception_type'), [
  31. [-2, ValueError],
  32. ['1', TypeError],
  33. ])
  34. def test_set_years_fail(years, exception_type):
  35. d = Duration()
  36. with pytest.raises(exception_type):
  37. d.years = years
  38. @pytest.mark.parametrize(('init_params', 'iso'), [
  39. [{'years': 0}, 'P0Y'],
  40. [{'years': 3}, 'P3Y'],
  41. [{'years': 30}, 'P30Y'],
  42. ])
  43. def test_get_isoformat(init_params, iso):
  44. d = Duration(**init_params)
  45. assert d.isoformat == iso
  46. @pytest.mark.parametrize(('a', 'b'), [
  47. [Duration(), Duration(years = 0)],
  48. [Duration(years = 0), Duration(years = 0)],
  49. [Duration(years = 3), Duration(years = 3)],
  50. ])
  51. def test_eq(a, b):
  52. assert a == b
  53. assert b == a