Ticket #3466: tests.py

File tests.py, 1.8 KB (added by Simon G. <dev@…>, 18 years ago)
Line 
1r"""
2
3# uses the simple Article model from the "basic" tests
4>>> from models import Article
5
6>>> from datetime import datetime
7>>> a = Article(id=None, headline='Area man programs in Python', pub_date=datetime(2005, 7, 28))
8>>> a.save()
9
10# try json/xml/python without the fields keyword arg..
11
12>>> serializers.serialize("json", [a])
13'[{"pk": "1", "model": "serializerfields3466.article", "fields": {"headline": "Area man programs in Python", "pub_date": "2005-07-28 00:00:00"}}]'
14
15>>> serializers.serialize("xml", [a])
16'<?xml version="1.0" encoding="utf-8"?>\n<django-objects version="1.0"><object pk="1" model="serializerfields3466.article"><field type="CharField" name="headline">Area man programs in Python</field><field type="DateTimeField" name="pub_date">2005-07-28 00:00:00</field></object></django-objects>'
17
18>>> serializers.serialize("python", [a])
19[{'pk': '1', 'model': 'serializerfields3466.article', 'fields': {'headline': 'Area man programs in Python', 'pub_date': datetime.datetime(2005, 7, 28, 0, 0)}}]
20
21
22# now try with the keyword arg:
23
24>>> serializers.serialize("json", [a], fields=['headline'])
25'[{"pk": "1", "model": "serializerfields3466.article", "fields": {"headline": "Area man programs in Python"}}]'
26
27>>> serializers.serialize("xml", [a], fields=['headline'])
28'<?xml version="1.0" encoding="utf-8"?>\n<django-objects version="1.0"><object pk="1" model="serializerfields3466.article"><field type="CharField" name="headline">Area man programs in Python</field></object></django-objects>'
29
30>>> serializers.serialize("python", [a], fields=['headline'])
31[{'pk': '1', 'model': 'serializerfields3466.article', 'fields': {'headline': 'Area man programs in Python'}}]
32
33"""
34from django.core import serializers
35
36
37
38if __name__ == "__main__":
39 import doctest
40 doctest.testmod()
Back to Top