pixis
pixis

Reputation: 2891

assertEqual transforms a value into None

I'm very disturbed by this error. Here is some context

I have a Challenge model

class Challenge(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=2000, blank=True, null=True)
    author = models.ForeignKey(Member, related_name="challenge_author")
    category = models.ForeignKey(ChallengeCategory)
    rank = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
    validations = models.ManyToManyField(Member,
                                         through=Validation,
                                         related_name="validated_users",
                                         blank=True)
    def update_rank(self):
        ranks = [0 if validation.rank is None else int(validation.rank)
                 for validation
                 in Validation.objects.filter(challenge__pk=self.id,)]
        self.rank = sum(ranks)/len(ranks)
        self.save()

And I have a test to test update_rank()

def test_update_rank(self):
    hello_world = Challenge.objects.get(name="HelloWorld")
    hello_world.update_rank()
    print(hello_world.rank == 9.00)
    self.assertEqual(hello_world.rank, 9.00)

When I run the test, I get :

True

Failure
Traceback (most recent call last):
  File "/.../website/tests/test_models.py", line 105, in test_update_validation
    self.assertEqual(hello_world.rank, 9.00)
AssertionError: None != 9

So it means that hello_world.rank is equal to 9.00 according to the print() but for some reasons, assertEqual thinks that hello_world.rank is None

I don't know what to think. If you need more information, please do not hesitate.

Thank you very much for your help.

Upvotes: 0

Views: 278

Answers (1)

alecxe
alecxe

Reputation: 473903

According to the traceback, the test_update_validation method is failing, not the test_update_rank which you've presented.

Upvotes: 1

Related Questions