PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/blogpost/tests.py

https://github.com/thomasst/django-rest-framework
Python | 211 lines | 38 code | 13 blank | 160 comment | 1 complexity | ff0df77473bcf38a17303d90b51737d8 MD5 | raw file
  1. """Test a range of REST API usage of the example application.
  2. """
  3. from django.core.urlresolvers import reverse
  4. from django.test import TestCase
  5. from django.core.urlresolvers import reverse
  6. from django.utils import simplejson as json
  7. from djangorestframework.compat import RequestFactory
  8. from djangorestframework.views import InstanceModelView, ListOrCreateModelView
  9. from blogpost import models, urls
  10. #import blogpost
  11. # class AcceptHeaderTests(TestCase):
  12. # """Test correct behaviour of the Accept header as specified by RFC 2616:
  13. #
  14. # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1"""
  15. #
  16. # def assert_accept_mimetype(self, mimetype, expect=None):
  17. # """Assert that a request with given mimetype in the accept header,
  18. # gives a response with the appropriate content-type."""
  19. # if expect is None:
  20. # expect = mimetype
  21. #
  22. # resp = self.client.get(reverse(views.RootResource), HTTP_ACCEPT=mimetype)
  23. #
  24. # self.assertEquals(resp['content-type'], expect)
  25. #
  26. #
  27. # def dont_test_accept_json(self):
  28. # """Ensure server responds with Content-Type of JSON when requested."""
  29. # self.assert_accept_mimetype('application/json')
  30. #
  31. # def dont_test_accept_xml(self):
  32. # """Ensure server responds with Content-Type of XML when requested."""
  33. # self.assert_accept_mimetype('application/xml')
  34. #
  35. # def dont_test_accept_json_when_prefered_to_xml(self):
  36. # """Ensure server responds with Content-Type of JSON when it is the client's prefered choice."""
  37. # self.assert_accept_mimetype('application/json;q=0.9, application/xml;q=0.1', expect='application/json')
  38. #
  39. # def dont_test_accept_xml_when_prefered_to_json(self):
  40. # """Ensure server responds with Content-Type of XML when it is the client's prefered choice."""
  41. # self.assert_accept_mimetype('application/json;q=0.1, application/xml;q=0.9', expect='application/xml')
  42. #
  43. # def dont_test_default_json_prefered(self):
  44. # """Ensure server responds with JSON in preference to XML."""
  45. # self.assert_accept_mimetype('application/json,application/xml', expect='application/json')
  46. #
  47. # def dont_test_accept_generic_subtype_format(self):
  48. # """Ensure server responds with an appropriate type, when the subtype is left generic."""
  49. # self.assert_accept_mimetype('text/*', expect='text/html')
  50. #
  51. # def dont_test_accept_generic_type_format(self):
  52. # """Ensure server responds with an appropriate type, when the type and subtype are left generic."""
  53. # self.assert_accept_mimetype('*/*', expect='application/json')
  54. #
  55. # def dont_test_invalid_accept_header_returns_406(self):
  56. # """Ensure server returns a 406 (not acceptable) response if we set the Accept header to junk."""
  57. # resp = self.client.get(reverse(views.RootResource), HTTP_ACCEPT='invalid/invalid')
  58. # self.assertNotEquals(resp['content-type'], 'invalid/invalid')
  59. # self.assertEquals(resp.status_code, 406)
  60. #
  61. # def dont_test_prefer_specific_over_generic(self): # This test is broken right now
  62. # """More specific accept types have precedence over less specific types."""
  63. # self.assert_accept_mimetype('application/xml, */*', expect='application/xml')
  64. # self.assert_accept_mimetype('*/*, application/xml', expect='application/xml')
  65. #
  66. #
  67. # class AllowedMethodsTests(TestCase):
  68. # """Basic tests to check that only allowed operations may be performed on a Resource"""
  69. #
  70. # def dont_test_reading_a_read_only_resource_is_allowed(self):
  71. # """GET requests on a read only resource should default to a 200 (OK) response"""
  72. # resp = self.client.get(reverse(views.RootResource))
  73. # self.assertEquals(resp.status_code, 200)
  74. #
  75. # def dont_test_writing_to_read_only_resource_is_not_allowed(self):
  76. # """PUT requests on a read only resource should default to a 405 (method not allowed) response"""
  77. # resp = self.client.put(reverse(views.RootResource), {})
  78. # self.assertEquals(resp.status_code, 405)
  79. #
  80. # def test_reading_write_only_not_allowed(self):
  81. # resp = self.client.get(reverse(views.WriteOnlyResource))
  82. # self.assertEquals(resp.status_code, 405)
  83. #
  84. # def test_writing_write_only_allowed(self):
  85. # resp = self.client.put(reverse(views.WriteOnlyResource), {})
  86. # self.assertEquals(resp.status_code, 200)
  87. #
  88. #
  89. #class EncodeDecodeTests(TestCase):
  90. # def setUp(self):
  91. # super(self.__class__, self).setUp()
  92. # self.input = {'a': 1, 'b': 'example'}
  93. #
  94. # def test_encode_form_decode_json(self):
  95. # content = self.input
  96. # resp = self.client.put(reverse(views.WriteOnlyResource), content)
  97. # output = json.loads(resp.content)
  98. # self.assertEquals(self.input, output)
  99. #
  100. # def test_encode_json_decode_json(self):
  101. # content = json.dumps(self.input)
  102. # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json')
  103. # output = json.loads(resp.content)
  104. # self.assertEquals(self.input, output)
  105. #
  106. # #def test_encode_xml_decode_json(self):
  107. # # content = dict2xml(self.input)
  108. # # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/json')
  109. # # output = json.loads(resp.content)
  110. # # self.assertEquals(self.input, output)
  111. #
  112. # #def test_encode_form_decode_xml(self):
  113. # # content = self.input
  114. # # resp = self.client.put(reverse(views.WriteOnlyResource), content, HTTP_ACCEPT='application/xml')
  115. # # output = xml2dict(resp.content)
  116. # # self.assertEquals(self.input, output)
  117. #
  118. # #def test_encode_json_decode_xml(self):
  119. # # content = json.dumps(self.input)
  120. # # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
  121. # # output = xml2dict(resp.content)
  122. # # self.assertEquals(self.input, output)
  123. #
  124. # #def test_encode_xml_decode_xml(self):
  125. # # content = dict2xml(self.input)
  126. # # resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
  127. # # output = xml2dict(resp.content)
  128. # # self.assertEquals(self.input, output)
  129. #
  130. #class ModelTests(TestCase):
  131. # def test_create_container(self):
  132. # content = json.dumps({'name': 'example'})
  133. # resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json')
  134. # output = json.loads(resp.content)
  135. # self.assertEquals(resp.status_code, 201)
  136. # self.assertEquals(output['name'], 'example')
  137. # self.assertEquals(set(output.keys()), set(('absolute_uri', 'name', 'key')))
  138. #
  139. #class CreatedModelTests(TestCase):
  140. # def setUp(self):
  141. # content = json.dumps({'name': 'example'})
  142. # resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json', HTTP_ACCEPT='application/json')
  143. # self.container = json.loads(resp.content)
  144. #
  145. # def test_read_container(self):
  146. # resp = self.client.get(self.container["absolute_uri"])
  147. # self.assertEquals(resp.status_code, 200)
  148. # container = json.loads(resp.content)
  149. # self.assertEquals(container, self.container)
  150. #
  151. # def test_delete_container(self):
  152. # resp = self.client.delete(self.container["absolute_uri"])
  153. # self.assertEquals(resp.status_code, 204)
  154. # self.assertEquals(resp.content, '')
  155. #
  156. # def test_update_container(self):
  157. # self.container['name'] = 'new'
  158. # content = json.dumps(self.container)
  159. # resp = self.client.put(self.container["absolute_uri"], content, 'application/json')
  160. # self.assertEquals(resp.status_code, 200)
  161. # container = json.loads(resp.content)
  162. # self.assertEquals(container, self.container)
  163. #above testcases need to probably moved to the core
  164. class TestRotation(TestCase):
  165. """For the example the maximum amount of Blogposts is capped off at views.MAX_POSTS.
  166. Whenever a new Blogpost is posted the oldest one should be popped."""
  167. def setUp(self):
  168. self.factory = RequestFactory()
  169. models.BlogPost.objects.all().delete()
  170. def test_get_to_root(self):
  171. '''Simple get to the *root* url of blogposts'''
  172. request = self.factory.get('/blog-post')
  173. view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
  174. response = view(request)
  175. self.assertEqual(response.status_code, 200)
  176. def test_blogposts_not_exceed_MAX_POSTS(self):
  177. '''Posting blog-posts should not result in more than MAX_POSTS items stored.'''
  178. for post in range(models.MAX_POSTS + 5):
  179. form_data = {'title': 'This is post #%s' % post, 'content': 'This is the content of post #%s' % post}
  180. request = self.factory.post('/blog-post', data=form_data)
  181. view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
  182. view(request)
  183. self.assertEquals(len(models.BlogPost.objects.all()),models.MAX_POSTS)
  184. def test_fifo_behaviour(self):
  185. '''It's fine that the Blogposts are capped off at MAX_POSTS. But we want to make sure we see FIFO behaviour.'''
  186. for post in range(15):
  187. form_data = {'title': '%s' % post, 'content': 'This is the content of post #%s' % post}
  188. request = self.factory.post('/blog-post', data=form_data)
  189. view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
  190. view(request)
  191. request = self.factory.get('/blog-post')
  192. view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
  193. response = view(request)
  194. response_posts = json.loads(response.content)
  195. response_titles = [d['title'] for d in response_posts]
  196. response_titles.reverse()
  197. self.assertEquals(response_titles, ['%s' % i for i in range(models.MAX_POSTS - 5, models.MAX_POSTS + 5)])