test_yaml.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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(items = True, discounts = True):
  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. if items:
  74. order.items.append(get_item_a())
  75. order.items.append(get_item_b())
  76. if discounts:
  77. order.discounts.append(get_discount_a())
  78. order.discounts.append(get_discount_b())
  79. return order
  80. def get_order_b():
  81. order = dingguo.Order(
  82. platform = u'platformπ',
  83. order_id = u'order_b',
  84. order_date = datetime.datetime(2015, 5, 8, 0, 18, 17),
  85. )
  86. return order
  87. def get_order_c():
  88. order = dingguo.Order(
  89. platform = u'γάμμα',
  90. order_id = u'order_βήτα',
  91. order_date = datetime.datetime(2014, 5, 8, 0, 18, 17),
  92. customer_id = u'ρώ',
  93. )
  94. return order
  95. def get_distance():
  96. return dingguo.Distance(2.4142, u'km')
  97. def get_order_registry():
  98. registry = dingguo.OrderRegistry()
  99. registry.register(get_order_a(items = False, discounts = False))
  100. registry.register(get_order_b())
  101. registry.register(get_order_c())
  102. return registry
  103. def to_yaml(data):
  104. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  105. def yaml_diff(a, b):
  106. return '\n'.join(difflib.ndiff(
  107. to_yaml(a).split('\n'),
  108. to_yaml(b).split('\n'),
  109. ))
  110. def test_figure_to_yaml():
  111. assert to_yaml(get_figure_a()) == u"""!figure
  112. unit: km
  113. value: 12.3
  114. """
  115. def test_figure_to_yaml_unicode():
  116. assert to_yaml(get_figure_b()) == u"""!figure
  117. unit: 米
  118. value: 12300
  119. """
  120. def test_figure_from_yaml():
  121. assert get_figure_a() == yaml.load(u"""!figure
  122. unit: km
  123. value: 12.3
  124. """)
  125. def test_figure_to_yaml_unicode():
  126. assert get_figure_b() == yaml.load(u"""!figure
  127. unit: 米
  128. value: 12300
  129. """)
  130. def test_sum_to_yaml_a():
  131. assert to_yaml(get_sum_a()) == u"!sum '1.23 EUR'\n"
  132. def test_sum_to_yaml_b():
  133. assert to_yaml(get_sum_b()) == u"!sum '20.45 EUR'\n"
  134. def test_sum_from_yaml_a():
  135. assert get_sum_a() == yaml.load(u"!sum 1.23 EUR")
  136. def test_sum_from_yaml_a_sign():
  137. assert get_sum_a() == yaml.load(u"!sum 1.23 €")
  138. def test_sum_from_yaml_a_quotes():
  139. assert get_sum_a() == yaml.load(u"!sum '1.23 EUR'")
  140. def test_item_to_yaml_a():
  141. assert to_yaml(get_item_a()) == u"""!item
  142. name: item a
  143. price_brutto: !sum '1.23 EUR'
  144. """
  145. def test_item_to_yaml_b():
  146. assert to_yaml(get_item_b()) == u"""!item
  147. name: item β
  148. price_brutto: !sum '20.45 EUR'
  149. """
  150. def test_item_from_yaml_a():
  151. assert get_item_a() == yaml.load(u"""!item
  152. name: item a
  153. price_brutto: !sum '1.23 EUR'
  154. """)
  155. def test_item_from_yaml_b():
  156. assert get_item_b() == yaml.load(u"""!item
  157. name: item β
  158. price_brutto: !sum '20.45 EUR'
  159. """)
  160. def test_discount_to_yaml_a():
  161. assert to_yaml(get_discount_a()) == u"""!discount
  162. amount: !sum '1.23 EUR'
  163. name: discount a
  164. """
  165. def test_discount_to_yaml_b():
  166. assert to_yaml(get_discount_b()) == u"""!discount
  167. amount: !sum '20.45 EUR'
  168. name: discount β
  169. """
  170. def test_discount_from_yaml_a():
  171. assert get_discount_a() == yaml.load(u"""!discount
  172. name: discount a
  173. amount: !sum '1.23 EUR'
  174. """)
  175. def test_discount_from_yaml_b():
  176. assert get_discount_b() == yaml.load(u"""!discount
  177. name: discount β
  178. amount: !sum '20.45 EUR'
  179. """)
  180. def test_order_to_yaml():
  181. assert to_yaml(get_order_a()) == u"""!order
  182. customer_id: customer
  183. discounts:
  184. - !discount
  185. amount: !sum '1.23 EUR'
  186. name: discount a
  187. - !discount
  188. amount: !sum '20.45 EUR'
  189. name: discount β
  190. items:
  191. - !item
  192. name: item a
  193. price_brutto: !sum '1.23 EUR'
  194. - !item
  195. name: item β
  196. price_brutto: !sum '20.45 EUR'
  197. order_date: 2016-05-08
  198. order_id: id
  199. platform: platformπ
  200. """
  201. def test_order_from_yaml():
  202. order_loaded = yaml.load(u"""!order
  203. customer_id: customer
  204. discounts:
  205. - !discount
  206. amount: !sum '1.23 EUR'
  207. name: discount a
  208. - !discount
  209. amount: !sum '20.45 EUR'
  210. name: discount β
  211. items:
  212. - !item
  213. name: item a
  214. price_brutto: !sum '1.23 EUR'
  215. - !item
  216. name: item β
  217. price_brutto: !sum '20.45 EUR'
  218. order_date: 2016-05-08
  219. order_id: id
  220. platform: platformπ
  221. """)
  222. order_expected = get_order_a()
  223. assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)
  224. def test_scalar_figure_to_yaml():
  225. assert to_yaml(dingguo.ScalarFigure(1.34, u'μm')) == u"!scalar '1.34 μm'\n"
  226. def test_scalar_figure_from_yaml():
  227. assert dingguo.ScalarFigure(1.34, u'μm') == yaml.load(u"!scalar '1.34 μm'")
  228. def test_distance_to_yaml():
  229. assert to_yaml(dingguo.Distance(1.34, u'km')) == u"!distance '1.34 km'\n"
  230. def test_distance_from_yaml():
  231. assert dingguo.Distance(1.34, u'km') == yaml.load(u"!distance '1.34 km'\n")
  232. def test_article_to_yaml():
  233. assert to_yaml(get_article()) == u"""!article
  234. authors:
  235. - a
  236. - b
  237. delivery_date: null
  238. name: article name
  239. price_brutto: !sum '1.23 EUR'
  240. product_id: 0815
  241. quantity: 1
  242. reseller: seller
  243. shipper: shipper
  244. state: goood
  245. """
  246. def test_article_from_yaml():
  247. assert get_article() == yaml.load(u"""!article
  248. authors:
  249. - a
  250. - b
  251. delivery_date: null
  252. name: article name
  253. price_brutto: !sum '1.23 EUR'
  254. product_id: 0815
  255. quantity: 1
  256. reseller: seller
  257. shipper: shipper
  258. state: goood
  259. """)
  260. def test_transportation_to_yaml():
  261. assert to_yaml(get_transportation()) == u"""!transportation
  262. departure_point: home
  263. destination_point: city
  264. distance: !distance '3.21 km'
  265. name: ticket
  266. price_brutto: !sum '1.23 EUR'
  267. route_map: null
  268. """
  269. def test_transportation_from_yaml():
  270. assert get_transportation() == yaml.load(u"""!transportation
  271. departure_point: home
  272. destination_point: city
  273. distance: !distance '3.21 km'
  274. name: ticket
  275. price_brutto: !sum '1.23 EUR'
  276. route_map: null
  277. """)
  278. def test_taxi_ride_to_yaml():
  279. assert to_yaml(get_taxi_ride()) == u"""!taxi-ride
  280. arrival_time: 2016-05-02 18:10:00
  281. departure_point: home
  282. departure_time: 2016-05-02 18:25:00
  283. destination_point: city
  284. distance: !distance '3.21 km'
  285. driver: driver
  286. name: taxi ride
  287. price_brutto: !sum '1.23 EUR'
  288. route_map: null
  289. """
  290. def test_taxi_ride_from_yaml():
  291. assert get_taxi_ride() == yaml.load(u"""!taxi-ride
  292. arrival_time: 2016-05-02 18:10:00
  293. departure_point: home
  294. departure_time: 2016-05-02 18:25:00
  295. destination_point: city
  296. distance: !distance '3.21 km'
  297. driver: driver
  298. name: taxi ride
  299. price_brutto: !sum '1.23 EUR'
  300. route_map: null
  301. """)
  302. def test_order_registry_to_yaml():
  303. assert to_yaml(get_order_registry()) == u"""!order-registry
  304. platformπ:
  305. id: !order
  306. customer_id: customer
  307. discounts: []
  308. items: []
  309. order_date: 2016-05-08
  310. order_id: id
  311. platform: platformπ
  312. order_b: !order
  313. customer_id: null
  314. discounts: []
  315. items: []
  316. order_date: 2015-05-08
  317. order_id: order_b
  318. platform: platformπ
  319. γάμμα:
  320. order_βήτα: !order
  321. customer_id: ρώ
  322. discounts: []
  323. items: []
  324. order_date: 2014-05-08
  325. order_id: order_βήτα
  326. platform: γάμμα
  327. """
  328. def test_order_registry_from_yaml():
  329. expected = get_order_registry()
  330. loaded = yaml.load(u"""!order-registry
  331. platformπ:
  332. id: !order
  333. customer_id: customer
  334. discounts: []
  335. order_date: 2016-05-08
  336. order_id: id
  337. platform: platformπ
  338. order_b: !order
  339. customer_id: null
  340. items: []
  341. order_date: 2015-05-08
  342. order_id: order_b
  343. platform: platformπ
  344. γάμμα:
  345. order_βήτα: !order
  346. customer_id: ρώ
  347. discounts: []
  348. items: []
  349. order_date: 2014-05-08
  350. order_id: order_βήτα
  351. platform: γάμμα
  352. """)
  353. assert expected == loaded, yaml_diff(expected, loaded)