Commit ecb82b03 by markotibold

Refactoring and added another test for blog-posts. Finally got eclipse setup…

Refactoring and added another test for blog-posts. Finally got eclipse setup properly with virtualenvs (code analysis). Also I think  I get Bitbuckets workflow now.
parent d6e7e95d
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
from django.test import TestCase from django.test import TestCase
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from blogpost import views, models from blogpost import views, models
import blogpost
#import json #import json
#from rest.utils import xml2dict, dict2xml #from rest.utils import xml2dict, dict2xml
...@@ -164,14 +165,16 @@ class AllowedMethodsTests(TestCase): ...@@ -164,14 +165,16 @@ class AllowedMethodsTests(TestCase):
#above testcases need to probably moved to the core #above testcases need to probably moved to the core
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
import json
class TestRotation(TestCase): class TestRotation(TestCase):
"""For the example the maximum amount of Blogposts is capped off at 10. """For the example the maximum amount of Blogposts is capped off at views.MAX_POSTS.
Whenever a new Blogpost is posted the oldest one should be popped.""" Whenever a new Blogpost is posted the oldest one should be popped."""
def setUp(self): def setUp(self):
self.factory = RequestFactory() self.factory = RequestFactory()
models.BlogPost.objects.all().delete()
def test_get_to_root(self): def test_get_to_root(self):
'''Simple test to demonstrate how the requestfactory needs to be used''' '''Simple test to demonstrate how the requestfactory needs to be used'''
request = self.factory.get('/blog-post') request = self.factory.get('/blog-post')
...@@ -179,13 +182,26 @@ class TestRotation(TestCase): ...@@ -179,13 +182,26 @@ class TestRotation(TestCase):
response = view(request) response = view(request)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_blogposts_not_exceed_10(self): def test_blogposts_not_exceed_MAX_POSTS(self):
'''Posting blogposts should not result in more than 10 items stored.''' '''Posting blog-posts should not result in more than MAX_POSTS items stored.'''
models.BlogPost.objects.all().delete() for post in range(views.MAX_POSTS + 5):
for post in range(15):
form_data = {'title': 'This is post #%s' % post, 'content': 'This is the content of post #%s' % post} form_data = {'title': 'This is post #%s' % post, 'content': 'This is the content of post #%s' % post}
request = self.factory.post('/blog-post', data=form_data) request = self.factory.post('/blog-post', data=form_data)
view = views.BlogPosts.as_view() view = views.BlogPosts.as_view()
response = view(request) view(request)
self.assertEquals(len(models.BlogPost.objects.all()),10) self.assertEquals(len(models.BlogPost.objects.all()),views.MAX_POSTS)
def test_fifo_behaviour(self):
'''It's fine that the Blogposts are capped off at MAX_POSTS. But we want to make sure we see FIFO behaviour.'''
for post in range(15):
form_data = {'title': '%s' % post, 'content': 'This is the content of post #%s' % post}
request = self.factory.post('/blog-post', data=form_data)
view = views.BlogPosts.as_view()
view(request)
request = self.factory.get('/blog-post')
view = views.BlogPosts.as_view()
response = view(request)
response_posts = json.loads(response.content)
response_titles = [d['title'] for d in response_posts]
self.assertEquals(response_titles, ['%s' % i for i in range(views.MAX_POSTS - 5, views.MAX_POSTS + 5)])
\ No newline at end of file
from djangorestframework.response import Response
from djangorestframework.resource import Resource
from djangorestframework.modelresource import ModelResource, RootModelResource from djangorestframework.modelresource import ModelResource, RootModelResource
from djangorestframework import status
from blogpost import models from blogpost import models
BLOG_POST_FIELDS = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url') BLOG_POST_FIELDS = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url')
COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url') COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url')
MAX_POSTS = 10
class BlogPosts(RootModelResource): class BlogPosts(RootModelResource):
"""A resource with which lists all existing blog posts and creates new blog posts.""" """A resource with which lists all existing blog posts and creates new blog posts."""
......
from django.test import TestCase from django.test import TestCase
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
from pygments_api import views from pygments_api import views
import os, tempfile, shutil, time, json import tempfile, shutil, json
class TestPygmentsExample(TestCase): class TestPygmentsExample(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment