Created
March 15, 2015 23:44
-
-
Save doismellburning/0a63b86a20fa2770ce76 to your computer and use it in GitHub Desktop.
_Why_ does my test pass, when there's no `owner` filtering??
This file contains 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.conf import settings | |
from django.db import models | |
from django.utils.timezone import now | |
class Doable(models.Model): | |
short_description = models.CharField(max_length=120) | |
owner = models.ForeignKey(settings.AUTH_USER_MODEL) |
This file contains 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.contrib.auth import get_user_model | |
from django.test import TestCase | |
from rest_framework.test import ( | |
APIClient, | |
APIRequestFactory, | |
) | |
from ..models import * # noqa | |
import json | |
class TestAPI(TestCase): | |
def setUp(self): | |
self.factory = APIRequestFactory() | |
self.api_client = APIClient() | |
self.userA_password = "password" | |
self.userA = get_user_model().objects.create(username="A", password=self.userA_password) | |
self.userB_password = "password2" | |
self.userB = get_user_model().objects.create(username="B", password=self.userB_password) | |
self.doableA_short = "Lorem Ipsum" | |
self.doableA = Doable.objects.create( | |
short_description=self.doableA_short, | |
owner=self.userA, | |
) | |
self.doableB_short = "Dolor Sit Amet" | |
self.doableB = Doable.objects.create( | |
short_description=self.doableB_short, | |
owner=self.userB, | |
) | |
def test_getting_doables(self): | |
self.api_client.login(username=self.userA.username, password=self.userA_password) | |
response = self.api_client.get('/api/doables/') | |
doables = json.loads(response.content) | |
self.assertEquals(len(doables), 1) | |
self.assertEquals(doables[0]['short_description'], self.doableA_short) |
This file contains 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.conf.urls import url | |
from .views import * # noqa | |
urlpatterns = ( | |
url(r'api/doables/$', DoablesView.as_view()), | |
) |
This file contains 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 rest_framework import generics | |
from .models import * # noqa | |
from .serializers import * # noqa | |
class DoablesView(generics.ListCreateAPIView): | |
queryset = Doable.objects.all() | |
serializer_class = DoableSerializer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment