| 1 |
import unittest |
| 2 |
|
| 3 |
from django.db import connection |
| 4 |
from django.test import TransactionTestCase, skipUnlessDBFeature |
| 5 |
|
| 6 |
from ..models import Person, Square |
| 7 |
|
| 8 |
|
| 9 |
@unittest.skipUnless(connection.vendor == "oracle", "Oracle tests") |
| 10 |
class DatabaseSequenceTests(TransactionTestCase): |
| 11 |
available_apps = [] |
| 12 |
|
| 13 |
def test_get_sequences(self): |
| 14 |
with connection.cursor() as cursor: |
| 15 |
seqs = connection.introspection.get_sequences( |
| 16 |
cursor, Square._meta.db_table, Square._meta.local_fields |
| 17 |
) |
| 18 |
self.assertEqual(len(seqs), 1) |
| 19 |
self.assertIsNotNone(seqs[0]["name"]) |
| 20 |
self.assertEqual(seqs[0]["table"], Square._meta.db_table) |
| 21 |
self.assertEqual(seqs[0]["column"], "id") |
| 22 |
|
| 23 |
def test_get_sequences_manually_created_index(self): |
| 24 |
with connection.cursor() as cursor: |
| 25 |
with connection.schema_editor() as editor: |
| 26 |
editor._drop_identity(Square._meta.db_table, "id") |
| 27 |
seqs = connection.introspection.get_sequences( |
| 28 |
cursor, Square._meta.db_table, Square._meta.local_fields |
| 29 |
) |
| 30 |
self.assertEqual( |
| 31 |
seqs, [{"table": Square._meta.db_table, "column": "id"}] |
| 32 |
) |
| 33 |
# Recreate model, because adding identity is impossible. |
| 34 |
editor.delete_model(Square) |
| 35 |
editor.create_model(Square) |
| 36 |
|
| 37 |
@skipUnlessDBFeature("supports_collation_on_charfield") |
| 38 |
def test_get_table_description_view_default_collation(self): |
| 39 |
person_table = connection.introspection.identifier_converter( |
| 40 |
Person._meta.db_table |
| 41 |
) |
| 42 |
first_name_column = connection.ops.quote_name( |
| 43 |
Person._meta.get_field("first_name").column |
| 44 |
) |
| 45 |
person_view = connection.introspection.identifier_converter("TEST_PERSON_VIEW") |
| 46 |
with connection.cursor() as cursor: |
| 47 |
cursor.execute( |
| 48 |
f"CREATE VIEW {person_view} " |
| 49 |
f"AS SELECT {first_name_column} FROM {person_table}" |
| 50 |
) |
| 51 |
try: |
| 52 |
columns = connection.introspection.get_table_description( |
| 53 |
cursor, person_view |
| 54 |
) |
| 55 |
self.assertEqual(len(columns), 1) |
| 56 |
self.assertIsNone(columns[0].collation) |
| 57 |
finally: |
| 58 |
cursor.execute(f"DROP VIEW {person_view}") |
| 59 |
|
| 60 |
@skipUnlessDBFeature("supports_collation_on_charfield") |
| 61 |
def test_get_table_description_materialized_view_non_default_collation(self): |
| 62 |
person_table = connection.introspection.identifier_converter( |
| 63 |
Person._meta.db_table |
| 64 |
) |
| 65 |
first_name_column = connection.ops.quote_name( |
| 66 |
Person._meta.get_field("first_name").column |
| 67 |
) |
| 68 |
person_mview = connection.introspection.identifier_converter( |
| 69 |
"TEST_PERSON_MVIEW" |
| 70 |
) |
| 71 |
collation = connection.features.test_collations.get("ci") |
| 72 |
with connection.cursor() as cursor: |
| 73 |
cursor.execute( |
| 74 |
f"CREATE MATERIALIZED VIEW {person_mview} " |
| 75 |
f"DEFAULT COLLATION {collation} " |
| 76 |
f"AS SELECT {first_name_column} FROM {person_table}" |
| 77 |
) |
| 78 |
try: |
| 79 |
columns = connection.introspection.get_table_description( |
| 80 |
cursor, person_mview |
| 81 |
) |
| 82 |
self.assertEqual(len(columns), 1) |
| 83 |
self.assertIsNotNone(columns[0].collation) |
| 84 |
self.assertNotEqual(columns[0].collation, collation) |
| 85 |
finally: |
| 86 |
cursor.execute(f"DROP MATERIALIZED VIEW {person_mview}") |