Browse Source

implemented Order.from_dict()

Fabian Peter Hammerle 8 years ago
parent
commit
38c58cfcec
2 changed files with 41 additions and 2 deletions
  1. 16 0
      dingguo/__init__.py
  2. 25 2
      tests/test_.py

+ 16 - 0
dingguo/__init__.py

@@ -121,6 +121,22 @@ class Order(object):
             'platform': self.platform,
             }.items() if v is not None}
 
+    @staticmethod
+    def from_dict(attr):
+        order = Order(
+                platform = attr['platform'],
+                order_id = attr['order_id'],
+                order_date = datetime.datetime.strptime(attr['order_date'], '%Y-%m-%d'),
+                )
+
+        if 'customer_id' in attr:
+            order.customer_id = attr['customer_id']
+
+        order.items = attr['articles']
+        order.discounts = attr['discounts']
+
+        return order
+
 yaml.SafeDumper.add_representer(Order, lambda dumper, order: dumper.represent_dict(order.dict_repr()))
 
 class Item(object):

+ 25 - 2
tests/test_.py

@@ -11,7 +11,7 @@ project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
 test_data_path = os.path.join(project_root_path, 'tests', 'data')
 
 def test_order_dict_repr():
-    
+
     order = dingguo.Order(
             platform = u'platform',
             order_id = u'id',
@@ -28,7 +28,7 @@ def test_order_dict_repr():
     order.discounts.append(discount_0)
     discount_1 = dingguo.Discount(name = u'discount 1', amount = dingguo.Sum(4.0, u'EUR'))
     order.discounts.append(discount_1)
-    
+
     assert order.dict_repr() == {
             'articles': [item_0, item_1],
             'customer_id': u'customer',
@@ -37,3 +37,26 @@ def test_order_dict_repr():
             'order_id': u'id',
             'platform': u'platform',
             }
+
+def test_order_from_dict():
+
+    item_0 = dingguo.Item(name = u'item 0', price_brutto = dingguo.Sum(1.0, u'EUR'))
+    item_1 = dingguo.Item(name = u'item 1', price_brutto = dingguo.Sum(2.0, u'EUR'))
+    discount_0 = dingguo.Discount(name = u'discount 0', amount = dingguo.Sum(3.0, u'EUR'))
+    discount_1 = dingguo.Discount(name = u'discount 1', amount = dingguo.Sum(4.0, u'EUR'))
+
+    order = dingguo.Order.from_dict({
+            'articles': [item_0, item_1],
+            'customer_id': u'customer',
+            'discounts': [discount_0, discount_1],
+            'order_date': u'2016-05-08',
+            'order_id': u'id',
+            'platform': u'platform',
+            })
+
+    assert order.items == [item_0, item_1]
+    assert order.customer_id == u'customer'
+    assert order.discounts == [discount_0, discount_1]
+    assert order.order_date == datetime.date(2016, 5, 8)
+    assert order.order_id == u'id'
+    assert order.platform == u'platform'