Тесты в Django используют основную БД

При тестировании Django проекта данные берутся из основной БД, хотя по задумке должна создаваться пустая тестовая БД. Использую PostgreSQL. Юзер, через которого подключается проект, имеет права на создание новых БД.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'books_db',
        'USER': 'books_user',
        'PASSWORD': '1234qwe',
        'HOST': 'localhost',
        'PORT': '',
        'TEST': {
            'NAME': 'test_books_db'
        }
    }
}

Пример теста:

from django.test import TestCase
from django.contrib.auth.models import User
from django.db.models import Case, When, Count

from store.models import Book, UserBookRelation
from store.serializers import BooksSerializer

class BookSerializerTestCase(TestCase):
    def test_ok(self):
        user = User.objects.create(username='test_username')
        user2 = User.objects.create(username='test_username_2')
        user3 = User.objects.create(username='test_username_3')

        book_1 = Book.objects.create(title='The book 1', price=100.00, author_name='Author 1')
        book_2 = Book.objects.create(title='The book 2', price=200.00, author_name='Author 2')

        UserBookRelation.objects.create(user=user, book=book_2, like=True)
        UserBookRelation.objects.create(user=user2, book=book_2, like=True)
        UserBookRelation.objects.create(user=user3, book=book_2, like=True)

        UserBookRelation.objects.create(user=user, book=book_1, like=True)
        UserBookRelation.objects.create(user=user2, book=book_1, like=True)
        UserBookRelation.objects.create(user=user3, book=book_1, like=False)

        books = Book.objects.all()
        data = BooksSerializer(books, many=True).data
        expected_data = [
            {
                'id': book_1.id,
                'title': 'The book 1',
                'price': '100.00',
                'author_name': 'Author 1',
                'likes_count': 2,
            },
            {
                'id': book_2.id,
                'title': 'The book 2',
                'price': '200.00',
                'author_name': 'Author 2',
                'likes_count': 3,
            }
        ]
        self.assertEqual(expected_data, data)

Результат теста, как пример того, что данные берутся из основной БД:

c:\prjcts\bookshop\bookshop\store\tests\test_serializers.py::BookSerializerTestCase::test_ok failed: self = <store.tests.test_serializers.BookSerializerTestCase testMethod=test_ok>

    def test_ok(self):
        user = User.objects.create(username='test_username')
        user2 = User.objects.create(username='test_username_2')
        user3 = User.objects.create(username='test_username_3')
        book_1 = Book.objects.create(title='The book 1', price=100.00, author_name='Author 1')
        book_2 = Book.objects.create(title='The book 2', price=200.00, author_name='Author 2')
        UserBookRelation.objects.create(user=user, book=book_2, like=True)
        UserBookRelation.objects.create(user=user2, book=book_2, like=True)
        UserBookRelation.objects.create(user=user3, book=book_2, like=True)
    
        UserBookRelation.objects.create(user=user, book=book_1, like=True)
        UserBookRelation.objects.create(user=user2, book=book_1, like=True)
        UserBookRelation.objects.create(user=user3, book=book_1, like=False)
    
        books = Book.objects.all()
        data = BooksSerializer(books, many=True).data
        expected_data = [
            {
                'id': book_1.id,
                'title': 'The book 1',
                'price': '100.00',
                'author_name': 'Author 1',
                'likes_count': 2,
            },
            {
                'id': book_2.id,
                'title': 'The book 2',
                'price': '200.00',
                'author_name': 'Author 2',
                'likes_count': 3,
            }
        ]
>       self.assertEqual(expected_data, data)
E       AssertionError: [{'id[189 chars]': 3}] != [{'id[189 chars]': 3}, {'id': 22, 'title': 'Captain Daugther',[276 chars]: 0}]

bookshop\store\tests\test_serializers.py:45: AssertionError

Ответы (0 шт):