test_yaml.py 9.8 KB

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