2019-07-30 21:18:12 +08:00
|
|
|
from unittest import skipUnless
|
|
|
|
|
2021-08-12 15:35:46 +08:00
|
|
|
from django.db import connection, connections
|
2019-07-30 21:18:12 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(connection.vendor == "mysql", "MySQL tests")
|
|
|
|
class ParsingTests(TestCase):
|
|
|
|
def test_parse_constraint_columns(self):
|
2019-07-30 22:21:05 +08:00
|
|
|
_parse_constraint_columns = connection.introspection._parse_constraint_columns
|
2019-07-30 21:18:12 +08:00
|
|
|
tests = (
|
2019-07-30 22:21:05 +08:00
|
|
|
("`height` >= 0", ["height"], ["height"]),
|
|
|
|
("`cost` BETWEEN 1 AND 10", ["cost"], ["cost"]),
|
|
|
|
("`ref1` > `ref2`", ["id", "ref1", "ref2"], ["ref1", "ref2"]),
|
2019-07-30 21:18:12 +08:00
|
|
|
(
|
|
|
|
"`start` IS NULL OR `end` IS NULL OR `start` < `end`",
|
2019-07-30 22:21:05 +08:00
|
|
|
["id", "start", "end"],
|
2019-07-30 21:18:12 +08:00
|
|
|
["start", "end"],
|
|
|
|
),
|
2019-07-30 22:21:05 +08:00
|
|
|
("JSON_VALID(`json_field`)", ["json_field"], ["json_field"]),
|
|
|
|
("CHAR_LENGTH(`name`) > 2", ["name"], ["name"]),
|
|
|
|
("lower(`ref1`) != 'test'", ["id", "owe", "ref1"], ["ref1"]),
|
|
|
|
("lower(`ref1`) != 'test'", ["id", "lower", "ref1"], ["ref1"]),
|
|
|
|
("`name` LIKE 'test%'", ["name"], ["name"]),
|
2019-07-30 21:18:12 +08:00
|
|
|
)
|
2019-07-30 22:21:05 +08:00
|
|
|
for check_clause, table_columns, expected_columns in tests:
|
2019-07-30 21:18:12 +08:00
|
|
|
with self.subTest(check_clause):
|
2019-07-30 22:21:05 +08:00
|
|
|
check_columns = _parse_constraint_columns(check_clause, table_columns)
|
2019-07-30 21:18:12 +08:00
|
|
|
self.assertEqual(list(check_columns), expected_columns)
|
2021-08-12 15:35:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnless(connection.vendor == "mysql", "MySQL tests")
|
|
|
|
class StorageEngineTests(TestCase):
|
|
|
|
databases = {"default", "other"}
|
|
|
|
|
|
|
|
def test_get_storage_engine(self):
|
|
|
|
table_name = "test_storage_engine"
|
|
|
|
create_sql = "CREATE TABLE %s (id INTEGER) ENGINE = %%s" % table_name
|
|
|
|
drop_sql = "DROP TABLE %s" % table_name
|
|
|
|
default_connection = connections["default"]
|
|
|
|
other_connection = connections["other"]
|
|
|
|
try:
|
|
|
|
with default_connection.cursor() as cursor:
|
|
|
|
cursor.execute(create_sql % "InnoDB")
|
|
|
|
self.assertEqual(
|
|
|
|
default_connection.introspection.get_storage_engine(
|
|
|
|
cursor, table_name
|
|
|
|
),
|
|
|
|
"InnoDB",
|
|
|
|
)
|
|
|
|
with other_connection.cursor() as cursor:
|
|
|
|
cursor.execute(create_sql % "MyISAM")
|
|
|
|
self.assertEqual(
|
|
|
|
other_connection.introspection.get_storage_engine(
|
|
|
|
cursor, table_name
|
|
|
|
),
|
|
|
|
"MyISAM",
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
with default_connection.cursor() as cursor:
|
|
|
|
cursor.execute(drop_sql)
|
|
|
|
with other_connection.cursor() as cursor:
|
|
|
|
cursor.execute(drop_sql)
|