test_yaml.py 9.7 KB

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