1
0

test_yaml.py 15 KB

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