test_yaml.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_discount_a():
  27. return dingguo.Discount(
  28. name = u'discount a',
  29. amount = get_sum_a(),
  30. )
  31. def get_discount_b():
  32. return dingguo.Discount(
  33. name = u'discount β',
  34. amount = get_sum_b(),
  35. )
  36. def get_order_a():
  37. order = dingguo.Order(
  38. platform = u'platformπ',
  39. order_id = u'id',
  40. order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  41. customer_id = u'customer',
  42. )
  43. order.items.append(get_item_a())
  44. order.items.append(get_item_b())
  45. order.discounts.append(get_discount_a())
  46. order.discounts.append(get_discount_b())
  47. return order
  48. def to_yaml(data):
  49. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  50. def yaml_diff(a, b):
  51. return '\n'.join(difflib.ndiff(
  52. to_yaml(a).split('\n'),
  53. to_yaml(b).split('\n'),
  54. ))
  55. def test_figure_to_yaml():
  56. assert to_yaml(get_figure_a()) == u"""!figure
  57. unit: km
  58. value: 12.3
  59. """
  60. def test_figure_to_yaml_unicode():
  61. assert to_yaml(get_figure_b()) == u"""!figure
  62. unit: 米
  63. value: 12300
  64. """
  65. def test_figure_from_yaml():
  66. assert get_figure_a() == yaml.load(u"""!figure
  67. unit: km
  68. value: 12.3
  69. """)
  70. def test_figure_to_yaml_unicode():
  71. assert get_figure_b() == yaml.load(u"""!figure
  72. unit: 米
  73. value: 12300
  74. """)
  75. def test_sum_to_yaml_a():
  76. assert to_yaml(get_sum_a()) == u"!sum 'EUR 1.23'\n"
  77. def test_sum_to_yaml_b():
  78. assert to_yaml(get_sum_b()) == u"!sum 'EUR 20.45'\n"
  79. def test_sum_from_yaml_a():
  80. assert get_sum_a() == yaml.load(u"!sum EUR 1.23")
  81. def test_sum_from_yaml_a_sign():
  82. assert get_sum_a() == yaml.load(u"!sum € 1.23")
  83. def test_sum_from_yaml_a_quotes():
  84. assert get_sum_a() == yaml.load(u"!sum 'EUR 1.23'")
  85. def test_item_to_yaml_a():
  86. assert to_yaml(get_item_a()) == u"""!item
  87. name: item a
  88. price_brutto: !sum 'EUR 1.23'
  89. """
  90. def test_item_to_yaml_b():
  91. assert to_yaml(get_item_b()) == u"""!item
  92. name: item β
  93. price_brutto: !sum 'EUR 20.45'
  94. """
  95. def test_item_from_yaml_a():
  96. assert get_item_a() == yaml.load(u"""!item
  97. name: item a
  98. price_brutto: !sum 'EUR 1.23'
  99. """)
  100. def test_item_from_yaml_b():
  101. assert get_item_b() == yaml.load(u"""!item
  102. name: item β
  103. price_brutto: !sum 'EUR 20.45'
  104. """)
  105. def test_discount_to_yaml_a():
  106. assert to_yaml(get_discount_a()) == u"""!discount
  107. amount: !sum 'EUR 1.23'
  108. name: discount a
  109. """
  110. def test_discount_to_yaml_b():
  111. assert to_yaml(get_discount_b()) == u"""!discount
  112. amount: !sum 'EUR 20.45'
  113. name: discount β
  114. """
  115. def test_discount_from_yaml_a():
  116. assert get_discount_a() == yaml.load(u"""!discount
  117. name: discount a
  118. amount: !sum 'EUR 1.23'
  119. """)
  120. def test_discount_from_yaml_b():
  121. assert get_discount_b() == yaml.load(u"""!discount
  122. name: discount β
  123. amount: !sum 'EUR 20.45'
  124. """)
  125. def test_order_to_yaml():
  126. assert to_yaml(get_order_a()) == u"""!order
  127. customer_id: customer
  128. discounts:
  129. - !discount
  130. amount: !sum 'EUR 1.23'
  131. name: discount a
  132. - !discount
  133. amount: !sum 'EUR 20.45'
  134. name: discount β
  135. items:
  136. - !item
  137. name: item a
  138. price_brutto: !sum 'EUR 1.23'
  139. - !item
  140. name: item β
  141. price_brutto: !sum 'EUR 20.45'
  142. order_date: 2016-05-08
  143. order_id: id
  144. platform: platformπ
  145. """
  146. def test_order_from_yaml():
  147. order_loaded = yaml.load(u"""!order
  148. customer_id: customer
  149. discounts:
  150. - !discount
  151. amount: !sum 'EUR 1.23'
  152. name: discount a
  153. - !discount
  154. amount: !sum 'EUR 20.45'
  155. name: discount β
  156. items:
  157. - !item
  158. name: item a
  159. price_brutto: !sum 'EUR 1.23'
  160. - !item
  161. name: item β
  162. price_brutto: !sum 'EUR 20.45'
  163. order_date: 2016-05-08
  164. order_id: id
  165. platform: platformπ
  166. """)
  167. order_expected = get_order_a()
  168. assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)