Browse Source

added classes Campaign and Pledge

Fabian Peter Hammerle 7 years ago
parent
commit
9d607489e8
3 changed files with 118 additions and 10 deletions
  1. 40 2
      dingguo/__init__.py
  2. 41 8
      tests/test_comparison.py
  3. 37 0
      tests/test_yaml.py

+ 40 - 2
dingguo/__init__.py

@@ -43,6 +43,13 @@ class _YamlInitConstructor(_Object, yaml.YAMLObject):
         #         for (k, v) in loader.construct_mapping(node, deep = True).items()
         #     })
 
+    def __eq__(self, other):
+        return (type(self) == type(other)
+                and vars(self) == vars(other))
+
+    def __ne__(self, other):
+        return not (self == other)
+
 class Figure(_YamlInitConstructor):
 
     yaml_tag = u"!figure"
@@ -210,11 +217,41 @@ class Item(_YamlInitConstructor):
             name = None,
             price_brutto = None,
             ):
-        assert type(name) is unicode
-        self.name = name
+        if not name is None:
+            assert type(name) is unicode
+            self.name = name
         assert type(price_brutto) is Sum
         self.price_brutto = price_brutto
 
+class Campaign(_YamlInitConstructor):
+
+    yaml_tag = u'!campaign'
+
+    def __init__(self,
+            end = None,
+            founder = None,
+            name = None,
+            ):
+        assert type(name) is unicode
+        self.name = name
+        assert type(founder) is unicode
+        self.founder = founder
+        assert type(end) is datetime.datetime
+        assert not end.tzinfo is None, '%r' % end
+        self.end = end
+
+class Pledge(Item):
+
+    yaml_tag = u'!pledge'
+
+    def __init__(self,
+            campaign = None,
+            **kwargs
+            ):
+        super(Pledge, self).__init__(**kwargs)
+        assert type(campaign) is Campaign
+        self.campaign = campaign
+
 class Article(Item):
 
     yaml_tag = u'!article'
@@ -239,6 +276,7 @@ class Article(Item):
             **kwargs
             ):
         super(Article, self).__init__(**kwargs)
+        assert not self.name is None
         assert type(quantity) is int
         self.quantity = quantity
         if authors is not None:

+ 41 - 8
tests/test_comparison.py

@@ -6,6 +6,7 @@ import datetime
 import dingguo
 import os
 import pprint
+import pytz
 import yaml
 
 def get_item_a():
@@ -70,6 +71,32 @@ def get_order_c():
     order.discounts.append(get_discount_b())
     return order
 
