test_yaml.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import os
  4. import pytz
  5. import yaml
  6. import dingguo
  7. import difflib
  8. import datetime
  9. def get_figure_a():
  10. return dingguo.Figure(12.3, u'km')
  11. def get_figure_b():
  12. return dingguo.Figure(12300, u'米')
  13. def get_sum_a():
  14. return dingguo.Sum(1.23, u'EUR')
  15. def get_sum_b():
  16. return dingguo.Sum(20.45, u'€')
  17. def get_item_a():
  18. return dingguo.Item(
  19. name = u'item a',
  20. price_brutto = get_sum_a(),
  21. )
  22. def get_item_b():
  23. return dingguo.Item(
  24. name = u'item β',
  25. price_brutto = get_sum_b(),
  26. )
  27. def get_person_a():
  28. return dingguo.Person(
  29. first_name = u'Fabian Peter',
  30. last_name = u'Hammerle',
  31. )
  32. def get_person_b():
  33. return dingguo.Person(
  34. first_name = u'名字',
  35. last_name = u'贵姓',
  36. )
  37. def get_campaign():
  38. return dingguo.Campaign(
  39. name = u'campaign a',
  40. founder = u'company',
  41. end = datetime.datetime(2016, 7, 23, 9, 23, 17, tzinfo = pytz.timezone('Europe/Vienna')),
  42. )
  43. def get_pledge():
  44. return dingguo.Pledge(
  45. campaign = get_campaign(),
  46. price_brutto = dingguo.Sum(10.0, u'EUR'),
  47. reward = u'great',
  48. )
  49. def get_article():
  50. return dingguo.Article(
  51. authors = ['a', 'b'],
  52. depth = dingguo.ScalarFigure(12.3, u'dm'),
  53. features = u'supergeil',
  54. height = dingguo.ScalarFigure(123., u'cm'),
  55. maximum_load = dingguo.ScalarFigure(40., u'kg'),
  56. name = u'article name',
  57. price_brutto = get_sum_a(),
  58. product_id = u'0815',
  59. quantity = 1,
  60. reseller = u'seller',
  61. shipper = u'shipper',
  62. state = u'goood',
  63. width = dingguo.ScalarFigure(1.23, u'm'),
  64. )
  65. def get_transportation():
  66. return dingguo.Transportation(
  67. name = u'ticket',
  68. price_brutto = get_sum_a(),
  69. departure_point = u'home',
  70. destination_point = u'city',
  71. distance = dingguo.Distance(3.21, u'km'),
  72. passenger = get_person_a(),
  73. valid_from = datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')),
  74. valid_until = datetime.datetime(2016, 7, 14, 18, 50, 4, 0, tzinfo = pytz.utc),
  75. ticket_url = u'https://www.example.com',
  76. )
  77. def get_shipping():
  78. return dingguo.Shipping(
  79. price_brutto = get_sum_a(),
  80. destination_point = u'home',
  81. )
  82. def get_taxi_ride():
  83. return dingguo.TaxiRide(
  84. name = u'taxi ride',
  85. price_brutto = get_sum_a(),
  86. departure_point = u'home',
  87. destination_point = u'city',
  88. distance = dingguo.Distance(3.21, u'km'),
  89. driver = u'driver',
  90. arrival_time = datetime.datetime(2016, 5, 2, 18, 10, tzinfo = pytz.timezone('Europe/Vienna')),
  91. departure_time = datetime.datetime(2016, 5, 2, 18, 25, tzinfo = pytz.timezone('Europe/Vienna')),
  92. )
  93. def get_discount_a():
  94. return dingguo.Discount(
  95. name = u'discount a',
  96. amount = get_sum_a(),
  97. )
  98. def get_discount_b():
  99. return dingguo.Discount(
  100. name = u'discount β',
  101. amount = get_sum_b(),
  102. )
  103. def get_order_a(items = True, discounts = True):
  104. order = dingguo.Order(
  105. platform = u'platformπ',
  106. order_id = u'id',
  107. order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  108. customer_id = u'customer',
  109. )
  110. if items:
  111. order.items.append(get_item_a())
  112. order.items.append(get_item_b())
  113. if discounts:
  114. order.discounts.append(get_discount_a())
  115. order.discounts.append(get_discount_b())
  116. return order
  117. def get_order_b():
  118. order = dingguo.Order(
  119. platform = u'platformπ',
  120. order_id = u'order_b',
  121. order_date = datetime.datetime(2015, 5, 8, 0, 18, 17),
  122. )
  123. return order
  124. def get_order_c():
  125. order = dingguo.Order(
  126. platform = u'γάμμα',
  127. order_id = u'order_βήτα',
  128. order_date = datetime.datetime(2014, 5, 8, 0, 18, 17),
  129. customer_id = u'ρώ',
  130. )
  131. return order
  132. def get_distance():
  133. return dingguo.Distance(2.4142, u'km')
  134. def get_order_registry():
  135. registry = dingguo.OrderRegistry()
  136. registry.register(get_order_a(items = False, discounts = False))
  137. registry.register(get_order_b())
  138. registry.register(get_order_c())
  139. return registry
  140. def to_yaml(data):
  141. return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
  142. def yaml_diff(a, b):
  143. return '\n'.join(difflib.ndiff(
  144. to_yaml(a).split('\n'),
  145. to_yaml(b).split('\n'),
  146. ))
  147. @pytest.mark.parametrize('source_object,expected_yaml', [
  148. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04\n...\n'],
  149. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')), '2016-07-14 13:50:04+01:05\n...\n'],
  150. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.utc), '2016-07-14 13:50:04+00:00\n...\n'],
  151. [dingguo.Distance(1.34, u'km'), u"!distance '1.34 km'\n"],
  152. [dingguo.ScalarFigure(1.34, u'μm'), u"!scalar '1.34 μm'\n"],
  153. [get_discount_a(), u"!discount\namount: !sum '1.23 EUR'\nname: discount a\n"],
  154. [get_discount_b(), u"!discount\namount: !sum '20.45 EUR'\nname: discount β\n"],
  155. [get_figure_a(), u'!figure\nunit: km\nvalue: 12.3\n'],
  156. [get_figure_b(), u'!figure\nunit: 米\nvalue: 12300\n'],
  157. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum '1.23 EUR'\n"],
  158. [get_item_b(), u"!item\nname: item β\nprice_brutto: !sum '20.45 EUR'\n"],
  159. [get_person_a(), u'!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  160. [get_person_b(), u'!person\nfirst_name: 名字\nlast_name: 贵姓\n'],
  161. [get_sum_a(), u"!sum '1.23 EUR'\n"],
  162. [get_sum_b(), u"!sum '20.45 EUR'\n"],
  163. [get_campaign(), u"""!campaign
  164. end: 2016-07-23 09:23:17+01:05
  165. founder: company
  166. name: campaign a
  167. """],
  168. [get_pledge(), u"""!pledge
  169. campaign: !campaign
  170. end: 2016-07-23 09:23:17+01:05
  171. founder: company
  172. name: campaign a
  173. price_brutto: !sum '10.0 EUR'
  174. reward: great
  175. """],
  176. [get_order_a(), u"""!order
  177. customer_id: customer
  178. discounts:
  179. - !discount
  180. amount: !sum '1.23 EUR'
  181. name: discount a
  182. - !discount
  183. amount: !sum '20.45 EUR'
  184. name: discount β
  185. items:
  186. - !item
  187. name: item a
  188. price_brutto: !sum '1.23 EUR'
  189. - !item
  190. name: item β
  191. price_brutto: !sum '20.45 EUR'
  192. order_date: 2016-05-08
  193. order_id: id
  194. platform: platformπ
  195. """],
  196. [get_article(), u"""!article
  197. authors:
  198. - a
  199. - b
  200. delivery_date: null
  201. depth: !scalar '12.3 dm'
  202. features: supergeil
  203. height: !scalar '123.0 cm'
  204. maximum_load: !scalar '40.0 kg'
  205. name: article name
  206. price_brutto: !sum '1.23 EUR'
  207. product_id: 0815
  208. quantity: 1
  209. reseller: seller
  210. shipper: shipper
  211. state: goood
  212. width: !scalar '1.23 m'
  213. """],
  214. [get_transportation(), u"""!transportation
  215. departure_point: home
  216. destination_point: city
  217. distance: !distance '3.21 km'
  218. name: ticket
  219. passenger: !person
  220. first_name: Fabian Peter
  221. last_name: Hammerle
  222. price_brutto: !sum '1.23 EUR'
  223. ticket_url: https://www.example.com
  224. valid_from: 2016-07-14 13:50:04+01:05
  225. valid_until: 2016-07-14 18:50:04+00:00
  226. """],
  227. [get_shipping(), u"""!shipping
  228. destination_point: home
  229. price_brutto: !sum '1.23 EUR'
  230. """],
  231. [get_taxi_ride(), u"""!taxi-ride
  232. arrival_time: 2016-05-02 18:10:00+01:05
  233. departure_point: home
  234. departure_time: 2016-05-02 18:25:00+01:05
  235. destination_point: city
  236. distance: !distance '3.21 km'
  237. driver: driver
  238. name: taxi ride
  239. price_brutto: !sum '1.23 EUR'
  240. """],
  241. [get_order_registry(), u"""!order-registry
  242. platformπ:
  243. id: !order
  244. customer_id: customer
  245. discounts: []
  246. items: []
  247. order_date: 2016-05-08
  248. order_id: id
  249. platform: platformπ
  250. order_b: !order
  251. discounts: []
  252. items: []
  253. order_date: 2015-05-08
  254. order_id: order_b
  255. platform: platformπ
  256. γάμμα:
  257. order_βήτα: !order
  258. customer_id: ρώ
  259. discounts: []
  260. items: []
  261. order_date: 2014-05-08
  262. order_id: order_βήτα
  263. platform: γάμμα
  264. """],
  265. ])
  266. def test_to_yaml(source_object, expected_yaml):
  267. assert to_yaml(source_object) == expected_yaml
  268. @pytest.mark.parametrize('expected_object,source_yaml', [
  269. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04'],
  270. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')), '2016-07-14 13:50:04+01:05'],
  271. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.utc), '2016-07-14 13:50:04+00:00'],
  272. [dingguo.Distance(1.34, u'km'), u"!distance '1.34 km'\n"],
  273. [dingguo.ScalarFigure(1.34, u'μm'), u"!scalar '1.34 μm'"],
  274. [get_discount_a(), u"!discount\nname: discount a\namount: !sum '1.23 EUR'\n"],
  275. [get_discount_b(), u"!discount\nname: discount β\namount: !sum '20.45 EUR'\n"],
  276. [get_figure_a(), '!figure\nunit: km\nvalue: 12.3\n'],
  277. [get_figure_a(), u'!figure\nunit: km\nvalue: 12.3\n'],
  278. [get_figure_b(), '!figure\nunit: 米\nvalue: 12300\n'],
  279. [get_figure_b(), u'!figure\nunit: 米\nvalue: 12300\n'],
  280. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum '1.23 EUR'\n"],
  281. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum 1.23 EUR"],
  282. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum 1.23 €\n"],
  283. [get_item_b(), u"!item\nname: item β\nprice_brutto: !sum '20.45 EUR'\n"],
  284. [get_person_a(), '!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  285. [get_person_a(), u'!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  286. [get_person_b(), u'!person\nfirst_name: 名字\nlast_name: 贵姓\n'],
  287. [get_sum_a(), "!sum 1.23 €"],
  288. [get_sum_a(), u"!sum '1.23 EUR'"],
  289. [get_sum_a(), u"!sum '1.23 EUR'"],
  290. [get_sum_a(), u"!sum 1.23 EUR"],
  291. [get_campaign(), u"""!campaign
  292. name: campaign a
  293. founder: company
  294. end: 2016-07-23 09:23:17+01:05
  295. """],
  296. [get_pledge(), u"""!pledge
  297. campaign: !campaign
  298. name: campaign a
  299. founder: company
  300. end: 2016-07-23 09:23:17+01:05
  301. price_brutto: !sum '10.0 EUR'
  302. reward: great
  303. """],
  304. [get_sum_a(), u"!sum 1.23 €"],
  305. [[get_person_a(), get_person_b()], u"""
  306. - !person
  307. first_name: Fabian Peter
  308. last_name: Hammerle
  309. - !person
  310. first_name: 名字
  311. last_name: 贵姓"""],
  312. [get_transportation(), u"""!transportation
  313. departure_point: home
  314. destination_point: city
  315. distance: !distance '3.21 km'
  316. name: ticket
  317. price_brutto: !sum '1.23 EUR'
  318. route_map: null
  319. passenger: !person
  320. first_name: Fabian Peter
  321. last_name: Hammerle
  322. ticket_url: https://www.example.com
  323. valid_from: 2016-07-14 13:50:04+01:05
  324. valid_until: 2016-07-14 18:50:04+00:00
  325. """],
  326. [get_shipping(), u"""!shipping
  327. destination_point: home
  328. price_brutto: !sum 1.23 EUR
  329. """],
  330. ])
  331. def test_from_yaml(expected_object, source_yaml):
  332. loaded_object = yaml.load(source_yaml)
  333. assert loaded_object == expected_object
  334. def test_order_from_yaml():
  335. order_loaded = yaml.load(u"""!order
  336. customer_id: customer
  337. discounts:
  338. - !discount
  339. amount: !sum '1.23 EUR'
  340. name: discount a
  341. - !discount
  342. amount: !sum '20.45 EUR'
  343. name: discount β
  344. items:
  345. - !item
  346. name: item a
  347. price_brutto: !sum '1.23 EUR'
  348. - !item
  349. name: item β
  350. price_brutto: !sum '20.45 EUR'
  351. order_date: 2016-05-08
  352. order_id: id
  353. platform: platformπ
  354. """)
  355. order_expected = get_order_a()
  356. assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)
  357. def test_article_from_yaml():
  358. assert get_article() == yaml.load(u"""!article
  359. authors:
  360. - a
  361. - b
  362. delivery_date: null
  363. features: supergeil
  364. name: article name
  365. price_brutto: !sum '1.23 EUR'
  366. product_id: 0815
  367. quantity: 1
  368. reseller: seller
  369. shipper: shipper
  370. state: goood
  371. depth: !scalar 12.3 dm
  372. height: !scalar 123.0 cm
  373. maximum_load: !scalar 40.0 kg
  374. width: !scalar 1.23 m
  375. """)
  376. def test_taxi_ride_from_yaml():
  377. assert get_taxi_ride() == yaml.load(u"""!taxi-ride
  378. arrival_time: 2016-05-02 18:10:00+01:05
  379. departure_point: home
  380. departure_time: 2016-05-02 18:25:00+01:05
  381. destination_point: city
  382. distance: !distance '3.21 km'
  383. driver: driver
  384. name: taxi ride
  385. price_brutto: !sum '1.23 EUR'
  386. route_map: null
  387. """)
  388. def test_order_registry_from_yaml():
  389. expected = get_order_registry()
  390. loaded = yaml.load(u"""!order-registry
  391. platformπ:
  392. id: !order
  393. customer_id: customer
  394. discounts: []
  395. order_date: 2016-05-08
  396. order_id: id
  397. platform: platformπ
  398. order_b: !order
  399. customer_id: null
  400. items: []
  401. order_date: 2015-05-08
  402. order_id: order_b
  403. platform: platformπ
  404. γάμμα:
  405. order_βήτα: !order
  406. customer_id: ρώ
  407. discounts: []
  408. items: []
  409. order_date: 2014-05-08
  410. order_id: order_βήτα
  411. platform: γάμμα
  412. """)
  413. assert expected == loaded, yaml_diff(expected, loaded)