test_yaml.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import datetime
  4. import difflib
  5. import finoex
  6. import ioex.calcex
  7. import ioex.datetimeex
  8. import os
  9. import pytz
  10. yaml = pytest.importorskip('yaml')
  11. def get_figure_a():
  12. return ioex.calcex.Figure(12.3, u'km')
  13. def get_figure_b():
  14. return ioex.calcex.Figure(12300, u'米')
  15. def get_sum_a():
  16. return finoex.Sum(1.23, u'EUR')
  17. def get_sum_b():
  18. return finoex.Sum(20.45, u'€')
  19. def get_item_a(sub_items=False, set_url=False):
  20. item = finoex.Item(
  21. name = u'item a',
  22. price_brutto = get_sum_a(),
  23. url='http://shop.at/item-a' if set_url else None,
  24. )
  25. if sub_items:
  26. item.sub_items.append(get_item_b())
  27. return item
  28. def get_item_b():
  29. return finoex.Item(
  30. name = u'item β',
  31. price_brutto = get_sum_b(),
  32. )
  33. def get_service_a():
  34. return finoex.Service(
  35. duration = ioex.datetimeex.Duration(years = 2),
  36. ip_addresses = ['::1'],
  37. location = 'vienna',
  38. name = u'service',
  39. price_brutto = finoex.Sum(1.0, u'EUR'),
  40. state = 'success',
  41. )
  42. def get_hosting_service():
  43. return finoex.HostingService(
  44. duration = ioex.datetimeex.Duration(years = 2),
  45. ip_addresses = ['127.0.0.1', '::1'],
  46. location = 'vienna',
  47. name = u'virtual server',
  48. operating_system = 'debian 8',
  49. price_brutto = finoex.Sum(1.0, u'EUR'),
  50. state = 'running',
  51. )
  52. def get_cloud_mining():
  53. return finoex.CloudMining(
  54. hashrate = ioex.calcex.Figure(8, u'TH/s'),
  55. name = u'sha256 BTC',
  56. price_brutto = finoex.Sum(1.0, u'EUR'),
  57. )
  58. def get_service_b():
  59. return finoex.Service(
  60. name = u'service',
  61. price_brutto = finoex.Sum(1.0, u'EUR'),
  62. period = ioex.datetimeex.Period(
  63. start = datetime.datetime(2017, 4, 2, 10, 23),
  64. end = None,
  65. ),
  66. )
  67. def get_person_a():
  68. return finoex.Person(
  69. first_name = u'Fabian Peter',
  70. last_name = u'Hammerle',
  71. )
  72. def get_person_b():
  73. return finoex.Person(
  74. first_name = u'名字',
  75. last_name = u'贵姓',
  76. )
  77. def get_campaign_a():
  78. return finoex.Campaign(
  79. name = u'campaign a',
  80. founder = u'company',
  81. end = datetime.datetime(2016, 7, 23, 9, 23, 17, tzinfo = pytz.timezone('Europe/Vienna')),
  82. )
  83. def get_campaign_b():
  84. return finoex.Campaign(
  85. founder = u'company',
  86. name = u'campaign without end',
  87. website_url = u'http://campaign.com',
  88. )
  89. def get_pledge():
  90. return finoex.Pledge(
  91. campaign = get_campaign_a(),
  92. price_brutto = finoex.Sum(10.0, u'EUR'),
  93. reward = u'great',
  94. )
  95. def get_contribution():
  96. return finoex.Contribution(
  97. campaign = get_campaign_a(),
  98. price_brutto = finoex.Sum(10.0, u'EUR'),
  99. reward = u'product',
  100. )
  101. def get_article():
  102. return finoex.Article(
  103. access_option = 'download',
  104. authors = ['a', 'b'],
  105. depth = finoex.Distance(12.3, u'dm'),
  106. features = u'supergeil',
  107. height = finoex.Distance(123., u'cm'),
  108. maximum_load = ioex.calcex.Figure(40., u'kg'),
  109. name = u'article name',
  110. price_brutto = get_sum_a(),
  111. product_id = u'0815',
  112. quantity = 1,
  113. release_date = datetime.date(2017, 10, 24),
  114. reseller = u'seller',
  115. shipper = u'shipper',
  116. state = u'goood',
  117. width = finoex.Distance(1.23, u'm'),
  118. )
  119. def get_transportation(period=False):
  120. arrival_time = pytz.timezone('Europe/Vienna').localize(
  121. datetime.datetime(2016, 7, 24, 23, 47, 35)
  122. )
  123. if period:
  124. arrival_time = ioex.datetimeex.Period(
  125. start=arrival_time,
  126. end=arrival_time + datetime.timedelta(days=2),
  127. )
  128. return finoex.Transportation(
  129. name = u'ticket',
  130. price_brutto = get_sum_a(),
  131. departure_point = u'home',
  132. destination_point = u'city',
  133. distance = finoex.Distance(3.21, u'km'),
  134. passenger = get_person_a(),
  135. valid_from = datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')),
  136. valid_until = datetime.datetime(2016, 7, 14, 18, 50, 4, 0, tzinfo = pytz.utc),
  137. ticket_url = u'https://www.example.com',
  138. estimated_arrival_time = ioex.datetimeex.Period(
  139. start = pytz.timezone('Europe/Vienna').localize(datetime.datetime(2016, 7, 24, 23, 47, 2)),
  140. end = pytz.utc.localize(datetime.datetime(2016, 7, 24, 23, 48, 5)),
  141. ),
  142. arrival_time=arrival_time,
  143. )
  144. def get_shipping():
  145. return finoex.Shipping(
  146. price_brutto = get_sum_a(),
  147. destination_point = u'home',
  148. tracking_number = 'LX123456789CN',
  149. )
  150. def get_taxi_ride():
  151. return finoex.TaxiRide(
  152. name = u'taxi ride',
  153. price_brutto = get_sum_a(),
  154. departure_point = u'home',
  155. destination_point = u'city',
  156. distance = finoex.Distance(3.21, u'km'),
  157. driver = u'driver',
  158. arrival_time = datetime.datetime(2016, 5, 2, 18, 10, tzinfo = pytz.timezone('Europe/Vienna')),
  159. departure_time = datetime.datetime(2016, 5, 2, 18, 25, tzinfo = pytz.timezone('Europe/Vienna')),
  160. )
  161. def get_discount_a():
  162. return finoex.Discount(
  163. name = u'discount a',
  164. amount = get_sum_a(),
  165. )
  166. def get_discount_b():
  167. return finoex.Discount(
  168. name = u'discount β',
  169. amount = get_sum_b(),
  170. )
  171. def get_discount_c():
  172. return finoex.Discount(
  173. amount = get_sum_b(),
  174. code = 'DISCΓ',
  175. name = u'discount c',
  176. )
  177. def get_order_a(items = True, discounts = True):
  178. order = finoex.Order(
  179. customer_id = u'customer',
  180. debitor_address='@home',
  181. debitor_comment='important note',
  182. order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  183. order_id = u'id',
  184. platform = u'platformπ',
  185. platform_view_url = 'http://i.dont.know',
  186. )
  187. if items:
  188. order.items.append(get_item_a())
  189. order.items.append(get_item_b())
  190. if discounts:
  191. order.discounts.append(get_discount_a())
  192. order.discounts.append(get_discount_b())
  193. return order
  194. def get_order_b():
  195. order = finoex.Order(
  196. platform = u'platformπ',
  197. order_id = u'order_b',
  198. order_date = datetime.datetime(2015, 5, 8, 0, 18, 17),
  199. )
  200. return order
  201. def get_order_c():
  202. order = finoex.Order(
  203. platform = u'γάμμα',
  204. order_id = u'order_βήτα',
  205. order_date = datetime.datetime(2014, 5, 8, 0, 18, 17),
  206. customer_id = u'ρώ',
  207. )
  208. return order
  209. def get_invoice(items = True, discounts = True):
  210. inv = finoex.Invoice(
  211. creditor = u'platformπ',
  212. debitor_id = u'customer',
  213. debitor_address='@home',
  214. debitor_comment='important note',
  215. invoice_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
  216. invoice_id = u'id',
  217. invoice_url = 'http://example.com/invoice.pdf',
  218. )
  219. if items:
  220. inv.items.append(get_item_a())
  221. inv.items.append(get_item_b())
  222. if discounts:
  223. inv.discounts.append(get_discount_a())
  224. return inv
  225. def get_distance():
  226. return finoex.Distance(2.4142, u'km')
  227. def to_yaml(data):
  228. class Dumper(yaml.Dumper):
  229. pass
  230. ioex.datetimeex.Duration.register_yaml_representer(yaml.Dumper)
  231. ioex.datetimeex.Period.register_yaml_representer(yaml.Dumper)
  232. ioex.calcex.Figure.register_yaml_representer(yaml.Dumper)
  233. finoex.Sum.register_yaml_representer(yaml.Dumper)
  234. finoex.Distance.register_yaml_representer(yaml.Dumper)
  235. return yaml.dump(
  236. data,
  237. default_flow_style = False,
  238. allow_unicode = True,
  239. Dumper = Dumper,
  240. ) #.decode('utf-8')
  241. def yaml_diff(a, b):
  242. return '\n'.join(difflib.ndiff(
  243. to_yaml(a).split('\n'),
  244. to_yaml(b).split('\n'),
  245. ))
  246. @pytest.mark.parametrize('source_object,expected_yaml', [
  247. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04\n...\n'],
  248. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')), '2016-07-14 13:50:04+01:05\n...\n'],
  249. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.utc), '2016-07-14 13:50:04+00:00\n...\n'],
  250. [finoex.Distance(1.34, u'km'), u"!distance '1.34 km'\n"],
  251. [finoex.Distance(1.34, u'μm'), u"!distance '1.34 μm'\n"],
  252. [get_discount_a(), u"!discount\namount: !sum '1.23 EUR'\nname: discount a\n"],
  253. [get_discount_b(), u"!discount\namount: !sum '20.45 EUR'\nname: discount β\n"],
  254. [get_discount_c(), u"""!discount
  255. amount: !sum '20.45 EUR'
  256. code: DISCΓ
  257. name: discount c
  258. """],
  259. [get_figure_a(), u"!figure '12.3 km'\n"],
  260. [get_figure_b(), u"!figure '12300 米'\n"],
  261. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum '1.23 EUR'\n"],
  262. [get_item_a(sub_items=True, set_url=True), u"""!item
  263. name: item a
  264. price_brutto: !sum '1.23 EUR'
  265. sub_items:
  266. - !item
  267. name: item β
  268. price_brutto: !sum '20.45 EUR'
  269. url: http://shop.at/item-a
  270. """],
  271. [get_item_b(), u"!item\nname: item β\nprice_brutto: !sum '20.45 EUR'\n"],
  272. [get_person_a(), u'!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  273. [get_person_b(), u'!person\nfirst_name: 名字\nlast_name: 贵姓\n'],
  274. [get_campaign_a(), u"""!campaign
  275. end: 2016-07-23 09:23:17+01:05
  276. founder: company
  277. name: campaign a
  278. """],
  279. [get_campaign_b(), u"""!campaign
  280. founder: company
  281. name: campaign without end
  282. website_url: http://campaign.com
  283. """],
  284. [get_service_a(), u"""!service
  285. duration: !duration
  286. years: 2
  287. ip_addresses:
  288. - ::1
  289. location: vienna
  290. name: service
  291. price_brutto: !sum '1.0 EUR'
  292. state: success
  293. """],
  294. [get_service_b(), u"""!service
  295. name: service
  296. period: !period
  297. end: null
  298. start: 2017-04-02 10:23:00
  299. price_brutto: !sum '1.0 EUR'
  300. """],
  301. [get_hosting_service(), u"""!hosting-service
  302. duration: !duration
  303. years: 2
  304. ip_addresses:
  305. - 127.0.0.1
  306. - ::1
  307. location: vienna
  308. name: virtual server
  309. operating_system: debian 8
  310. price_brutto: !sum '1.0 EUR'
  311. state: running
  312. """],
  313. [get_cloud_mining(), u"""!cloud-mining
  314. hashrate: !figure '8 TH/s'
  315. name: sha256 BTC
  316. price_brutto: !sum '1.0 EUR'
  317. """],
  318. [get_pledge(), u"""!pledge
  319. campaign: !campaign
  320. end: 2016-07-23 09:23:17+01:05
  321. founder: company
  322. name: campaign a
  323. price_brutto: !sum '10.0 EUR'
  324. reward: great
  325. """],
  326. [get_contribution(), u"""!contribution
  327. campaign: !campaign
  328. end: 2016-07-23 09:23:17+01:05
  329. founder: company
  330. name: campaign a
  331. price_brutto: !sum '10.0 EUR'
  332. reward: product
  333. """],
  334. [get_order_a(), u"""!order
  335. customer_id: customer
  336. debitor_address: '@home'
  337. debitor_comment: important note
  338. discounts:
  339. - !discount
  340. amount: !sum '1.23 EUR'
  341. name: discount a
  342. - !discount
  343. amount: !sum '20.45 EUR'
  344. name: discount β
  345. items:
  346. - !item
  347. name: item a
  348. price_brutto: !sum '1.23 EUR'
  349. - !item
  350. name: item β
  351. price_brutto: !sum '20.45 EUR'
  352. order_date: 2016-05-08 00:18:17
  353. order_id: id
  354. platform: platformπ
  355. platform_view_url: http://i.dont.know
  356. """],
  357. [get_order_a(discounts = False), u"""!order
  358. customer_id: customer
  359. debitor_address: '@home'
  360. debitor_comment: important note
  361. items:
  362. - !item
  363. name: item a
  364. price_brutto: !sum '1.23 EUR'
  365. - !item
  366. name: item β
  367. price_brutto: !sum '20.45 EUR'
  368. order_date: 2016-05-08 00:18:17
  369. order_id: id
  370. platform: platformπ
  371. platform_view_url: http://i.dont.know
  372. """],
  373. [get_article(), u"""!article
  374. access_option: download
  375. authors:
  376. - a
  377. - b
  378. depth: !distance '12.3 dm'
  379. features: supergeil
  380. height: !distance '123.0 cm'
  381. maximum_load: !figure '40.0 kg'
  382. name: article name
  383. price_brutto: !sum '1.23 EUR'
  384. product_id: 0815
  385. quantity: 1
  386. release_date: 2017-10-24
  387. reseller: seller
  388. shipper: shipper
  389. state: goood
  390. width: !distance '1.23 m'
  391. """],
  392. [get_transportation(period=False), u"""!transportation
  393. arrival_time: 2016-07-24 23:47:35+02:00
  394. departure_point: home
  395. destination_point: city
  396. distance: !distance '3.21 km'
  397. estimated_arrival_time: !period
  398. end: 2016-07-24 23:48:05+00:00
  399. start: 2016-07-24 23:47:02+02:00
  400. name: ticket
  401. passenger: !person
  402. first_name: Fabian Peter
  403. last_name: Hammerle
  404. price_brutto: !sum '1.23 EUR'
  405. ticket_url: https://www.example.com
  406. valid_from: 2016-07-14 13:50:04+01:05
  407. valid_until: 2016-07-14 18:50:04+00:00
  408. """],
  409. [get_transportation(period=True), u"""!transportation
  410. arrival_time: !period
  411. end: 2016-07-26 23:47:35+02:00
  412. start: 2016-07-24 23:47:35+02:00
  413. departure_point: home
  414. destination_point: city
  415. distance: !distance '3.21 km'
  416. estimated_arrival_time: !period
  417. end: 2016-07-24 23:48:05+00:00
  418. start: 2016-07-24 23:47:02+02:00
  419. name: ticket
  420. passenger: !person
  421. first_name: Fabian Peter
  422. last_name: Hammerle
  423. price_brutto: !sum '1.23 EUR'
  424. ticket_url: https://www.example.com
  425. valid_from: 2016-07-14 13:50:04+01:05
  426. valid_until: 2016-07-14 18:50:04+00:00
  427. """],
  428. [get_shipping(), u"""!shipping
  429. destination_point: home
  430. price_brutto: !sum '1.23 EUR'
  431. tracking_number: LX123456789CN
  432. """],
  433. [get_taxi_ride(), u"""!taxi-ride
  434. arrival_time: 2016-05-02 18:10:00+01:05
  435. departure_point: home
  436. departure_time: 2016-05-02 18:25:00+01:05
  437. destination_point: city
  438. distance: !distance '3.21 km'
  439. driver: driver
  440. name: taxi ride
  441. price_brutto: !sum '1.23 EUR'
  442. """],
  443. [get_invoice(), u"""!invoice
  444. creditor: platformπ
  445. debitor_address: '@home'
  446. debitor_comment: important note
  447. debitor_id: customer
  448. discounts:
  449. - !discount
  450. amount: !sum '1.23 EUR'
  451. name: discount a
  452. invoice_date: 2016-05-08 00:18:17
  453. invoice_id: id
  454. invoice_url: http://example.com/invoice.pdf
  455. items:
  456. - !item
  457. name: item a
  458. price_brutto: !sum '1.23 EUR'
  459. - !item
  460. name: item β
  461. price_brutto: !sum '20.45 EUR'
  462. """],
  463. ])
  464. def test_to_yaml(source_object, expected_yaml):
  465. assert expected_yaml == to_yaml(source_object)
  466. @pytest.mark.parametrize('expected_object,source_yaml', [
  467. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0), '2016-07-14 13:50:04'],
  468. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')), '2016-07-14 13:50:04+01:05'],
  469. [datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.utc), '2016-07-14 13:50:04+00:00'],
  470. [finoex.Distance(1.34, u'km'), u"!distance '1.34 km'\n"],
  471. [finoex.Distance(1.34, u'μm'), u"!distance '1.34 μm'"],
  472. [get_discount_a(), u"!discount\nname: discount a\namount: !sum '1.23 EUR'\n"],
  473. [get_discount_b(), u"!discount\nname: discount β\namount: !sum '20.45 EUR'\n"],
  474. [get_discount_c(), u"""!discount
  475. amount: !sum 20.45 EUR
  476. name: discount c
  477. code: DISCΓ
  478. """],
  479. [get_figure_a(), '!figure\nunit: km\nvalue: 12.3\n'],
  480. [get_figure_a(), u'!figure\nunit: km\nvalue: 12.3\n'],
  481. [get_figure_b(), '!figure\nunit: 米\nvalue: 12300\n'],
  482. [get_figure_b(), u'!figure\nunit: 米\nvalue: 12300\n'],
  483. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum '1.23 EUR'\n"],
  484. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum 1.23 EUR"],
  485. [get_item_a(sub_items=True, set_url=True), u"""!item
  486. name: item a
  487. price_brutto: !sum '1.23 EUR'
  488. sub_items:
  489. - !item
  490. name: item β
  491. price_brutto: !sum '20.45 EUR'
  492. url: http://shop.at/item-a
  493. """],
  494. [get_item_a(), u"!item\nname: item a\nprice_brutto: !sum 1.23 €\n"],
  495. [get_item_b(), u"!item\nname: item β\nprice_brutto: !sum '20.45 EUR'\n"],
  496. [get_person_a(), '!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  497. [get_person_a(), u'!person\nfirst_name: Fabian Peter\nlast_name: Hammerle\n'],
  498. [get_person_b(), u'!person\nfirst_name: 名字\nlast_name: 贵姓\n'],
  499. [get_campaign_a(), u"""!campaign
  500. name: campaign a
  501. founder: company
  502. end: 2016-07-23 09:23:17+01:05
  503. """],
  504. [get_campaign_b(), u"""!campaign
  505. founder: company
  506. name: campaign without end
  507. website_url: http://campaign.com
  508. """],
  509. [get_service_a(), u"""!service
  510. duration: !duration
  511. years: 2
  512. ip_addresses:
  513. - ::1
  514. location: vienna
  515. name: service
  516. price_brutto: !sum '1.0 EUR'
  517. state: success
  518. """],
  519. [get_service_b(), u"""!service
  520. name: service
  521. period: !period
  522. start: 2017-04-02 10:23:00
  523. price_brutto: !sum '1.0 EUR'
  524. """],
  525. [get_hosting_service(), u"""!hosting-service
  526. duration: !duration
  527. years: 2
  528. ip_addresses:
  529. - 127.0.0.1
  530. - ::1
  531. location: vienna
  532. name: virtual server
  533. operating_system: debian 8
  534. price_brutto: !sum '1.0 EUR'
  535. state: running
  536. """],
  537. [get_cloud_mining(), u"""!cloud-mining
  538. hashrate: !figure 8 TH/s
  539. name: sha256 BTC
  540. price_brutto: !sum 1.0 EUR
  541. """],
  542. [get_pledge(), u"""!pledge
  543. campaign: !campaign
  544. name: campaign a
  545. founder: company
  546. end: 2016-07-23 09:23:17+01:05
  547. price_brutto: !sum '10.0 EUR'
  548. reward: great
  549. """],
  550. [get_contribution(), u"""!contribution
  551. campaign: !campaign
  552. end: 2016-07-23 09:23:17+01:05
  553. founder: company
  554. name: campaign a
  555. price_brutto: !sum '10.0 EUR'
  556. reward: product
  557. """],
  558. [[get_person_a(), get_person_b()], u"""
  559. - !person
  560. first_name: Fabian Peter
  561. last_name: Hammerle
  562. - !person
  563. first_name: 名字
  564. last_name: 贵姓"""],
  565. [get_transportation(period=False), u"""!transportation
  566. arrival_time: 2016-07-24 23:47:35+02:00
  567. departure_point: home
  568. destination_point: city
  569. distance: !distance '3.21 km'
  570. name: ticket
  571. price_brutto: !sum '1.23 EUR'
  572. route_map: null
  573. passenger: !person
  574. first_name: Fabian Peter
  575. last_name: Hammerle
  576. ticket_url: https://www.example.com
  577. valid_from: 2016-07-14 13:50:04+01:05
  578. valid_until: 2016-07-14 18:50:04+00:00
  579. estimated_arrival_time: !period
  580. end: 2016-07-24T23:48:05+00:00
  581. start: 2016-07-24T23:47:02+02:00
  582. """],
  583. [get_transportation(period=True), u"""!transportation
  584. arrival_time: !period
  585. end: 2016-07-26 23:47:35+02:00
  586. start: 2016-07-24 23:47:35+02:00
  587. departure_point: home
  588. destination_point: city
  589. distance: !distance '3.21 km'
  590. estimated_arrival_time: !period
  591. end: 2016-07-24 23:48:05+00:00
  592. start: 2016-07-24 23:47:02+02:00
  593. name: ticket
  594. passenger: !person
  595. first_name: Fabian Peter
  596. last_name: Hammerle
  597. price_brutto: !sum '1.23 EUR'
  598. ticket_url: https://www.example.com
  599. valid_from: 2016-07-14 13:50:04+01:05
  600. valid_until: 2016-07-14 18:50:04+00:00
  601. """],
  602. [get_shipping(), u"""!shipping
  603. destination_point: home
  604. price_brutto: !sum 1.23 EUR
  605. tracking_number: LX123456789CN
  606. """],
  607. [get_order_a(), u"""!order
  608. customer_id: customer
  609. debitor_address: '@home'
  610. debitor_comment: important note
  611. discounts:
  612. - !discount
  613. amount: !sum '1.23 EUR'
  614. name: discount a
  615. - !discount
  616. amount: !sum '20.45 EUR'
  617. name: discount β
  618. items:
  619. - !item
  620. name: item a
  621. price_brutto: !sum '1.23 EUR'
  622. - !item
  623. name: item β
  624. price_brutto: !sum '20.45 EUR'
  625. order_date: 2016-05-08 00:18:17
  626. order_id: id
  627. platform: platformπ
  628. platform_view_url: http://i.dont.know
  629. """],
  630. [get_article(), u"""!article
  631. access_option: download
  632. authors:
  633. - a
  634. - b
  635. delivery_date: null
  636. features: supergeil
  637. name: article name
  638. price_brutto: !sum '1.23 EUR'
  639. product_id: 0815
  640. quantity: 1
  641. release_date: 2017-10-24
  642. reseller: seller
  643. shipper: shipper
  644. state: goood
  645. depth: !distance 12.3 dm
  646. height: !distance 123.0 cm
  647. maximum_load: !figure 40.0 kg
  648. width: !distance 1.23 m
  649. """],
  650. [get_taxi_ride(), u"""!taxi-ride
  651. arrival_time: 2016-05-02 18:10:00+01:05
  652. departure_point: home
  653. departure_time: 2016-05-02 18:25:00+01:05
  654. destination_point: city
  655. distance: !distance '3.21 km'
  656. driver: driver
  657. name: taxi ride
  658. price_brutto: !sum '1.23 EUR'
  659. route_map: null
  660. """],
  661. [get_invoice(), u"""!invoice
  662. creditor: platformπ
  663. debitor_address: '@home'
  664. debitor_comment: important note
  665. debitor_id: customer
  666. invoice_date: 2016-05-08T00:18:17
  667. invoice_id: id
  668. invoice_url: http://example.com/invoice.pdf
  669. items:
  670. - !item
  671. name: item a
  672. price_brutto: !sum '1.23 EUR'
  673. - !item
  674. name: item β
  675. price_brutto: !sum '20.45 EUR'
  676. discounts:
  677. - !discount
  678. name: discount a
  679. amount: !sum 1.23 EUR
  680. """],
  681. ])
  682. def test_from_yaml(expected_object, source_yaml):
  683. class Loader(yaml.Loader):
  684. pass
  685. ioex.datetimeex.Duration.register_yaml_constructor(Loader)
  686. ioex.datetimeex.Period.register_yaml_constructor(Loader)
  687. ioex.calcex.Figure.register_yaml_constructor(Loader)
  688. finoex.Sum.register_yaml_constructor(Loader)
  689. finoex.Distance.register_yaml_constructor(Loader)
  690. loaded_object = yaml.load(source_yaml, Loader=Loader)
  691. assert loaded_object == expected_object