Debian Codegraph @

tests/backends/oracle/test_operations.py

raw
1 import unittest
2
3 from django.core.management.color import no_style
4 from django.db import connection, models
5 from django.test import TransactionTestCase
6
7 from ..models import Person, Tag
8
9
10 @unittest.skipUnless(connection.vendor == "oracle", "Oracle tests")
11 class OperationsTests(TransactionTestCase):
12 available_apps = ["backends"]
13
14 def test_sequence_name_truncation(self):
15 seq_name = connection.ops._get_no_autofield_sequence_name(
16 "schema_authorwithevenlongee869"
17 )
18 self.assertEqual(seq_name, "SCHEMA_AUTHORWITHEVENLOB0B8_SQ")
19
20 def test_bulk_batch_size(self):
21 # Oracle restricts the number of parameters in a query.
22 objects = range(2**16)
23 self.assertEqual(connection.ops.bulk_batch_size([], objects), len(objects))
24 # Each field is a parameter for each object.
25 first_name_field = Person._meta.get_field("first_name")
26 last_name_field = Person._meta.get_field("last_name")
27 self.assertEqual(
28 connection.ops.bulk_batch_size([first_name_field], objects),
29 connection.features.max_query_params,
30 )
31 self.assertEqual(
32 connection.ops.bulk_batch_size(
33 [first_name_field, last_name_field],
34 objects,
35 ),
36 connection.features.max_query_params // 2,
37 )
38 composite_pk = models.CompositePrimaryKey("first_name", "last_name")
39 composite_pk.fields = [first_name_field, last_name_field]
40 self.assertEqual(
41 connection.ops.bulk_batch_size([composite_pk, first_name_field], objects),
42 connection.features.max_query_params // 3,
43 )
44
45 def test_sql_flush(self):
46 statements = connection.ops.sql_flush(
47 no_style(),
48 [Person._meta.db_table, Tag._meta.db_table],
49 )
50 # The tables and constraints are processed in an unordered set.
51 self.assertEqual(
52 statements[0],
53 'ALTER TABLE "BACKENDS_TAG" DISABLE CONSTRAINT '
54 '"BACKENDS__CONTENT_T_FD9D7A85_F" KEEP INDEX;',
55 )
56 self.assertEqual(
57 sorted(statements[1:-1]),
58 [
59 'TRUNCATE TABLE "BACKENDS_PERSON";',
60 'TRUNCATE TABLE "BACKENDS_TAG";',
61 ],
62 )
63 self.assertEqual(
64 statements[-1],
65 'ALTER TABLE "BACKENDS_TAG" ENABLE CONSTRAINT '
66 '"BACKENDS__CONTENT_T_FD9D7A85_F";',
67 )
68
69 def test_sql_flush_allow_cascade(self):
70 statements = connection.ops.sql_flush(
71 no_style(),
72 [Person._meta.db_table, Tag._meta.db_table],
73 allow_cascade=True,
74 )
75 # The tables and constraints are processed in an unordered set.
76 self.assertEqual(
77 statements[0],
78 'ALTER TABLE "BACKENDS_VERYLONGMODELNAME540F" DISABLE CONSTRAINT '
79 '"BACKENDS__PERSON_ID_1DD5E829_F" KEEP INDEX;',
80 )
81 self.assertEqual(
82 sorted(statements[1:-1]),
83 [
84 'TRUNCATE TABLE "BACKENDS_PERSON";',
85 'TRUNCATE TABLE "BACKENDS_TAG";',
86 'TRUNCATE TABLE "BACKENDS_VERYLONGMODELNAME540F";',
87 ],
88 )
89 self.assertEqual(
90 statements[-1],
91 'ALTER TABLE "BACKENDS_VERYLONGMODELNAME540F" ENABLE CONSTRAINT '
92 '"BACKENDS__PERSON_ID_1DD5E829_F";',
93 )
94
95 def test_sql_flush_sequences(self):
96 statements = connection.ops.sql_flush(
97 no_style(),
98 [Person._meta.db_table, Tag._meta.db_table],
99 reset_sequences=True,
100 )
101 # The tables and constraints are processed in an unordered set.
102 self.assertEqual(
103 statements[0],
104 'ALTER TABLE "BACKENDS_TAG" DISABLE CONSTRAINT '
105 '"BACKENDS__CONTENT_T_FD9D7A85_F" KEEP INDEX;',
106 )
107 self.assertEqual(
108 sorted(statements[1:3]),
109 [
110 'TRUNCATE TABLE "BACKENDS_PERSON";',
111 'TRUNCATE TABLE "BACKENDS_TAG";',
112 ],
113 )
114 self.assertEqual(
115 statements[3],
116 'ALTER TABLE "BACKENDS_TAG" ENABLE CONSTRAINT '
117 '"BACKENDS__CONTENT_T_FD9D7A85_F";',
118 )
119 # Sequences.
120 self.assertEqual(len(statements[4:]), 2)
121 self.assertIn("BACKENDS_PERSON_SQ", statements[4])
122 self.assertIn("BACKENDS_TAG_SQ", statements[5])
123
124 def test_sql_flush_sequences_allow_cascade(self):
125 statements = connection.ops.sql_flush(
126 no_style(),
127 [Person._meta.db_table, Tag._meta.db_table],
128 reset_sequences=True,
129 allow_cascade=True,
130 )
131 # The tables and constraints are processed in an unordered set.
132 self.assertEqual(
133 statements[0],
134 'ALTER TABLE "BACKENDS_VERYLONGMODELNAME540F" DISABLE CONSTRAINT '
135 '"BACKENDS__PERSON_ID_1DD5E829_F" KEEP INDEX;',
136 )
137 self.assertEqual(
138 sorted(statements[1:4]),
139 [
140 'TRUNCATE TABLE "BACKENDS_PERSON";',
141 'TRUNCATE TABLE "BACKENDS_TAG";',
142 'TRUNCATE TABLE "BACKENDS_VERYLONGMODELNAME540F";',
143 ],
144 )
145 self.assertEqual(
146 statements[4],
147 'ALTER TABLE "BACKENDS_VERYLONGMODELNAME540F" ENABLE CONSTRAINT '
148 '"BACKENDS__PERSON_ID_1DD5E829_F";',
149 )
150 # Sequences.
151 self.assertEqual(len(statements[5:]), 3)
152 self.assertIn("BACKENDS_PERSON_SQ", statements[5])
153 self.assertIn("BACKENDS_VERYLONGMODELN7BE2_SQ", statements[6])
154 self.assertIn("BACKENDS_TAG_SQ", statements[7])