Repository: fossasia/open-event-server

Issue:
Add tickets API to dredd testing
parent issue: #3870 


Discussion:
Hi @magdalenesuo!

Looks like you're missing some information we'll need to replicate this issue.

The easier it is for us to decipher an issue with the info provided,
the more likely it is that we'll be able to help.

Please make sure you have the following information in this issue:

1. The current (incorrect) behavior you're seeing.
2. The behavior you expect.
3. The OS version, browser, app, device you use.
4. A [short, self-contained example](http://sscce.org/).

Resolving patch:
diff --git a/app/api/tickets.py b/app/api/tickets.py
--- a/app/api/tickets.py
+++ b/app/api/tickets.py
@@ -27,14 +27,15 @@ def validate_date(self, data, original_data):
         if 'id' in original_data['data']:
             ticket = Ticket.query.filter_by(id=original_data['data']['id']).one()
 
-            if 'starts_at' not in data:
-                data['starts_at'] = ticket.starts_at
+            if 'sales_starts_at' not in data:
+                data['sales_starts_at'] = ticket.sales_starts_at
 
-            if 'ends_at' not in data:
-                data['ends_at'] = ticket.ends_at
+            if 'sales_ends_at' not in data:
+                data['sales_ends_at'] = ticket.sales_ends_at
 
-        if data['starts_at'] >= data['ends_at']:
-            raise UnprocessableEntity({'pointer': '/data/attributes/ends-at'}, "ends-at should be after starts-at")
+        if data['sales_starts_at'] >= data['sales_ends_at']:
+            raise UnprocessableEntity({'pointer': '/data/attributes/sales-ends-at'},
+                                      "sales-ends-at should be after sales-starts-at")
 
     @validates_schema
     def validate_order_quantity(self, data):
diff --git a/app/factories/ticket.py b/app/factories/ticket.py
new file mode 100644
--- /dev/null
+++ b/app/factories/ticket.py
@@ -0,0 +1,25 @@
+import factory
+from app.models.ticket import db, Ticket
+from app.factories.event import EventFactoryBasic
+import app.factories.common as common
+
+
+class TicketFactory(factory.alchemy.SQLAlchemyModelFactory):
+    class Meta:
+        model = Ticket
+        sqlalchemy_session = db.session
+
+    event = factory.RelatedFactory(EventFactoryBasic)
+    name = common.string_
+    description = common.string_
+    type = common.string_
+    price = common.float_
+    quantity = 10
+    is_description_visible = True
+    position = 10
+    is_fee_absorbed = True
+    sales_starts_at = common.date_
+    sales_ends_at = common.dateEnd_
+    is_hidden = True
+    min_order = 0
+    max_order = 10


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -29,6 +29,7 @@
 from app.factories.sponsor import SponsorFactory
 from app.factories.speakers_call import SpeakersCallFactory
 from app.factories.tax import TaxFactory
+from app.factories.ticket import TicketFactory
 
 stash = {}
 api_username = "open_event_test_user@fossasia.org"
@@ -772,7 +773,10 @@ def ticket_get_list(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        ticket = TicketFactory()
+        db.session.add(ticket)
+        db.session.commit()
 
 
 @hooks.before("Tickets > Tickets Collection > Create Ticket")
@@ -782,7 +786,11 @@ def ticket_post(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        event = EventFactoryBasic()
+        db.session.add(event)
+        db.session.commit()
+
 
 
 @hooks.before("Tickets > Ticket Details > Ticket Details")
@@ -792,7 +800,11 @@ def ticket_get_detail(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        ticket = TicketFactory()
+        db.session.add(ticket)
+        db.session.commit()
+
 
 
 @hooks.before("Tickets > Ticket Details > Update Ticket")
@@ -802,7 +814,11 @@ def ticket_patch(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        ticket = TicketFactory()
+        db.session.add(ticket)
+        db.session.commit()
+
 
 
 @hooks.before("Tickets > Ticket Details > Delete Ticket")
@@ -812,7 +828,11 @@ def ticket_delete(transaction):
     :param transaction:
     :return:
     """
-    transaction['skip'] = True
+    with stash['app'].app_context():
+        ticket = TicketFactory()
+        db.session.add(ticket)
+        db.session.commit()
+
 
 
 # ------------------------- Tracks -------------------------