Hello all,
I hope you can help me further.
I try to lern Django while going to the tutorial step by step. But now I’m stuck in the tutorial part 5 automated tests.
If I run
python manage.py test polls
I get the error that the user cannot create databases.
But in postgresql I granted my testuser the CREATE DATABASE command.
Role name | Attributes | Member of
------------+-------------+--------------
testuser | Create DB | {}
I started a dbshell after setting the PATH variable:
PYTHONPATH=$PYTHONPATH:$(pwd)
I can start a dbshell
python manage.py dbshell
With my database settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'OPTIONS': {
'service': 'my_service',
'passfile': '.my_pgpass',
},
'TEST': {'NAME': 'test'}
}
}
My .pg_service.conf
:
[my_service]
host=127.0.0.1
user=testuser
dbname=testdb
port=5432
My .my_pgpass
:
127.0.0.1:5432:testdb:testuser:testpassword
and within the dbshell the testuser can create and drop databases without any errors. All other operations (make migrations) also perform well without problems. If I create the test database within postgresql and then run
python manage.py test polls --keepdb
it complaints the database schema is restricted. I use the following versions:
Python 3.10.9
Django 4.1.7
Postgresql 15.1
My tests.py
:
import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question
class QuestionModelTests(TestCase):
import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question
class QuestionModelTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""was_published_recently() returns False for questions whose pub_date is in the future"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)
I hope someone could help me as I tried all I can.
Many thanks