Search code examples
pythondjangopython-unittest

Django test assert not equal


Trying to test selector layer im my app, but django dont pass simle test. Querysets looks quite similar, maybe i lost something.

test.py

from django.test import TestCase

from books.models import Author, Book
from books.selectors import get_books


class SelectorTest(TestCase):
    def setUp(self):
        self.author = Author.objects.create(
            name='test_author'
        )
        self.book = Book.objects.create(
            name='test_book',
            category='Drama',
            release_date='2001-01-01',
            author=self.author,
            is_read=True
        )

    def test_get_books(self):
         self.assertEqual(get_books(), Book.objects.all())

selectors.py

from django.db.models.query import QuerySet

from books.models import Book


def get_books() -> QuerySet[Book]:
    """
    Return all objects of Book model.
    """
    books = Book.objects.all()

    return books
    

assertion error

AssertionError: <QuerySet [<Book: test_book>]> != <QuerySet [<Book: test_book>]>

Solution

  • You can use the TransactionTestCase.assertQuerysetEqual assertion from Django's TestCase class. Check the documentation here.

    In your case:

        # …
    
        def test_get_books(self):
             self.assertQuerysetEqual(get_books(), Book.objects.all())