Skip to content

Commit

Permalink
Donation endpoints (#65)
Browse files Browse the repository at this point in the history
* Added Get and POST endpoints

* Black Format

* Added PUT endpoint

* Added DELETE endpoint

* Fixed variable names

* Black reformat

* Edited routes to consider the amount field. Added tests for each endpoint.

* Black test_donation.py

* Removed get donation by user

* Removed id from POST endpoint. Checked for the case data=={}.

* Checked for the case data=={} in update

Co-authored-by: aqghawa <[email protected]>
  • Loading branch information
ahmad675 and aqghawa authored Mar 13, 2021
1 parent 97c01d6 commit 5274c14
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 2 deletions.
96 changes: 95 additions & 1 deletion backend/app/donation/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,100 @@
from flask import request, abort, jsonify
from app.models import Donation
from . import donation
from .. import db
from app.models import Donation
import uuid
from datetime import datetime

# Create new donation
@donation.route("", methods=["POST"])
def create_donation():
# Load request to data
data = request.get_json(force=True)
# Set date to now
date = datetime.now()
# Load data to the corresponding variables
name = data.get("name")
email = data.get("email")
donation_source = data.get("donation_source")
event = data.get("event")
num_tickets = data.get("num_tickets")
added_by = data.get("added_by")
amount = data.get("amount")
# Check if nullable=False columns are empty
if name == "" or donation_source == "" or email == "" or data == {}:
abort(400, "Name, doantion_source, and email can not be empty")
new_donation = Donation(
name=name,
email=email,
date=date,
donation_source=donation_source,
event=event,
num_tickets=num_tickets,
added_by=added_by,
amount=amount,
)
db.session.add(new_donation)
db.session.commit()
return jsonify(new_donation.serialize)


# Update Donation
@donation.route("/<uuid:donation_uuid>", methods=["PUT"])
def update_donation(donation_uuid):
data = request.get_json(force=True)
donation = Donation.query.get(donation_uuid)
if donation is None:
abort(404, "No donation found with specified UUID")
name = data.get("name")
email = data.get("email")
date = data.get("date")
donation_source = data.get("donation_source")
event = data.get("event")
num_tickets = data.get("num_tickets")
added_by = data.get("added_by")
amount = data.get("amount")
if (
(name == "" or name is None)
and (email == "" or email is None)
and date is None
and (donation_source == "" or donation_source is None)
and (event == "" or event is None)
and num_tickets is None
and (added_by == "" or added_by is None)
and (amount is None)
and (data == {})
):
abort(400, "No donation fields to update")
if name:
donation.name = name
if email:
donation.email = email
if date:
donation.date = date
if donation_source:
donation.donation_source = donation_source
if event:
donation.event = event
if num_tickets:
donation.num_tickets = num_tickets
if added_by:
donation.added_by = added_by
if amount:
donation.amount = amount
db.session.add(donation)
db.session.commit()
return jsonify(donation.serialize)


# Delete Donation (#why_so_heartless)
@donation.route("/<uuid:donation_uuid>", methods=["DELETE"])
def delete_donation(donation_uuid):
donation = Donation.query.get(donation_uuid)
if donation is None:
abort(404, "No donation found with specified UUID")
db.session.delete(donation)
db.session.commit()
return jsonify(donation.serialize)


@donation.route("/amount", methods=["GET"])
Expand Down
Empty file added backend/grep
Empty file.
127 changes: 126 additions & 1 deletion backend/tests/test_donation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
import uuid
import json
from app import create_app, db
from app.models import Donation
from app.models import Donation, MuUser
import datetime
from sqlalchemy.exc import IntegrityError

Expand Down Expand Up @@ -84,3 +86,126 @@ def test_get_amount(self):
bad_email = "[email protected]"
response3 = self.client.get("/donation/amount?email={}".format(bad_email))
self.assertEqual(response3.status_code, 404)

def test_create_donation(self):
u_id = uuid.uuid4()
u = MuUser(
id=u_id,
name="dummy",
email="email_address",
role="dummy role",
)
db.session.add(u)
db.session.commit()

response = self.client.post(
"/donation",
json={
"name": "dummy name",
"email": "[email protected]",
"donation_source": "dummy source",
"event": "dummy event",
"num_tickets": 2,
"added_by": u_id,
"amount": 100,
},
)
response2 = self.client.post(
"/donation",
json={
"name": "dummy2 name",
"email": "[email protected]",
"donation_source": "dummy2 source",
"event": "dummy event 2",
"num_tickets": 15,
"added_by": u_id,
"amount": 50,
},
)
empty_response = self.client.post(
"/donation",
json={
"name": "",
"email": "",
"donation_source": "",
"event": "",
"num_tickets": None,
"added_by": None,
"amount": None,
},
)
# test posted correctly
self.assertEqual(empty_response.status_code, 400)
self.assertEqual(response.status_code, 200)
self.assertEqual(response2.status_code, 200)

def test_update_donation(self):
d_id = uuid.uuid4()
d = Donation(
id=d_id,
name="dummy",
email="dummy",
date=datetime.datetime.now(),
donation_source="dummy",
event="dummy",
num_tickets=2,
amount=200,
)
db.session.add(d)
db.session.commit()

# update a donation with empty argumets
response = self.client.put(
"/donation/{}".format(d_id),
content_type="application/json",
data=json.dumps({}),
)
self.assertEqual(response.status_code, 400)

# update a donation with valid arguments
update_obj = {
"name": "dummy2 name",
"email": "[email protected]",
"donation_source": "dummy2 source",
"event": "dummy event 2",
"num_tickets": 15,
"amount": 50,
}
response = self.client.put(
"/donation/{}".format(d_id),
content_type="application/json",
data=json.dumps(update_obj),
)
self.assertEqual(response.status_code, 200)
json_response = json.loads(response.get_data(as_text=True))
self.assertDictContainsSubset(update_obj, json_response)

# update a donation that does not exist
response = self.client.put(
"/donation/{}".format(uuid.uuid4()),
content_type="application/json",
data=json.dumps(update_obj),
)
self.assertEqual(response.status_code, 404)

def test_delete_donation(self):
d_id = uuid.uuid4()

response = self.client.delete("/donation/{}".format(d_id))
self.assertEqual(response.status_code, 404)

d = Donation(
id=d_id,
name="dummy",
email="dummy",
date=datetime.datetime.now(),
donation_source="dummy",
event="dummy",
num_tickets=2,
amount=200,
)
db.session.add(d)
db.session.commit()

response2 = self.client.delete("/donation/{}".format(d_id))
self.assertEqual(response2.status_code, 200)

0 comments on commit 5274c14

Please sign in to comment.