Django модель пользователя пост сохранить взломать юнит тестовый логин - PullRequest
0 голосов
/ 13 марта 2012

Этот сигнал, кажется, нарушает мои юнит-тесты для входа Я не уверен, почему?

Сигнал

@receiver(post_save, sender=User)
def update_profile(sender, instance, **kwargs):
    post_save.disconnect(update_profile, sender=User)
    if Profile.objects.filter(user=instance).exists():
        profile = Profile.objects.get(user=instance)
        if instance.first_name:
            profile.first_name = instance.first_name
        if instance.last_name:
            profile.last_name = instance.last_name
        if instance.email:
            profile.email = instance.email

        profile.save()
        post_save.connect(update_profile, sender=User)

post_save.connect(update_profile, sender=User)

Модульный тест

class AdminProfileUpdate(TestCase):
    def setUp(self):
        self.user = create_user(password='foobar')
        self.profile = self.user.get_or_create_profile
        self.client = Client()

    def test_profile_base_template(self)
        logged_in = self.client.login(username=self.user.username,
                password='foobar')
        self.assertTrue(logged_in)

1 Ответ

1 голос
/ 13 марта 2012

Вызов save() в обработчике после сохранения выглядит неудачным решением для меня.Вы пытались переопределить save вместо этого?Как это:

class MyModel(models.Model):
    ...

    def save(self, *args, **kwargs):
        # do the instance changes you want to be saved as well
        super(MyModel, self).save(*args, **kwargs) # do the save operation
        # update anything else, if you want to
...