+def get_campaign_a():
+    return dingguo.Campaign(
+            name = u'campaign a',
+            founder = u'company',
+            end = datetime.datetime(2016, 7, 23, 9, 23, 17, tzinfo = pytz.timezone('Europe/Vienna')),
+            )
+
+def get_campaign_b():
+    return dingguo.Campaign(
+            name = u'campaign b',
+            founder = u'company',
+            end = datetime.datetime(2016, 7, 23, 9, 23, 17, tzinfo = pytz.timezone('Europe/Vienna')),
+            )
+
+def get_pledge_a():
+    return dingguo.Pledge(
+            campaign = get_campaign_a(),
+            price_brutto = dingguo.Sum(10.0, u'EUR'),
+            )
+
+def get_pledge_b():
+    return dingguo.Pledge(
+            campaign = get_campaign_a(),
+            price_brutto = dingguo.Sum(4.0, u'EUR'),
+            )
+
 def get_person_a():
     return dingguo.Person(
         first_name = u'Fabian Peter',
@@ -84,15 +111,19 @@ def get_person_b():
 
 @pytest.mark.parametrize('a,b', [
     [dingguo.Figure(1, u'mm'), dingguo.Figure(1, u'mm')],
-    [get_item_a(), get_item_a()],
-    [get_item_b(), get_item_b()],
-    [get_person_a(), get_person_a()],
-    [get_person_b(), get_person_b()],
+    [get_campaign_a(), get_campaign_a()],
+    [get_campaign_b(), get_campaign_b()],
     [get_discount_a(), get_discount_a()],
     [get_discount_b(), get_discount_b()],
+    [get_item_a(), get_item_a()],
+    [get_item_b(), get_item_b()],
     [get_order_a(), get_order_a()],
     [get_order_b(), get_order_b()],
     [get_order_c(), get_order_c()],
+    [get_person_a(), get_person_a()],
+    [get_person_b(), get_person_b()],
+    [get_pledge_a(), get_pledge_a()],
+    [get_pledge_b(), get_pledge_b()],
     ])
 def test_eq(a, b):
     assert a == b, '\n'.join([
@@ -107,13 +138,15 @@ def test_eq(a, b):
 
 @pytest.mark.parametrize('a,b', [
     [dingguo.Figure(1, u'mm'), dingguo.Figure(2, u'mm')],
-    [get_item_a(), get_item_b()],
-    [get_person_a(), get_person_b()],
     [get_discount_a(), get_discount_b()],
-    [get_order_a(), get_order_b()],
+    [get_item_a(), get_item_b()],
     [get_order_a(), get_order_a(items = False)],
-    [get_order_b(), get_order_c()],
+    [get_order_a(), get_order_b()],
     [get_order_a(), get_order_c()],
+    [get_order_b(), get_order_c()],
+    [get_person_a(), get_person_b()],
+    [get_pledge_a(), get_order_b()],
+    [get_pledge_a(), get_pledge_b()],
     ])
 def test_neq(a, b):
     assert a != b

+ 37 - 0
tests/test_yaml.py

@@ -45,6 +45,19 @@ def get_person_b():
         last_name = u'贵姓',
         )
 
+def get_campaign():
+    return dingguo.Campaign(
+            name = u'campaign a',
+            founder = u'company',
+            end = datetime.datetime(2016, 7, 23, 9, 23, 17, tzinfo = pytz.timezone('Europe/Vienna')),
+            )
+
+def get_pledge():
+    return dingguo.Pledge(
+            campaign = get_campaign(),
+            price_brutto = dingguo.Sum(10.0, u'EUR'),
+            )
+
 def get_article():
     return dingguo.Article(
             authors = ['a', 'b'],
@@ -166,6 +179,18 @@ def yaml_diff(a, b):
     [get_person_b(), u'!person\nfirst_name: 名字\nlast_name: 贵姓\n'],
     [get_sum_a(), u"!sum '1.23 EUR'\n"],
     [get_sum_b(), u"!sum '20.45 EUR'\n"],
+    [get_campaign(), u"""!campaign
+end: 2016-07-23 09:23:17+01:05
+founder: company
+name: campaign a
+"""],
+    [get_pledge(), u"""!pledge
+campaign: !campaign
+  end: 2016-07-23 09:23:17+01:05
+  founder: company
+  name: campaign a
+price_brutto: !sum '10.0 EUR'
+"""],
     [get_order_a(), u"""!order
 customer_id: customer
 discounts:
@@ -278,6 +303,18 @@ def test_to_yaml(source_object, expected_yaml):
     [get_sum_a(), u"!sum '1.23 EUR'"],
     [get_sum_a(), u"!sum '1.23 EUR'"],
     [get_sum_a(), u"!sum 1.23 EUR"],
+    [get_campaign(), u"""!campaign
+name: campaign a
+founder: company
+end: 2016-07-23 09:23:17+01:05
+"""],
+    [get_pledge(), u"""!pledge
+campaign: !campaign
+  name: campaign a
+  founder: company
+  end: 2016-07-23 09:23:17+01:05
+price_brutto: !sum '10.0 EUR'
+"""],
     [get_sum_a(), u"!sum 1.23 €"],
     [[get_person_a(), get_person_b()], u"""
 - !person