-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -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) |