Skip to main content

Django Rest Framework Tests Failing When Using PostgreSQL

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

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype