test_yaml.py 7.3 KB

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