Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Contact deletion and more test improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Jul 25, 2014
1 parent 0fdefb1 commit 592eebe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
19 changes: 14 additions & 5 deletions go_http/contacts.py
Expand Up @@ -51,6 +51,15 @@ def _api_request(self, method, api_path, data=None):
r.raise_for_status()
return r.json()

def create_contact(self, contact_data):
"""
Create a contact.
:param dict contact_data:
Data for new contact.
"""
return self._api_request("POST", "", contact_data)

def get_contact(self, contact_key):
"""
Get a contact.
Expand All @@ -60,11 +69,11 @@ def get_contact(self, contact_key):
"""
return self._api_request("GET", contact_key)

def create_contact(self, contact_data):
def delete_contact(self, contact_key):
"""
Create a contact.
Delete a contact.
:param dict contact_data:
Data for new contact.
:param str contact_key:
Key for the contact to delete.
"""
return self._api_request("POST", "", contact_data)
return self._api_request("DELETE", contact_key)
72 changes: 54 additions & 18 deletions go_http/tests/test_contacts.py
Expand Up @@ -65,6 +65,8 @@ def handle_request(self, request):

if request.method == "GET":
return self.get_contact(contact_key, request)
elif request.method == "DELETE":
return self.delete_contact(contact_key, request)
else:
return self.build_response("", 405)

Expand All @@ -84,9 +86,16 @@ def create_contact(self, request):
self.contacts_data[contact[u"key"]] = contact
return self.build_response(json.dumps(contact))

def get_contact(self, path, request):
def get_contact(self, contact_key, request):
# TODO: Confirm this behaviour against the real API.
contact = self.contacts_data.get(path)
contact = self.contacts_data.get(contact_key)
if contact is None:
return self.build_response("Contact not found.", 404)
return self.build_response(json.dumps(contact))

def delete_contact(self, contact_key, request):
# TODO: Confirm this behaviour against the real API.
contact = self.contacts_data.pop(contact_key, None)
if contact is None:
return self.build_response("Contact not found.", 404)
return self.build_response(json.dumps(contact))
Expand Down Expand Up @@ -130,6 +139,15 @@ def make_client(self, auth_token=AUTH_TOKEN):
return ContactsApiClient(
auth_token, api_url=self.API_URL, session=self.session)

def make_existing_contact(self, contact_data):
existing_contact = make_contact_dict(contact_data)
self.contacts_data[existing_contact[u"key"]] = existing_contact
return existing_contact

def assert_contact_status(self, contact_key, exists=True):
exists_status = (contact_key in self.contacts_data)
self.assertEqual(exists_status, exists)

def assert_http_error(self, expected_status, func, *args, **kw):
try:
func(*args, **kw)
Expand Down Expand Up @@ -177,22 +195,6 @@ def test_auth_failure(self):
contacts = self.make_client(auth_token="bogus_token")
self.assert_http_error(403, contacts.get_contact, "foo")

def test_get_missing_contact(self):
contacts = self.make_client()
self.assert_http_error(404, contacts.get_contact, "foo")

def test_get_contact(self):
contacts = self.make_client()
existing_contact = make_contact_dict({
u"msisdn": u"+15556483",
u"name": u"Arthur",
u"surname": u"of Camelot",
})
self.contacts_data[existing_contact[u"key"]] = existing_contact

contact = contacts.get_contact(existing_contact[u"key"])
self.assertEqual(contact, existing_contact)

def test_create_contact(self):
contacts = self.make_client()
contact_data = {
Expand All @@ -206,6 +208,7 @@ def test_create_contact(self):
# The key is generated for us.
expected_contact[u"key"] = contact[u"key"]
self.assertEqual(contact, expected_contact)
self.assert_contact_status(contact[u"key"], exists=True)

def test_create_contact_with_key(self):
contacts = self.make_client()
Expand All @@ -216,3 +219,36 @@ def test_create_contact_with_key(self):
u"surname": u"of Camelot",
}
self.assert_http_error(400, contacts.create_contact, contact_data)
self.assert_contact_status(u"foo", exists=False)

def test_get_contact(self):
contacts = self.make_client()
existing_contact = self.make_existing_contact({
u"msisdn": u"+15556483",
u"name": u"Arthur",
u"surname": u"of Camelot",
})

contact = contacts.get_contact(existing_contact[u"key"])
self.assertEqual(contact, existing_contact)

def test_get_missing_contact(self):
contacts = self.make_client()
self.assert_http_error(404, contacts.get_contact, "foo")

def test_delete_contact(self):
contacts = self.make_client()
existing_contact = self.make_existing_contact({
u"msisdn": u"+15556483",
u"name": u"Arthur",
u"surname": u"of Camelot",
})

self.assert_contact_status(existing_contact[u"key"], exists=True)
contact = contacts.delete_contact(existing_contact[u"key"])
self.assertEqual(contact, existing_contact)
self.assert_contact_status(existing_contact[u"key"], exists=False)

def test_delete_missing_contact(self):
contacts = self.make_client()
self.assert_http_error(404, contacts.delete_contact, "foo")

0 comments on commit 592eebe

Please sign in to comment.