2017-09-13 18:12:32 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from ..models import Person
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "Test only for PostgreSQL")
|
|
|
|
class DatabaseSequenceTests(TestCase):
|
|
|
|
def test_get_sequences(self):
|
2017-11-28 13:12:28 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
seqs = connection.introspection.get_sequences(cursor, Person._meta.db_table)
|
|
|
|
self.assertEqual(
|
|
|
|
seqs,
|
|
|
|
[
|
2022-02-03 19:24:19 +00:00
|
|
|
{
|
2017-11-28 13:12:28 +00:00
|
|
|
"table": Person._meta.db_table,
|
|
|
|
"column": "id",
|
|
|
|
"name": "backends_person_id_seq",
|
2022-02-03 19:24:19 +00:00
|
|
|
}
|
|
|
|
],
|
2017-11-28 13:12:28 +00:00
|
|
|
)
|
|
|
|
cursor.execute("ALTER SEQUENCE backends_person_id_seq RENAME TO pers_seq")
|
|
|
|
seqs = connection.introspection.get_sequences(cursor, Person._meta.db_table)
|
|
|
|
self.assertEqual(
|
|
|
|
seqs,
|
|
|
|
[{"table": Person._meta.db_table, "column": "id", "name": "pers_seq"}],
|
|
|
|
)
|
2022-03-24 15:46:19 +00:00
|
|
|
|
|
|
|
def test_get_sequences_old_serial(self):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("CREATE TABLE testing (serial_field SERIAL);")
|
|
|
|
seqs = connection.introspection.get_sequences(cursor, "testing")
|
|
|
|
self.assertEqual(
|
|
|
|
seqs,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"table": "testing",
|
|
|
|
"column": "serial_field",
|
|
|
|
"name": "testing_serial_field_seq",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|