1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #373 -- Simplified DatabaseIntrospection.get_constraints() tests for composite primary keys.

This commit is contained in:
Mariusz Felisiak 2024-12-01 20:55:35 +01:00 committed by Sarah Boyce
parent 81cf690111
commit 49761ac99a

View File

@ -1,5 +1,4 @@
import json import json
import unittest
from uuid import UUID from uuid import UUID
import yaml import yaml
@ -35,9 +34,9 @@ class CompositePKTests(TestCase):
cls.comment = Comment.objects.create(tenant=cls.tenant, id=1, user=cls.user) cls.comment = Comment.objects.create(tenant=cls.tenant, id=1, user=cls.user)
@staticmethod @staticmethod
def get_constraints(table): def get_primary_key_columns(table):
with connection.cursor() as cursor: with connection.cursor() as cursor:
return connection.introspection.get_constraints(cursor, table) return connection.introspection.get_primary_key_columns(cursor, table)
def test_pk_updated_if_field_updated(self): def test_pk_updated_if_field_updated(self):
user = User.objects.get(pk=self.user.pk) user = User.objects.get(pk=self.user.pk)
@ -125,53 +124,15 @@ class CompositePKTests(TestCase):
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
Comment.objects.create(tenant=self.tenant, id=self.comment.id) Comment.objects.create(tenant=self.tenant, id=self.comment.id)
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific test") def test_get_primary_key_columns(self):
def test_get_constraints_postgresql(self): self.assertEqual(
user_constraints = self.get_constraints(User._meta.db_table) self.get_primary_key_columns(User._meta.db_table),
user_pk = user_constraints["composite_pk_user_pkey"] ["tenant_id", "id"],
self.assertEqual(user_pk["columns"], ["tenant_id", "id"]) )
self.assertIs(user_pk["primary_key"], True) self.assertEqual(
self.get_primary_key_columns(Comment._meta.db_table),
comment_constraints = self.get_constraints(Comment._meta.db_table) ["tenant_id", "comment_id"],
comment_pk = comment_constraints["composite_pk_comment_pkey"] )
self.assertEqual(comment_pk["columns"], ["tenant_id", "comment_id"])
self.assertIs(comment_pk["primary_key"], True)
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite specific test")
def test_get_constraints_sqlite(self):
user_constraints = self.get_constraints(User._meta.db_table)
user_pk = user_constraints["__primary__"]
self.assertEqual(user_pk["columns"], ["tenant_id", "id"])
self.assertIs(user_pk["primary_key"], True)
comment_constraints = self.get_constraints(Comment._meta.db_table)
comment_pk = comment_constraints["__primary__"]
self.assertEqual(comment_pk["columns"], ["tenant_id", "comment_id"])
self.assertIs(comment_pk["primary_key"], True)
@unittest.skipUnless(connection.vendor == "mysql", "MySQL specific test")
def test_get_constraints_mysql(self):
user_constraints = self.get_constraints(User._meta.db_table)
user_pk = user_constraints["PRIMARY"]
self.assertEqual(user_pk["columns"], ["tenant_id", "id"])
self.assertIs(user_pk["primary_key"], True)
comment_constraints = self.get_constraints(Comment._meta.db_table)
comment_pk = comment_constraints["PRIMARY"]
self.assertEqual(comment_pk["columns"], ["tenant_id", "comment_id"])
self.assertIs(comment_pk["primary_key"], True)
@unittest.skipUnless(connection.vendor == "oracle", "Oracle specific test")
def test_get_constraints_oracle(self):
user_constraints = self.get_constraints(User._meta.db_table)
user_pk = next(c for c in user_constraints.values() if c["primary_key"])
self.assertEqual(user_pk["columns"], ["tenant_id", "id"])
self.assertEqual(user_pk["primary_key"], 1)
comment_constraints = self.get_constraints(Comment._meta.db_table)
comment_pk = next(c for c in comment_constraints.values() if c["primary_key"])
self.assertEqual(comment_pk["columns"], ["tenant_id", "comment_id"])
self.assertEqual(comment_pk["primary_key"], 1)
def test_in_bulk(self): def test_in_bulk(self):
""" """