I am trying to do unittests but it does not work.
Here is my file of tests :
class SimpleTest(TestCase):
def authenticate(self):
useremail = 'myemail@email.fr'
userpassword = 'password'
c = self.client
loggedin = c.login(email=useremail, password=userpassword)
self.assertTrue(loggedin, "Not logged")
def test_1(self):
self.authenticate()
c = self.client
data = {
'searched': 'companylimit'
}
response = c.post('/djangoproject/get_information/', data, follow=True)
self.assertEqual(response.status_code, 200)
logger.info(pformat(json.loads(response.content)['result']))
self.assertIs(type(json.loads(response.content)['result']), int)
time.sleep(5)
def test_2(self):
self.authenticate()
c = self.client
data = {
'searched': 'management',
'requested': User.objects.filter(is_active=True)[0].uniqueid
}
response = c.post('/djangoproject/get_information/', data, follow=True)
self.assertEqual(response.status_code, 200)
logger.info(pformat(json.loads(response.content)['result']))
if type(json.loads(response.content)['result']) == list:
self.assertIs(type(json.loads(response.content)['result']), list)
else:
self.assertEqual(json.loads(response.content)['result'], "")
I got django.db.utils.InterfaceError: connection already closed
because I have 2 tests whereas with just one test it works.
Could you help me please ?
Thank you very much !
EDIT :Here is the full traceback :
ERROR: test_2
----------------------------------------------------------------------
Traceback (most recent call last):
File "/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 234, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "/env/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 222, in create_cursor
cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/tests/test_3.py", line 47, in test_2
self.authenticate()
File "/tests/test_3.py", line 26, in authenticate
loggedin = c.login(email=useremail, password=userpassword)
File "/env/lib/python3.8/site-packages/django/test/client.py", line 594, in login
user = authenticate(**credentials)
File "/env/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate
user = backend.authenticate(request, **credentials)
File "/env/lib/python3.8/site-packages/django/contrib/auth/backends.py", line 16, in authenticate
user = UserModel._default_manager.get_by_natural_key(username)
File "/env/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 44, in get_by_natural_key
return self.get(**{self.model.USERNAME_FIELD: username})
File "/env/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/env/lib/python3.8/site-packages/django/db/models/query.py", line 393, in get
num = len(clone)
File "/env/lib/python3.8/site-packages/django/db/models/query.py", line 250, in __len__
self._fetch_all()
File "/env/lib/python3.8/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/env/lib/python3.8/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
cursor = self.connection.cursor()
File "/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 234, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "/env/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/env/lib/python3.8/site-packages/django/db/backends/base/base.py", line 234, in _cursor
return self._prepare_cursor(self.create_cursor(name))
File "/env/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 222, in create_cursor
cursor = self.connection.cursor()
django.db.utils.InterfaceError: connection already closed