test_.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import copy
  4. import datetime
  5. import dingguo
  6. import os
  7. import yaml
  8. def test_sum_init_eur():
  9. assert dingguo.Sum(1.23, u'EUR') == dingguo.Sum(1.23, u'€')
  10. def test_sum_init_usd():
  11. assert dingguo.Sum(1.23, u'USD') == dingguo.Sum(1.23, u'US$')
  12. def test_distance_metres():
  13. assert int(dingguo.Distance(1.23, u'km').metres) == 1230
  14. def test_person_first_name():
  15. p = dingguo.Person()
  16. p.first_name = u'äbc'
  17. assert p.first_name == u'äbc'
  18. p.first_name = u'qrßtuvw'
  19. assert p.first_name == u'qrßtuvw'
  20. def test_person_first_name_string():
  21. p = dingguo.Person()
  22. with pytest.raises(Exception):
  23. p.first_name = 'äbc'
  24. def test_person_last_name():
  25. p = dingguo.Person()
  26. p.last_name = u'歌曲'
  27. assert p.last_name == u'歌曲'
  28. p.last_name = u'bär'
  29. assert p.last_name == u'bär'
  30. def test_person_last_name_string():
  31. p = dingguo.Person()
  32. with pytest.raises(Exception):
  33. p.last_name = 'äbc'
  34. @pytest.mark.parametrize(('text', 'sum'), [
  35. [u'$1.23 USD', dingguo.Sum(1.23, u'USD')],
  36. [u'$30 USD', dingguo.Sum(30.0, u'USD')],
  37. [u'$30.00 USD', dingguo.Sum(30.0, u'USD')],
  38. [u'$8', dingguo.Sum(8.0, u'USD')],
  39. [u'$8.00', dingguo.Sum(8.0, u'USD')],
  40. ])
  41. def test_sum_parse_text(text, sum):
  42. assert dingguo.Sum.parse_text(text) == sum
  43. @pytest.mark.parametrize(('text'), [
  44. u'pre$1.23 USD',
  45. u'$1.23 USDpost',
  46. ])
  47. def test_sum_parse_text_fail(text):
  48. with pytest.raises(Exception):
  49. dingguo.Sum.parse_text(text)
  50. @pytest.mark.parametrize(('minuend', 'subtrahend', 'difference'), [
  51. [dingguo.Sum(5.0, u'USD'), dingguo.Sum(2.0, u'USD'), dingguo.Sum(3.0, u'USD')],
  52. [dingguo.ScalarFigure(5.0, u'cm'), dingguo.ScalarFigure(2.0, u'cm'), dingguo.ScalarFigure(3.0, u'cm')],
  53. [dingguo.ScalarFigure(1.0, u'kg'), dingguo.ScalarFigure(2.0, u'kg'), dingguo.ScalarFigure(-1.0, u'kg')],
  54. ])
  55. def test_figure_sub(minuend, subtrahend, difference):
  56. minuend_copy = copy.deepcopy(minuend)
  57. subtrahend_copy = copy.deepcopy(subtrahend)
  58. assert (minuend - subtrahend) == difference
  59. assert minuend_copy == minuend
  60. assert subtrahend_copy == subtrahend
  61. @pytest.mark.parametrize(('minuend', 'subtrahend'), [
  62. [dingguo.Sum(5.0, u'USD'), dingguo.Sum(2.0, u'EUR')],
  63. [dingguo.ScalarFigure(5.0, u'cm'), dingguo.ScalarFigure(2.0, u'kg')],
  64. ])
  65. def test_figure_sub_fail(minuend, subtrahend):
  66. with pytest.raises(Exception):
  67. (minuend - subtrahend)