123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- # -*- coding: utf-8 -*-
- import pytest
- import os
- import yaml
- import dingguo
- import difflib
- import datetime
- def get_figure_a():
- return dingguo.Figure(12.3, u'km')
- def get_figure_b():
- return dingguo.Figure(12300, u'米')
- def get_sum_a():
- return dingguo.Sum(1.23, u'EUR')
- def get_sum_b():
- return dingguo.Sum(20.45, u'€')
- def get_item_a():
- return dingguo.Item(
- name = u'item a',
- price_brutto = get_sum_a(),
- )
- def get_item_b():
- return dingguo.Item(
- name = u'item β',
- price_brutto = get_sum_b(),
- )
- def get_article():
- return dingguo.Article(
- authors = ['a', 'b'],
- name = u'article name',
- price_brutto = get_sum_a(),
- quantity = 1,
- reseller = u'seller',
- shipper = u'shipper',
- state = u'goood',
- )
- def get_discount_a():
- return dingguo.Discount(
- name = u'discount a',
- amount = get_sum_a(),
- )
- def get_discount_b():
- return dingguo.Discount(
- name = u'discount β',
- amount = get_sum_b(),
- )
- def get_order_a():
- order = dingguo.Order(
- platform = u'platformπ',
- order_id = u'id',
- order_date = datetime.datetime(2016, 5, 8, 0, 18, 17),
- customer_id = u'customer',
- )
- order.items.append(get_item_a())
- order.items.append(get_item_b())
- order.discounts.append(get_discount_a())
- order.discounts.append(get_discount_b())
- return order
- def get_distance():
- return dingguo.Distance(2.4142, u'km')
- def to_yaml(data):
- return yaml.dump(data, default_flow_style = False, allow_unicode = True).decode('utf-8')
- def yaml_diff(a, b):
- return '\n'.join(difflib.ndiff(
- to_yaml(a).split('\n'),
- to_yaml(b).split('\n'),
- ))
- def test_figure_to_yaml():
- assert to_yaml(get_figure_a()) == u"""!figure
- unit: km
- value: 12.3
- """
- def test_figure_to_yaml_unicode():
- assert to_yaml(get_figure_b()) == u"""!figure
- unit: 米
- value: 12300
- """
- def test_figure_from_yaml():
- assert get_figure_a() == yaml.load(u"""!figure
- unit: km
- value: 12.3
- """)
- def test_figure_to_yaml_unicode():
- assert get_figure_b() == yaml.load(u"""!figure
- unit: 米
- value: 12300
- """)
- def test_sum_to_yaml_a():
- assert to_yaml(get_sum_a()) == u"!sum '1.23 EUR'\n"
- def test_sum_to_yaml_b():
- assert to_yaml(get_sum_b()) == u"!sum '20.45 EUR'\n"
- def test_sum_from_yaml_a():
- assert get_sum_a() == yaml.load(u"!sum 1.23 EUR")
- def test_sum_from_yaml_a_sign():
- assert get_sum_a() == yaml.load(u"!sum 1.23 €")
- def test_sum_from_yaml_a_quotes():
- assert get_sum_a() == yaml.load(u"!sum '1.23 EUR'")
- def test_item_to_yaml_a():
- assert to_yaml(get_item_a()) == u"""!item
- name: item a
- price_brutto: !sum '1.23 EUR'
- """
- def test_item_to_yaml_b():
- assert to_yaml(get_item_b()) == u"""!item
- name: item β
- price_brutto: !sum '20.45 EUR'
- """
- def test_item_from_yaml_a():
- assert get_item_a() == yaml.load(u"""!item
- name: item a
- price_brutto: !sum '1.23 EUR'
- """)
- def test_item_from_yaml_b():
- assert get_item_b() == yaml.load(u"""!item
- name: item β
- price_brutto: !sum '20.45 EUR'
- """)
- def test_discount_to_yaml_a():
- assert to_yaml(get_discount_a()) == u"""!discount
- amount: !sum '1.23 EUR'
- name: discount a
- """
- def test_discount_to_yaml_b():
- assert to_yaml(get_discount_b()) == u"""!discount
- amount: !sum '20.45 EUR'
- name: discount β
- """
- def test_discount_from_yaml_a():
- assert get_discount_a() == yaml.load(u"""!discount
- name: discount a
- amount: !sum '1.23 EUR'
- """)
- def test_discount_from_yaml_b():
- assert get_discount_b() == yaml.load(u"""!discount
- name: discount β
- amount: !sum '20.45 EUR'
- """)
- def test_order_to_yaml():
- assert to_yaml(get_order_a()) == u"""!order
- customer_id: customer
- discounts:
- - !discount
- amount: !sum '1.23 EUR'
- name: discount a
- - !discount
- amount: !sum '20.45 EUR'
- name: discount β
- items:
- - !item
- name: item a
- price_brutto: !sum '1.23 EUR'
- - !item
- name: item β
- price_brutto: !sum '20.45 EUR'
- order_date: 2016-05-08
- order_id: id
- platform: platformπ
- """
- def test_order_from_yaml():
- order_loaded = yaml.load(u"""!order
- customer_id: customer
- discounts:
- - !discount
- amount: !sum '1.23 EUR'
- name: discount a
- - !discount
- amount: !sum '20.45 EUR'
- name: discount β
- items:
- - !item
- name: item a
- price_brutto: !sum '1.23 EUR'
- - !item
- name: item β
- price_brutto: !sum '20.45 EUR'
- order_date: 2016-05-08
- order_id: id
- platform: platformπ
- """)
- order_expected = get_order_a()
- assert order_expected == order_loaded, yaml_diff(order_expected, order_loaded)
- def test_scalar_figure_to_yaml():
- assert to_yaml(dingguo.ScalarFigure(1.34, u'μm')) == u"!scalar '1.34 μm'\n"
- def test_scalar_figure_from_yaml():
- assert dingguo.ScalarFigure(1.34, u'μm') == yaml.load(u"!scalar '1.34 μm'")
- def test_distance_to_yaml():
- assert to_yaml(dingguo.Distance(1.34, u'km')) == u"!distance '1.34 km'\n"
- def test_distance_from_yaml():
- assert dingguo.Distance(1.34, u'km') == yaml.load(u"!distance '1.34 km'\n")
- def test_article_to_yaml_a():
- assert to_yaml(get_article()) == u"""!article
- authors:
- - a
- - b
- delivery_date: null
- name: article name
- price_brutto: !sum '1.23 EUR'
- quantity: 1
- reseller: seller
- shipper: shipper
- state: goood
- """
- def test_article_to_yaml_a():
- assert get_article() == yaml.load(u"""!article
- authors:
- - a
- - b
- delivery_date: null
- name: article name
- price_brutto: !sum '1.23 EUR'
- quantity: 1
- reseller: seller
- shipper: shipper
- state: goood
- """)
|