Skip to content

Instantly share code, notes, and snippets.

@pdvyas
Last active March 14, 2016 04:06
Show Gist options
  • Save pdvyas/185a23967c9b075908bd to your computer and use it in GitHub Desktop.
Save pdvyas/185a23967c9b075908bd to your computer and use it in GitHub Desktop.
"""
Script to test assignment 1. Subject to change.
"""
import json
import unittest
try:
import requests
except Exception as e:
print "Requests library not found. Please install it. \nHint: pip install requests"
person = {
"email": "foo@gmail.com",
"zip": "95110",
"country": "U.S.A",
"profession": "student",
"favorite_color": "blue",
"is_smoking": "no",
"favorite_sport": "hiking",
"food": {
"type": "vegetarian",
"drink_alcohol": "yes"
},
"music": {
"spotify_user_id": "wizzler"
},
"movie": {
"tv_shows": ["The Big Bang Theory"],
"movies": ["Taken"]
},
"travel": {
"flight": {
"seat": "aisle"
}
}
}
change_person = {
"travel": {
"flight": {
"seat": "window"
}
},
"favorite_sport": "football"
}
url_prefix = "http://localhost:3000"
class TestPost(unittest.TestCase):
def test_a_post(self):
post_url = "{}/profile".format(url_prefix)
r = requests.post(post_url, data=json.dumps(person))
r.raise_for_status()
self.assertEqual(r.status_code, 201)
def test_b_get(self):
get_url = "{}/profile/{}".format(url_prefix, person['email'])
r = requests.get(get_url)
r.raise_for_status()
self.assertEqual(r.status_code, 200)
resp = r.json()
if isinstance(resp, list):
resp = resp[0]
self.assertEqual(r.status_code, 200)
self.assertEqual(resp["zip"], person["zip"])
self.assertEqual(type(resp.get("food")), dict)
self.assertEqual(resp['movie']['movies'][0], person['movie']['movies'][0])
def test_c_put(self):
put_url = "{}/profile/{}".format(url_prefix, person['email'])
r = requests.put(put_url, data=json.dumps(change_person))
r.raise_for_status()
self.assertEqual(r.status_code, 204)
get_url = "{}/profile/{}".format(url_prefix, person['email'])
r = requests.get(get_url)
self.assertEqual(r.status_code, 200)
resp = r.json()
if isinstance(resp, list):
resp = resp[0]
self.assertEqual(resp['travel']['flight']['seat'], change_person['travel']['flight']['seat'])
self.assertEqual(resp['favorite_sport'], change_person['favorite_sport'])
def test_d_delete(self):
delete_url = "{}/profile/{}".format(url_prefix, person['email'])
r = requests.delete(delete_url)
self.assertEqual(r.status_code, 204)
get_url = "{}/profile/{}".format(url_prefix, person['email'])
r = requests.get(get_url)
self.assertNotEqual(r.status_code, 200)
if __name__ == '__main__':
unittest.main()
@nagkumar91
Copy link

Thank you!
I will use this from the next time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment