Last active
August 29, 2015 14:06
-
-
Save rochacon/fa014de300cfb26ebd6e to your computer and use it in GitHub Desktop.
A different approach to Django view unit testing
This file contains hidden or 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
from django.db import models | |
class Twt(models.Model): | |
txt = models.CharField(max_length=140) |
This file contains hidden or 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
from django.http import HttpRequest | |
from django.test import TestCase | |
from .models import Twt | |
from . import views | |
class TwtTestCase(TestCase): | |
def test_list_empty(self): | |
req = HttpRequest() | |
resp = views.get(req) | |
self.assertEquals(resp.status_code, 200) | |
self.assertEquals(resp['Content-Type'], 'application/json') | |
self.assertJSONEqual(resp.content, []) | |
def test_list_with_values(self): | |
t = Twt.objects.create(txt='140 characters should be enough') | |
req = HttpRequest() | |
resp = views.get(req) | |
self.assertEquals(resp.status_code, 200) | |
self.assertEquals(resp['Content-Type'], 'application/json') | |
self.assertJSONEqual(resp.content, [{'txt': t.txt}]) |
This file contains hidden or 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
import json | |
from django.http import HttpResponse | |
from .models import Twt | |
def get(request): | |
twts = [{'txt': t.txt} for t in Twt.objects.all()] | |
return HttpResponse(json.dumps(twts), content_type='application/json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment