Answer the question
In order to leave comments, you need to log in
Why is shell queryset not empty, but empty in tests.py?
I want to write a test to check the task in celery, and it would seem that I just check the functions, but it doesn’t work because the queryset is empty.
In shell - Content.objects.all() =
<QuerySet [<Content: Content object (3)>, <Content: Content object (4)>]>
`<QuerySet []>`
class TaskTest(TestCase):
def test_scale_image(self):
path = Content.objects.get(id=4).image.path
output_size = update_image(path,500, 500)
self.assertEqual(output_size, (500,500))
class Content(models.Model):
width = models.IntegerField()
heigth = models.IntegerField()
image = models.ImageField(blank=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
try:
update_image.delay(self.image.path, self.width, self.heigth)
except Exception as e:
logger.error('Ошибка в таске: {}'.format(e))
@shared_task
def update_image(path, width, heigth):
image = Image.open(path)
output_size = (width, heigth)
image.thumbnail(output_size)
image.save(path)
return output_size
Answer the question
In order to leave comments, you need to log in
Because before running the tests, Django creates an empty test database, rolls migrations onto it, and runs the actual tests. And at the end it deletes everything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question