-
-
Save jesperp/980369 to your computer and use it in GitHub Desktop.
Upp! API version 1
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
#coding=utf-8 | |
__test__ = { | |
"api_v1": """ | |
Upp! API version 1 | |
================== | |
Testing the functionality of Upp! API | |
>>> from django.utils import simplejson as json | |
>>> from django.test.client import Client | |
>>> from events.models import Event | |
>>> c = Client() | |
Create event | |
------------ | |
A POST request to the event list url creates new events. | |
>>> data = { | |
... "third_party_id": 11111, | |
... "title": "Konferens", | |
... "start_date": "2011-03-21", | |
... "location": "Rum 3", | |
... } | |
>>> resp = c.post('/api/v1/events/', json.dumps(data), 'application/json') | |
>>> resp.status_code # Http201 = Created | |
201 | |
Let's create another one: | |
>>> data = { | |
... "third_party_id": 22222, | |
... "title": "Festivalen", | |
... "start_date": "2011-07-21", | |
... "start_time": "20:30", | |
... "end_date": "2011-07-25", | |
... "location": u"Slottsskogen, Göteborg", | |
... } | |
>>> resp = c.post('/api/v1/events/', json.dumps(data), 'application/json') | |
>>> resp.status_code # Http201 = Created | |
201 | |
List events | |
----------- | |
A GET request to the list view will return events | |
>>> resp = c.get('/api/v1/events/') | |
>>> resp.status_code | |
200 | |
>>> events = json.loads(resp.content) | |
>>> len(events['objects']) | |
2 | |
>>> events['meta']['total_count'] | |
2 | |
>>> events['objects'][1].pop('resource_uri') | |
u'/api/v1/events/2/' | |
>>> festivalen = Event(**events['objects'][1]) | |
>>> festivalen.id, festivalen.title | |
(u'2', u'Festivalen') | |
>>> festivalen.start_date, festivalen.start_time | |
(u'2011-07-21', u'20:30:00') | |
Update and show events | |
---------------------- | |
A PUT request to the detail view updates an event | |
>>> data = { | |
... "title": "Fest-I-Valen", | |
... "start_date": "2011-07-22", | |
... "end_time": "20:00", | |
... } | |
>>> eventurl = '/api/v1/events/%s/' % festivalen.id | |
>>> resp = c.put(eventurl, json.dumps(data), 'application/json') | |
>>> resp.content | |
'' | |
>>> resp.status_code # Http204 = No content | |
204 | |
Look at the changes: | |
>>> resp = c.get(eventurl) | |
>>> resp.status_code | |
200 | |
>>> event = json.loads(resp.content) | |
>>> event['title'] | |
u'Fest-I-Valen' | |
>>> event['start_date'] | |
u'2011-07-22' | |
>>> event['end_time'] | |
u'20:00:00' | |
Create profile | |
-------------- | |
A POST request to the profile list url creates new profiles. | |
>>> data = { | |
... "third_party_id": 10000, | |
... "name": "Johnny Degsson", | |
... "email": "[email protected]", | |
... "membership": "silver", | |
... "phone": "921384759", | |
... "is_active": True, | |
... } | |
>>> resp = c.post('/api/v1/profiles/', json.dumps(data), 'application/json') | |
>>> resp.status_code | |
201 | |
>>> resp['Location'] # Contains link to resource with id | |
'http://testserver/api/v1/profiles/.../' | |
Add another one... | |
>>> data = { | |
... "third_party_id": 10001, | |
... "name": "Jonna Degsson", | |
... "email": "[email protected]", | |
... "membership": "guld", | |
... "phone": "1234567", | |
... "is_active": True, | |
... } | |
>>> resp = c.post('/api/v1/profiles/', json.dumps(data), 'application/json') | |
>>> resp.status_code | |
201 | |
Listing profiles | |
---------------- | |
Look at our newly created profiles in the list view: | |
>>> resp = c.get('/api/v1/profiles/') | |
>>> resp.status_code | |
200 | |
>>> profiles = json.loads(resp.content) | |
>>> profiles['meta']['total_count'] | |
2 | |
>>> degsson = profiles['objects'][0] | |
>>> degsson['third_party_id'], degsson['name'], degsson['email'] | |
(10000, u'Johnny Degsson', u'[email protected]') | |
Updating profiles | |
----------------- | |
A PUT request to the detail view updates a profile | |
>>> data = { "name": "John Doe", 'phone': 555, 'email': '[email protected]' } | |
>>> profileurl = '/api/v1/profiles/%s/' % degsson['id'] | |
>>> resp = c.put(profileurl, json.dumps(data), 'application/json') | |
>>> resp.status_code # Http204 = No content | |
204 | |
Look at the changes: | |
>>> resp = c.get(profileurl) | |
>>> resp.status_code | |
200 | |
>>> profile = json.loads(resp.content) | |
>>> profile['name'], profile['phone'], profile['email'] | |
(u'John Doe', u'555', u'[email protected]') | |
""" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment