Skip to content

Instantly share code, notes, and snippets.

@jesperp
Forked from sorl/gist:979651
Created May 19, 2011 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jesperp/980369 to your computer and use it in GitHub Desktop.
Save jesperp/980369 to your computer and use it in GitHub Desktop.
Upp! API version 1
#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": "johnny@example.com",
... "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": "jonna@example.com",
... "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'johnny@example.com')
Updating profiles
-----------------
A PUT request to the detail view updates a profile
>>> data = { "name": "John Doe", 'phone': 555, 'email': 'a@aa.com' }
>>> 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'a@aa.com')
"""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment