Django - 在测试中获取用户对象

时间:2012-10-23 16:19:06

标签: django unit-testing testing

我有这样的models.py:

from django.db import models
from django.contrib.auth.models import User


# everything has a colour...
class Colour(models.Model):
    colour = models.CharField(max_length=100)

    def __unicode__(self):
        return self.colour


# a thing can have a colour...
class Thing(models.Model):
    name = models.CharField(max_length=155)
    colour = models.ForeignKey(Colour)
    description = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name


# a user can save his choices
class UserLikes(models.Model):
    user = models.ForeignKey(User)
    colour = models.ForeignKey(Colour)
    thing = models.ForeignKey(Thing)

    class Meta:
        verbose_name_plural = "User Likes"

    def __unicode__(self):
        return '%d - %s' % (self.pk, self.user)

在我看来:

def ThingPicker(request):
    if request.method == 'POST':
        form = ThingForm(request.POST)
        if form.is_valid():
            colour = form.cleaned_data['colour']
            thing = form.cleaned_data['thing']
            likes = UserLikes(user=request.user, colour=colour, thing=thing)
            likes.save()
            return HttpResponseRedirect('/profile/')
        else:
            return render_to_response('thing_picker.html', {
                'form': form,
            }, RequestContext(request))
    else:
        form = ThingForm()
        return render_to_response('thing_picker.html', {
            'form': form,
        }, RequestContext(request))

在我的tests.py中,我想做这样的事情( EDITED ):

class ViewTests(TestCase):
    def setUp(self):
        self.client = Client()      

    def test_thingpicker(self):
        User.objects.create_superuser('foo', 'myemail@test.com', 'bar')
        self.client.login(username='foo', password='bar') # request.user is now user foo

        response = self.client.post('/colours/things/', {'colour': 1, 'thing': 2})
        userlike = UserLikes.objects.get(pk=1)
        self.assertEqual(userlike.colour_id, 1)

我收到错误:

DoesNotExist: UserLikes matching query does not exist.

如果我在shell中尝试使用测试客户端:

>>>  c = Client()
>>>  user1 = User.objects.create_superuser('foo', 'myemail@test.com', 'bar')
>>>  c.login(username='foo', password='bar')
>>>  c.post('/colours/things/', {'user': user1, 'colour': 1, 'thing': 2})
>>>  userlike = UserLikes.objects.get(pk=1)
>>>  userlike.colour_id
1L

我得到了预期的结果。

2 个答案:

答案 0 :(得分:1)

您的视图代码有些奇怪(意外剪切和粘贴?),但是从以下代码行开始:

likes = UserLikes(user=request.user, colour=colour, thing=thing)

我猜您正在获取当前登录的用户。要从测试用例中正确获取新创建的超级用户,您应该这样做:

def test_thingpicker(self):
    user1 = User.objects.create_user('foo', 'myemail@test.com', 'bar')

    self.client.login(username='foo', password='bar') # request.user is now user foo

    response = self.client.post('/colours/things/', {'colour': 1, 'thing': 2})
    userlike = UserLikes.objects.get(user=user1, color=1, thing=2)

此外,您应该注意request.user可能是AnonymousUser(请参阅https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.AnonymousUser),因此您应该在创建UserLikes之前检查用户是否已登录。

您可以使用@login_required装饰器(https://docs.djangoproject.com/en/dev/topics/auth/#the-login-required-decorator)或手动检查request.user.is_authenticated()https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.AbstractBaseUser.is_anonymous)来完成此操作。

为什么没有创建UserLikes

从视图:

colour = form.cleaned_data['colour']
thing = form.cleaned_data['thing']
likes = UserLikes(user=request.user, colour=colour, thing=thing)

请注意,UserLikes的Model定义使用ForeignKey来实现Color和Thing。我猜测表单中的colourthingIntegerField,因此您需要检索实际的颜色和事物对象。

color = Colour.objects.get(pk=form.cleaned_data['colour'])
thing = Thing.objects.get(pk=form.cleaned_data['thing'])
likes = UserLikes(user=request.user, colour=colour, thing=thing)

当然,您需要确保之前已经创建了Color和Thing对象。

答案 1 :(得分:0)

最终的代码有效:

<强> tests.py:

class ViewTests(TestCase):
    """
    Run before each test in class
    """
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_superuser(
            'foo',
            'myemail@test.com',
            'pass'
        )
        self.colour = Colour.objects.create(colour='Green')
        self.thing = Thing.objects.create(
            name='Leaf',
            colour=self.colour,
            description='leafy'
        )
        self.logged_in = self.client.login(
            username='foo',
            password='pass'
        )

    """
    Test whether user can login and post choice
    to db and then check if data can be retreived.
    """
    def test_thingpicker(self):
        # check new User object created
        self.assertEqual(User.objects.count(), 1)
        # check use is logged in
        self.assertEqual(self.logged_in, True)  # check login success

        # post some data
        response = self.client.post(
            '/colours/things/', {
            'colour': self.colour.pk,
            'thing': self.thing.pk
            })
        self.assertEqual(response.status_code, 200)

        # check object
        self.assertEqual(UserLikes.objects.count(), 1)
        userlikes = UserLikes.objects.get(
            user=self.user,
            colour_id=self.colour.pk,
            thing=self.thing.pk
        )
        self.assertEqual(userlikes.colour_id, self.colour.pk)