test_yaml.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import os
  4. import yaml
  5. import dingguo
  6. import datetime
  7. project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
  8. test_data_path = os.path.join(project_root_path, 'tests', 'data')
  9. def get_figure_a():
  10. return dingguo.Figure(12.3, u'km')
  11. def get_figure_b():
  12. return dingguo.Figure(12300, u'米')
  13. def get_sum_a():
  14. return dingguo.Sum(1.23, u'EUR')
  15. def get_sum_b():
  16. return dingguo.Sum(20.45, u'€')
  17. def to_yaml(data):
  18. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  19. def test_figure_to_yaml():
  20. assert to_yaml(get_figure_a()) == u"""!figure
  21. unit: km
  22. value: 12.3
  23. """
  24. def test_figure_to_yaml_unicode():
  25. assert to_yaml(get_figure_b()) == u"""!figure
  26. unit: 米
  27. value: 12300
  28. """
  29. def test_figure_from_yaml():
  30. assert get_figure_a() == yaml.load(u"""!figure
  31. unit: km
  32. value: 12.3
  33. """)
  34. def test_figure_to_yaml_unicode():
  35. assert get_figure_b() == yaml.load(u"""!figure
  36. unit: 米
  37. value: 12300
  38. """)
  39. def test_sum_to_yaml_a():
  40. assert to_yaml(get_sum_a()) == u"!sum 'EUR 1.23'\n"
  41. def test_sum_to_yaml_b():
  42. assert to_yaml(get_sum_b()) == u"!sum 'EUR 20.45'\n"
  43. def test_sum_from_yaml_a():
  44. assert get_sum_a() == yaml.load(u"!sum EUR 1.23")
  45. def test_sum_from_yaml_a_sign():
  46. assert get_sum_a() == yaml.load(u"!sum € 1.23")
  47. def test_sum_from_yaml_a_quotes():
  48. assert get_sum_a() == yaml.load(u"!sum 'EUR 1.23'")