test_yaml.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import os
  4. import yaml
  5. import dingguo
  6. import difflib
  7. import datetime
  8. def get_figure_a():
  9. return dingguo.Figure(12.3, u'km')
  10. def get_figure_b():
  11. return dingguo.Figure(12300, u'米')
  12. def get_sum_a():
  13. return dingguo.Sum(1.23, u'EUR')
  14. def get_sum_b():
  15. return dingguo.Sum(20.45, u'€')
  16. def get_item_a():
  17. return dingguo.Item(
  18. name = u'item a',
  19. price_brutto = get_sum_a(),
  20. )
  21. def get_item_b():
  22. return dingguo.Item(
  23. name = u'item β',
  24. price_brutto = get_sum_b(),
  25. )
  26. def get_order_a():
  27. order = dingguo.Order(
  28. platform = u'platformπ',
  29. order_id = u'id',
  30. order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  31. customer_id = u'customer',
  32. )
  33. order.items.append(get_item_a())
  34. order.items.append(get_item_b())
  35. return order
  36. def to_yaml(data):
  37. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  38. def yaml_diff(a, b):
  39. return '\n'.join(difflib.ndiff(
  40. to_yaml(a).split('\n'),
  41. to_yaml(b).split('\n'),
  42. ))
  43. def test_figure_to_yaml():
  44. assert to_yaml(get_figure_a()) == u"""!figure
  45. unit: km
  46. value: 12.3
  47. """
  48. def test_figure_to_yaml_unicode():
  49. assert to_yaml(get_figure_b()) == u"""!figure
  50. unit: 米
  51. value: 12300
  52. """
  53. def test_figure_from_yaml():
  54. assert get_figure_a() == yaml.load(u"""!figure
  55. unit: km
  56. value: 12.3
  57. """)
  58. def test_figure_to_yaml_unicode():
  59. assert get_figure_b() == yaml.load(u"""!figure
  60. unit: 米
  61. value: 12300
  62. """)
  63. def test_sum_to_yaml_a():
  64. assert to_yaml(get_sum_a()) == u"!sum 'EUR 1.23'\n"
  65. def test_sum_to_yaml_b():
  66. assert to_yaml(get_sum_b()) == u"!sum 'EUR 20.45'\n"
  67. def test_sum_from_yaml_a():
  68. assert get_sum_a() == yaml.load(u"!sum EUR 1.23")
  69. def test_sum_from_yaml_a_sign():
  70. assert get_sum_a() == yaml.load(u"!sum € 1.23")
  71. def test_sum_from_yaml_a_quotes():
  72. assert get_sum_a() == yaml.load(u"!sum 'EUR 1.23'")
  73. def test_item_to_yaml_a():
  74. assert to_yaml(get_item_a()) == u"""!item
  75. name: item a
  76. price_brutto: !sum 'EUR 1.23'
  77. """
  78. def test_item_to_yaml_b():
  79. assert to_yaml(get_item_b()) == u"""!item
  80. name: item β
  81. price_brutto: !sum 'EUR 20.45'
  82. """
  83. def test_item_from_yaml_a():
  84. assert get_item_a() == yaml.load(u"""!item
  85. name: item a
  86. price_brutto: !sum 'EUR 1.23'
  87. """)
  88. def test_item_from_yaml_b():
  89. assert get_item_b() == yaml.load(u"""!item
  90. name: item β
  91. price_brutto: !sum 'EUR 20.45'
  92. """)
  93. def test_order_to_yaml():
  94. assert to_yaml(get_order_a()) == u"""!order
  95. customer_id: customer
  96. discounts: []
  97. items:
  98. - !item
  99. name: item a
  100. price_brutto: !sum 'EUR 1.23'
  101. - !item
  102. name: item β
  103. price_brutto: !sum 'EUR 20.45'
  104. order_date: 2016-05-08
  105. order_id: id
  106. platform: platformπ
  107. """
  108. def test_order_from_yaml():
  109. order_loaded = yaml.load(u"""!order
  110. customer_id: customer
  111. discounts: []
  112. items:
  113. - !item
  114. name: item a
  115. price_brutto: !sum 'EUR 1.23'
  116. - !item
  117. name: item β
  118. price_brutto: !sum 'EUR 20.45'
  119. order_date: 2016-05-08
  120. order_id: id
  121. platform: platformπ
  122. """)
  123. order_expected = get_order_a()
  124. assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)