M
M
marselabdullin2020-06-11 19:08:35
Django
marselabdullin, 2020-06-11 19:08:35

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)>]>


In tests.py - Content.objects.all() = `<QuerySet []>`

tests.py:

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))

models.py:

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))


tasks.py:

@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

1 answer(s)
S
Sergey Tikhonov, 2020-06-11
@marselabdullin

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 question

Ask a Question

731 491 924 answers to any question