Test fails with `django.db.utils.IntegrityError`

mhs

My test case gives me following error.

django.db.utils.IntegrityError: insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_c564eba6_fk_auth_user_id" DETAIL: Key (user_id)=(1) is not present in table "auth_user".

In my view class I have a log entry, during the test request.user.id is always None. so it use anonymous_user which is id = 1. (If I commented out the LogEntry.objects.log_action(), the test pass)

My view class:

class MyView(CreateAPIView):
    # For admin LogEntry
    anonymous_user_id = get_anonymous_user_id()

    def post(self, request, *args, **kwargs):
        """
        ...
        """

        LogEntry.objects.log_action(
            user_id=request.user.id or self.anonymous_user_id,
            content_type_id=self.content_type_id,
            object_id=target.id,
            object_repr=str(target),
            action_flag=ADDITION,
            change_message='message',
        )

        return Response({}, status=status.HTTP_200_OK)

My test:

def test_myview_append_api_works(self):

    def myview_append(url, p1, p2):
        resp = None
        resp = self.client.post(url, data={'paraphrase': p1, 'data_id': p2})
        return resp

    url = reverse('api-my_data:paraphrase_append')
    current_qa = Qa.objects.all()[0]  # get current existing qa = the qa created in the setUp

    p1 = 'test paraphrase'
    p2 = target.id
    resp = myview_append(url, p1, p2)
    self.assertEqual(resp.status_code, status.HTTP_200_OK)

I Tried using request_factory to set user in request but it didn't work

        request = self.request_factory.post(url,  data={'paraphrase': p1, 'qa_id': p2})
        request.user = self.user

        resp = MyView.as_view()(request)

Can any one help me with this test.

schillingt

LogEntry.user requires an actual user record in your database. As a part of your setUp code in your test you need to create a user whose id will map to the id that's returned by get_anonymous_user_id.

Side note, you're heading down a path of pain with

class MyView(CreateAPIView):
    # For admin LogEntry
    anonymous_user_id = get_anonymous_user_id()

If for some reason the data changes such that get_anonymous_user_id would return a different value, the server will need to restart in order to update this value as the class will be loaded into memory. The other potential problem is that if get_anonymous_user_id hits the database, it means as your class is being loaded into memory, it's running hitting the database. If that sql query were to perform poorly then it's going to take longer to deploy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django Python loaddata fails with django.db.utils.IntegrityError

Django app : unit tests fails because of django.db.utils.IntegrityError

Django makemigrations works, migrate fails with "django.db.utils.IntegrityError: NOT NULL constraint failed"

django.db.utils.IntegrityError in Custom UserModel

Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist'

Createsuperuser django.db.utils.IntegrityError: NOT NULL constraint failed

django.db.utils.IntegrityError: NOT NULL constraint failed

django.db.utils.IntegrityError while writing APITestCase for my views?

django.db.utils.IntegrityError: Stuck with this error for days

django.db.utils.IntegrityError: FOREIGN KEY constraint failed

Django tests.py django.db.utils.IntegrityError: UNIQUE constraint failed: auth_user.username

django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"

Django: django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

Django throwing error django.db.utils.IntegrityError: UNIQUE constraint failed: mediaSort_userdata.user_id

django.db.utils.IntegrityError: insert or update on table "authtoken_token" violates foreign key constraint

django.db.utils.IntegrityError: duplicate key value violates unique constraint, while 2 of 4 rows are different

django.db.utils.IntegrityError: duplicate key value violates unique constraint "spirit_category_category_pkey"

django.db.utils.IntegrityError: NOT NULL constraint failed: products_product.image ERROR WITH IMAGE FIELD

django.db.utils.IntegrityError: UNIQUE constraint failed: mode_setting.user_id

Getting django.db.utils.IntegrityError: duplicate key value violates unique constraint But value does not exists

django.db.utils.IntegrityError: UNIQUE constraint failed: auctions_bids.item_id

django.db.utils.IntegrityError: NOT NULL constraint failed: app_user.zip

django.db.utils.IntegrityError: UNIQUE constraint failed: rango_category__new.slug

django.db.utils.IntegrityError: NOT NULL constraint failed: app_users.key_value_id

django.db.utils.IntegrityError: (1062, "Duplicate entry for user_id")

django.db.utils.IntegrityError: NOT NULL constraint failed: app.area_id

django.db.utils.IntegrityError: (1048, "Column 'category_id' cannot be null")?

django.db.utils.IntegrityError: column "color_set_id" contains null values

django.db.utils.IntegrityError: NOT NULL constraint failed: polls_article.reporter_id