test_yaml.py 7.3 KB

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