| 1 |
from unittest import skipUnless |
| 2 |
|
| 3 |
from django.db import connection, connections |
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
|
| 7 |
@skipUnless(connection.vendor == "mysql", "MySQL tests") |
| 8 |
class ParsingTests(TestCase): |
| 9 |
def test_parse_constraint_columns(self): |
| 10 |
_parse_constraint_columns = connection.introspection._parse_constraint_columns |
| 11 |
tests = ( |
| 12 |
("`height` >= 0", ["height"], ["height"]), |
| 13 |
("`cost` BETWEEN 1 AND 10", ["cost"], ["cost"]), |
| 14 |
("`ref1` > `ref2`", ["id", "ref1", "ref2"], ["ref1", "ref2"]), |
| 15 |
( |
| 16 |
"`start` IS NULL OR `end` IS NULL OR `start` < `end`", |
| 17 |
["id", "start", "end"], |
| 18 |
["start", "end"], |
| 19 |
), |
| 20 |
("JSON_VALID(`json_field`)", ["json_field"], ["json_field"]), |
| 21 |
("CHAR_LENGTH(`name`) > 2", ["name"], ["name"]), |
| 22 |
("lower(`ref1`) != 'test'", ["id", "owe", "ref1"], ["ref1"]), |
| 23 |
("lower(`ref1`) != 'test'", ["id", "lower", "ref1"], ["ref1"]), |
| 24 |
("`name` LIKE 'test%'", ["name"], ["name"]), |
| 25 |
) |
| 26 |
for check_clause, table_columns, expected_columns in tests: |
| 27 |
with self.subTest(check_clause): |
| 28 |
check_columns = _parse_constraint_columns(check_clause, table_columns) |
| 29 |
self.assertEqual(list(check_columns), expected_columns) |
| 30 |
|
| 31 |
|
| 32 |
@skipUnless(connection.vendor == "mysql", "MySQL tests") |
| 33 |
class StorageEngineTests(TestCase): |
| 34 |
databases = {"default", "other"} |
| 35 |
|
| 36 |
def test_get_storage_engine(self): |
| 37 |
table_name = "test_storage_engine" |
| 38 |
create_sql = "CREATE TABLE %s (id INTEGER) ENGINE = %%s" % table_name |
| 39 |
drop_sql = "DROP TABLE %s" % table_name |
| 40 |
default_connection = connections["default"] |
| 41 |
other_connection = connections["other"] |
| 42 |
try: |
| 43 |
with default_connection.cursor() as cursor: |
| 44 |
cursor.execute(create_sql % "InnoDB") |
| 45 |
self.assertEqual( |
| 46 |
default_connection.introspection.get_storage_engine( |
| 47 |
cursor, table_name |
| 48 |
), |
| 49 |
"InnoDB", |
| 50 |
) |
| 51 |
with other_connection.cursor() as cursor: |
| 52 |
cursor.execute(create_sql % "MyISAM") |
| 53 |
self.assertEqual( |
| 54 |
other_connection.introspection.get_storage_engine( |
| 55 |
cursor, table_name |
| 56 |
), |
| 57 |
"MyISAM", |
| 58 |
) |
| 59 |
finally: |
| 60 |
with default_connection.cursor() as cursor: |
| 61 |
cursor.execute(drop_sql) |
| 62 |
with other_connection.cursor() as cursor: |
| 63 |
cursor.execute(drop_sql) |
| 64 |
|
| 65 |
|
| 66 |
@skipUnless(connection.vendor == "mysql", "MySQL specific SQL") |
| 67 |
class TestCrossDatabaseRelations(TestCase): |
| 68 |
databases = {"default", "other"} |
| 69 |
|
| 70 |
def test_omit_cross_database_relations(self): |
| 71 |
default_connection = connections["default"] |
| 72 |
other_connection = connections["other"] |
| 73 |
main_table = "cross_schema_get_relations_main_table" |
| 74 |
main_table_quoted = default_connection.ops.quote_name(main_table) |
| 75 |
other_schema_quoted = other_connection.ops.quote_name( |
| 76 |
other_connection.settings_dict["NAME"] |
| 77 |
) |
| 78 |
rel_table = "cross_schema_get_relations_rel_table" |
| 79 |
rel_table_quoted = other_connection.ops.quote_name(rel_table) |
| 80 |
rel_column = "cross_schema_get_relations_rel_table_id" |
| 81 |
rel_column_quoted = other_connection.ops.quote_name(rel_column) |
| 82 |
try: |
| 83 |
with other_connection.cursor() as other_cursor: |
| 84 |
other_cursor.execute(f""" |
| 85 |
CREATE TABLE {rel_table_quoted} ( |
| 86 |
id integer AUTO_INCREMENT, |
| 87 |
PRIMARY KEY (id) |
| 88 |
) |
| 89 |
""") |
| 90 |
with default_connection.cursor() as default_cursor: |
| 91 |
# Create table in the default schema with a cross-database |
| 92 |
# relation. |
| 93 |
default_cursor.execute(f""" |
| 94 |
CREATE TABLE {main_table_quoted} ( |
| 95 |
id integer AUTO_INCREMENT, |
| 96 |
{rel_column_quoted} integer NOT NULL, |
| 97 |
PRIMARY KEY (id), |
| 98 |
FOREIGN KEY ({rel_column_quoted}) |
| 99 |
REFERENCES {other_schema_quoted}.{rel_table_quoted}(id) |
| 100 |
) |
| 101 |
""") |
| 102 |
relations = default_connection.introspection.get_relations( |
| 103 |
default_cursor, main_table |
| 104 |
) |
| 105 |
constraints = default_connection.introspection.get_constraints( |
| 106 |
default_cursor, main_table |
| 107 |
) |
| 108 |
self.assertEqual(len(relations), 0) |
| 109 |
rel_column_fk_constraints = [ |
| 110 |
spec |
| 111 |
for name, spec in constraints.items() |
| 112 |
if spec["columns"] == [rel_column] and spec["foreign_key"] is not None |
| 113 |
] |
| 114 |
self.assertEqual(len(rel_column_fk_constraints), 0) |
| 115 |
finally: |
| 116 |
with default_connection.cursor() as default_cursor: |
| 117 |
default_cursor.execute(f"DROP TABLE IF EXISTS {main_table_quoted}") |
| 118 |
with other_connection.cursor() as other_cursor: |
| 119 |
other_cursor.execute(f"DROP TABLE IF EXISTS {rel_table_quoted}") |