Repository: fossasia/open-event-server

Issue:
Add Discount Codes API to dredd testing
Parent issue #3870.


Discussion:

Resolving patch:
diff --git a/app/factories/discount_code.py b/app/factories/discount_code.py
new file mode 100644
--- /dev/null
+++ b/app/factories/discount_code.py
@@ -0,0 +1,24 @@
+import factory
+from app.models.discount_code import db, DiscountCode
+from app.factories.event import EventFactoryBasic
+import app.factories.common as common
+
+
+class DiscountCodeFactory(factory.alchemy.SQLAlchemyModelFactory):
+    class Meta:
+        model = DiscountCode
+        sqlalchemy_session = db.session
+
+    event = factory.RelatedFactory(EventFactoryBasic)
+    code = common.string_
+    discount_url = common.url_
+    value = common.float_
+    type = "amount"
+    is_active = True
+    tickets_number = 10
+    min_quantity = 10
+    max_quantity = 10
+    valid_from = common.date_
+    valid_till = common.dateEnd_
+    used_for = common.string_
+    tickets = "event"
diff --git a/app/models/discount_code.py b/app/models/discount_code.py
--- a/app/models/discount_code.py
+++ b/app/models/discount_code.py
@@ -41,7 +41,8 @@ def __init__(self,
                  valid_till=None,
                  is_active=True,
                  used_for=None,
-                 event_id=None):
+                 event_id=None,
+                 tickets=None):
         self.code = code
         self.discount_url = discount_url
         self.type = type
@@ -55,6 +56,7 @@ def __init__(self,
         self.is_active = is_active
         self.created_at = datetime.utcnow()
         self.used_for = used_for
+        self.tickets = tickets
 
     @staticmethod
     def get_service_name():


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -1,3 +1,5 @@
+
+
 import sys
 import os.path as path
 import dredd_hooks as hooks
@@ -19,6 +21,7 @@
 from app.factories.social_link import SocialLinkFactory
 from app.factories.image_size import ImageSizeFactory
 from app.factories.page import PageFactory
+from app.factories.discount_code import DiscountCodeFactory
 
 stash = {}
 api_username = "open_event_test_user@fossasia.org"
@@ -1034,7 +1037,10 @@ def discount_code_get_list(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        discount_code = DiscountCodeFactory()
+        db.session.add(discount_code)
+        db.session.commit()
 
 
 @hooks.before("Discount Codes > Discount Code Collection > Create Discount Code")
@@ -1044,7 +1050,10 @@ def discount_code_post(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
 
 
 @hooks.before("Discount Codes > Discount Code Detail > Discount Code Detail")
@@ -1054,7 +1063,10 @@ def discount_code_get_detail(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        discount_code = DiscountCodeFactory()
+        db.session.add(discount_code)
+        db.session.commit()
 
 
 @hooks.before("Discount Codes > Discount Code Detail > Update Discount Code")
@@ -1064,7 +1076,10 @@ def discount_code_patch(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        discount_code = DiscountCodeFactory()
+        db.session.add(discount_code)
+        db.session.commit()
 
 
 @hooks.before("Discount Codes > Discount Code Detail > Delete Discount Code")
@@ -1074,7 +1089,10 @@ def discount_delete(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        discount_code = DiscountCodeFactory()
+        db.session.add(discount_code)
+        db.session.commit()
 
 
 # ------------------------- Upload -------------------------