Get test-only models properly working

This commit is contained in:
Tom Christie 2012-09-28 14:28:50 +01:00
parent 84f7758039
commit e003cc91b6
6 changed files with 79 additions and 31 deletions

View File

@ -2,7 +2,7 @@
Generic views that provide commmonly needed behaviour.
"""
from rest_framework import views, mixins
from rest_framework import views, mixins, serializers
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.list import MultipleObjectMixin
@ -18,11 +18,19 @@ class BaseView(views.APIView):
def get_serializer(self, data=None, files=None, instance=None):
# TODO: add support for files
# TODO: add support for seperate serializer/deserializer
serializer_class = self.serializer_class
if serializer_class is None:
class DefaultSerializer(serializers.ModelSerializer):
class Meta:
model = self.model
serializer_class = DefaultSerializer
context = {
'request': self.request,
'format': self.kwargs.get('format', None)
}
return self.serializer_class(data, instance=instance, context=context)
return serializer_class(data, instance=instance, context=context)
class MultipleObjectBaseView(MultipleObjectMixin, BaseView):

View File

@ -100,7 +100,6 @@ import django
if django.VERSION < (1, 3):
INSTALLED_APPS += ('staticfiles',)
# OAuth support is optional, so we only test oauth if it's installed.
try:
import oauth_provider

View File

@ -114,7 +114,7 @@ class BaseSerializer(Field):
Returns the complete set of fields for the object as a dict.
This will be the set of any explicitly declared fields,
plus the set of fields returned by get_default_fields().
plus the set of fields returned by default_fields().
"""
ret = SortedDict()
@ -234,7 +234,7 @@ class BaseSerializer(Field):
return dict([(key, self.to_native(val))
for (key, val) in obj.items()])
elif hasattr(obj, '__iter__'):
return (self.to_native(item) for item in obj)
return [self.to_native(item) for item in obj]
return self.convert_object(obj)
def from_native(self, data):

View File

@ -1,4 +1,7 @@
"""Force import of all modules in this package in order to get the standard test runner to pick up the tests. Yowzers."""
"""
Force import of all modules in this package in order to get the standard test
runner to pick up the tests. Yowzers.
"""
import os
modules = [filename.rsplit('.', 1)[0]

View File

@ -0,0 +1,30 @@
from django.test import TestCase
from django.test.client import RequestFactory
from rest_framework import generics, status
from rest_framework.tests.models import BasicModel
factory = RequestFactory()
class RootView(generics.RootAPIView):
model = BasicModel
class TestListView(TestCase):
def setUp(self):
items = ['foo', 'bar', 'baz']
for item in items:
BasicModel(text=item).save()
self.objects = BasicModel.objects
self.data = [
{'id': obj.id, 'text': obj.text}
for obj in self.objects.all()
]
def test_get_root_view(self):
view = RootView.as_view()
request = factory.get('/')
response = view(request).render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.data, self.data)

View File

@ -1,28 +1,36 @@
from django.db import models
from django.contrib.auth.models import Group
class CustomUser(models.Model):
"""
A custom user model, which uses a 'through' table for the foreign key
"""
username = models.CharField(max_length=255, unique=True)
groups = models.ManyToManyField(
to=Group, blank=True, null=True, through='UserGroupMap'
)
@models.permalink
def get_absolute_url(self):
return ('custom_user', (), {
'pk': self.id
})
# from django.contrib.auth.models import Group
class UserGroupMap(models.Model):
user = models.ForeignKey(to=CustomUser)
group = models.ForeignKey(to=Group)
@models.permalink
def get_absolute_url(self):
return ('user_group_map', (), {
'pk': self.id
})
# class CustomUser(models.Model):
# """
# A custom user model, which uses a 'through' table for the foreign key
# """
# username = models.CharField(max_length=255, unique=True)
# groups = models.ManyToManyField(
# to=Group, blank=True, null=True, through='UserGroupMap'
# )
# @models.permalink
# def get_absolute_url(self):
# return ('custom_user', (), {
# 'pk': self.id
# })
# class UserGroupMap(models.Model):
# user = models.ForeignKey(to=CustomUser)
# group = models.ForeignKey(to=Group)
# @models.permalink
# def get_absolute_url(self):
# return ('user_group_map', (), {
# 'pk': self.id
# })
class BasicModel(models.Model):
text = models.CharField(max_length=100)
class Meta:
app_label = 'rest_framework'