test_yaml.py 6.2 KB

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