1
0

test_yaml.py 14 KB

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