I'm new to Django and I recently changed the database from SQLite to PostgreSQL (first time using postgreSQL). I updated the settings with the below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost'
}
}
For the user, I'm not using the default postgress user, instead I created a new user (for which there is a User account on the OS) and added a password for it. I then gave that user permission to createdb:
ALTER USER username CREATEDB;
I've also installed psycopg2.
When using the api normally, the PostgreSQL database updates just fine and works as expected. Originally when using SQLite, all the tests used to pass too. But when I changed to PostgreSQL using the above, 2 out of 9 tests fail.
These are the tests that fail. The tests that fail are all under the same class. 6 of the tests are under a different class and they all pass fine.
class APIDetailTest(APITestCase):
items_url = reverse('site')
item_url = reverse('site-item', args = [1])
def setUp(self):
# Creating an initial entry to be tested.
data = {
"Name": "data name",
"Description": "desc",
"Date": "2022-06-11",
"Time": "11:00:00",
"Tag": "",
}
self.client.post(self.items_url, data, format='json')
def test_Get(self):
# Testing that getting an item by id returns the correct data.
response = self.client.get(self.item_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["Name"], "data name")
def test_Update_Item(self):
# Testing that changing the data works correctly.
data = {
"Name": "data name",
"Description": "desc 2",
"Date": "2022-06-11",
"Time": "11:00:00",
"Tag": "Work",
}
response = self.client.put(self.item_url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Checking that changed fields are changed correctly:
self.assertEqual(response.data["Description"], "desc 2")
self.assertEqual(response.data["Tag"], "Work")
# Checking that unchanged fields remained unchanged:
self.assertEqual(response.data["Name"], "data name")
def test_Delete_Item(self):
# Testing that deleting an item removes it fully.
response = self.client.delete(self.item_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Testing that the item no longer exists after it has been deleted
response = self.client.get(self.item_url)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
From the above the test_Get and the test_Update_Item both fail if I run the tests. However, when I remove 2 of the 3 tests from the class, each one always passes. But when running all three tests together they fail for some reason.
The error message is below:
Found 9 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.FF......
======================================================================
FAIL: test_Get (Calendar.Cal_API.tests.APIDetailTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/Documents/Calendar/Calendar/Calendar/Cal_API/tests.py", line 113, in test_Get
self.assertEqual(response.status_code, status.HTTP_200_OK)
AssertionError: 400 != 200
======================================================================
FAIL: test_Update_Item (Calendar.Cal_API.tests.APIDetailTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/Documents/Calendar/Calendar/Calendar/Cal_API/tests.py", line 126, in test_Update_Item
self.assertEqual(response.status_code, status.HTTP_200_OK)
AssertionError: 400 != 200
----------------------------------------------------------------------
Ran 9 tests in 0.324s
FAILED (failures=2)
Destroying test database for alias 'default'...
Does anybody know why this is?
Thanks for any help
source https://stackoverflow.com/questions/72585565/django-rest-framework-tests-failing-when-using-postgresql
Comments
Post a Comment