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)