test_yaml.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_article():
  27. return dingguo.Article(
  28. authors = ['a', 'b'],
  29. name = u'article name',
  30. price_brutto = get_sum_a(),
  31. quantity = 1,
  32. reseller = u'seller',
  33. shipper = u'shipper',
  34. state = u'goood',
  35. )
  36. def get_discount_a():
  37. return dingguo.Discount(
  38. name = u'discount a',
  39. amount = get_sum_a(),
  40. )
  41. def get_discount_b():
  42. return dingguo.Discount(
  43. name = u'discount β',
  44. amount = get_sum_b(),
  45. )
  46. def get_order_a():
  47. order = dingguo.Order(
  48. platform = u'platformπ',
  49. order_id = u'id',
  50. order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  51. customer_id = u'customer',
  52. )
  53. order.items.append(get_item_a())
  54. order.items.append(get_item_b())
  55. order.discounts.append(get_discount_a())
  56. order.discounts.append(get_discount_b())
  57. return order
  58. def get_distance():
  59. return dingguo.Distance(2.4142, u'km')
  60. def to_yaml(data):
  61. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  62. def yaml_diff(a, b):
  63. return '\n'.join(difflib.ndiff(
  64. to_yaml(a).split('\n'),
  65. to_yaml(b).split('\n'),
  66. ))
  67. def test_figure_to_yaml():
  68. assert to_yaml(get_figure_a()) == u"""!figure
  69. unit: km
  70. value: 12.3
  71. """
  72. def test_figure_to_yaml_unicode():
  73. assert to_yaml(get_figure_b()) == u"""!figure
  74. unit: 米
  75. value: 12300
  76. """
  77. def test_figure_from_yaml():
  78. assert get_figure_a() == yaml.load(u"""!figure
  79. unit: km
  80. value: 12.3
  81. """)
  82. def test_figure_to_yaml_unicode():
  83. assert get_figure_b() == yaml.load(u"""!figure
  84. unit: 米
  85. value: 12300
  86. """)
  87. def test_sum_to_yaml_a():
  88. assert to_yaml(get_sum_a()) == u"!sum '1.23 EUR'\n"
  89. def test_sum_to_yaml_b():
  90. assert to_yaml(get_sum_b()) == u"!sum '20.45 EUR'\n"
  91. def test_sum_from_yaml_a():
  92. assert get_sum_a() == yaml.load(u"!sum 1.23 EUR")
  93. def test_sum_from_yaml_a_sign():
  94. assert get_sum_a() == yaml.load(u"!sum 1.23 €")
  95. def test_sum_from_yaml_a_quotes():
  96. assert get_sum_a() == yaml.load(u"!sum '1.23 EUR'")
  97. def test_item_to_yaml_a():
  98. assert to_yaml(get_item_a()) == u"""!item
  99. name: item a
  100. price_brutto: !sum '1.23 EUR'
  101. """
  102. def test_item_to_yaml_b():
  103. assert to_yaml(get_item_b()) == u"""!item
  104. name: item β
  105. price_brutto: !sum '20.45 EUR'
  106. """
  107. def test_item_from_yaml_a():
  108. assert get_item_a() == yaml.load(u"""!item
  109. name: item a
  110. price_brutto: !sum '1.23 EUR'
  111. """)
  112. def test_item_from_yaml_b():
  113. assert get_item_b() == yaml.load(u"""!item
  114. name: item β
  115. price_brutto: !sum '20.45 EUR'
  116. """)
  117. def test_discount_to_yaml_a():
  118. assert to_yaml(get_discount_a()) == u"""!discount
  119. amount: !sum '1.23 EUR'
  120. name: discount a
  121. """
  122. def test_discount_to_yaml_b():
  123. assert to_yaml(get_discount_b()) == u"""!discount
  124. amount: !sum '20.45 EUR'
  125. name: discount β
  126. """
  127. def test_discount_from_yaml_a():
  128. assert get_discount_a() == yaml.load(u"""!discount
  129. name: discount a
  130. amount: !sum '1.23 EUR'
  131. """)
  132. def test_discount_from_yaml_b():
  133. assert get_discount_b() == yaml.load(u"""!discount
  134. name: discount β
  135. amount: !sum '20.45 EUR'
  136. """)
  137. def test_order_to_yaml():
  138. assert to_yaml(get_order_a()) == u"""!order
  139. customer_id: customer
  140. discounts:
  141. - !discount
  142. amount: !sum '1.23 EUR'
  143. name: discount a
  144. - !discount
  145. amount: !sum '20.45 EUR'
  146. name: discount β
  147. items:
  148. - !item
  149. name: item a
  150. price_brutto: !sum '1.23 EUR'
  151. - !item
  152. name: item β
  153. price_brutto: !sum '20.45 EUR'
  154. order_date: 2016-05-08
  155. order_id: id
  156. platform: platformπ
  157. """
  158. def test_order_from_yaml():
  159. order_loaded = yaml.load(u"""!order
  160. customer_id: customer
  161. discounts:
  162. - !discount
  163. amount: !sum '1.23 EUR'
  164. name: discount a
  165. - !discount
  166. amount: !sum '20.45 EUR'
  167. name: discount β
  168. items:
  169. - !item
  170. name: item a
  171. price_brutto: !sum '1.23 EUR'
  172. - !item
  173. name: item β
  174. price_brutto: !sum '20.45 EUR'
  175. order_date: 2016-05-08
  176. order_id: id
  177. platform: platformπ
  178. """)
  179. order_expected = get_order_a()
  180. assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)
  181. def test_scalar_figure_to_yaml():
  182. assert to_yaml(dingguo.ScalarFigure(1.34, u'μm')) == u"!scalar '1.34 μm'\n"
  183. def test_scalar_figure_from_yaml():
  184. assert dingguo.ScalarFigure(1.34, u'μm') == yaml.load(u"!scalar '1.34 μm'")
  185. def test_distance_to_yaml():
  186. assert to_yaml(dingguo.Distance(1.34, u'km')) == u"!distance '1.34 km'\n"
  187. def test_distance_from_yaml():
  188. assert dingguo.Distance(1.34, u'km') == yaml.load(u"!distance '1.34 km'\n")
  189. def test_article_to_yaml_a():
  190. assert to_yaml(get_article()) == u"""!article
  191. authors:
  192. - a
  193. - b
  194. delivery_date: null
  195. name: article name
  196. price_brutto: !sum '1.23 EUR'
  197. quantity: 1
  198. reseller: seller
  199. shipper: shipper
  200. state: goood
  201. """
  202. def test_article_to_yaml_a():
  203. assert get_article() == yaml.load(u"""!article
  204. authors:
  205. - a
  206. - b
  207. delivery_date: null
  208. name: article name
  209. price_brutto: !sum '1.23 EUR'
  210. quantity: 1
  211. reseller: seller
  212. shipper: shipper
  213. state: goood
  214. """)