Вопрос

I'm trying to create a test below and I get this message:

TypeError: 'datetime.datetime' object is not callable

on the line: self.stock.expiry_date(yesterday)

Test

 def test_stock_passed_expiration(self):
        """
        'True' returned when stock is passed it's expiration date.
        """
        yesterday = date.today()-timedelta(days=1)
        self.stock.expiry_date(yesterday)
        self.assertEqual(self.stock.has_expried(), True)

Model:

class Stock(models.Model):
    product = models.ForeignKey(Product, related_name="stocks")
    expiry_date = models.DateTimeField(default=timezone.now)


    def has_expried(self):
        if timezone.now > self.expiry_date:
            return True
        return False
Это было полезно?

Решение

You have

self.stock.expiry_date(yesterday)

but meant

self.stock.expiry_date = yesterday

The one you have tries to treat self.stock.expiry_date (a datetime object) as if it were a function, leading to that error message.

Другие советы

In your test function you are calling expiry_date as if it were a function. Instead, set the self.expiry_date attribute as yesterday, save the model and your test should work.

def test_stock_passed_expiration(self):
    """
    'True' returned when stock is passed it's expiration date.
    """
    yesterday = date.today()-timedelta(days=1)
    self.stock.expiry_date = yesterday
    self.stock.save()
    self.assertEqual(self.stock.has_expried(), True)

Using

self.stock.expiry_date = yesterday
self.stock.save()

or

self.stock.set(expiry_date=yesterday).save()

instead wil help you

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top