Browse Source

yaml support for Discount

Fabian Peter Hammerle 8 years ago
parent
commit
1efe575b19
2 changed files with 54 additions and 2 deletions
  1. 2 0
      dingguo/__init__.py
  2. 52 2
      tests/test_yaml.py

+ 2 - 0
dingguo/__init__.py

@@ -119,6 +119,8 @@ class Sum(Figure):
 
 class Discount(yaml.YAMLObject):
 
+    yaml_tag = u'!discount'
+
     def __init__(
             self,
             name = None,

+ 52 - 2
tests/test_yaml.py

@@ -32,6 +32,18 @@ def get_item_b():
             price_brutto = get_sum_b(),
             )
 
+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π',
@@ -41,6 +53,8 @@ def get_order_a():
             )
     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 to_yaml(data):
@@ -115,10 +129,40 @@ name: item β
 price_brutto: !sum 'EUR 20.45'
 """)
 
+def test_discount_to_yaml_a():
+    assert to_yaml(get_discount_a()) == u"""!discount
+amount: !sum 'EUR 1.23'
+name: discount a
+"""
+
+def test_discount_to_yaml_b():
+    assert to_yaml(get_discount_b()) == u"""!discount
+amount: !sum 'EUR 20.45'
+name: discount β
+"""
+
+def test_discount_from_yaml_a():
+    assert get_discount_a() == yaml.load(u"""!discount
+name: discount a
+amount: !sum 'EUR 1.23'
+""")
+
+def test_discount_from_yaml_b():
+    assert get_discount_b() == yaml.load(u"""!discount
+name: discount β
+amount: !sum 'EUR 20.45'
+""")
+
 def test_order_to_yaml():
     assert to_yaml(get_order_a()) == u"""!order
 customer_id: customer
-discounts: []
+discounts:
+- !discount
+  amount: !sum 'EUR 1.23'
+  name: discount a
+- !discount
+  amount: !sum 'EUR 20.45'
+  name: discount β
 items:
 - !item
   name: item a
@@ -134,7 +178,13 @@ platform: platformπ
 def test_order_from_yaml():
     order_loaded = yaml.load(u"""!order
 customer_id: customer
-discounts: []
+discounts:
+- !discount
+  amount: !sum 'EUR 1.23'
+  name: discount a
+- !discount
+  amount: !sum 'EUR 20.45'
+  name: discount β
 items:
 - !item
   name: item a