1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
import unittest
import sqlparse
from django.db import connection
from django.test import TestCase
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests")
class IntrospectionTests(TestCase):
def test_get_primary_key_column(self):
"""
Get the primary key column regardless of whether or not it has
quotation.
"""
testable_column_strings = (
("id", "id"),
("[id]", "id"),
("`id`", "id"),
('"id"', "id"),
("[id col]", "id col"),
("`id col`", "id col"),
('"id col"', "id col"),
)
with connection.cursor() as cursor:
for column, expected_string in testable_column_strings:
sql = "CREATE TABLE test_primary (%s int PRIMARY KEY NOT NULL)" % column
with self.subTest(column=column):
try:
cursor.execute(sql)
field = connection.introspection.get_primary_key_column(
cursor, "test_primary"
)
self.assertEqual(field, expected_string)
finally:
cursor.execute("DROP TABLE test_primary")
def test_get_primary_key_column_pk_constraint(self):
sql = """
CREATE TABLE test_primary(
id INTEGER NOT NULL,
created DATE,
PRIMARY KEY(id)
)
"""
with connection.cursor() as cursor:
try:
cursor.execute(sql)
field = connection.introspection.get_primary_key_column(
cursor,
"test_primary",
)
self.assertEqual(field, "id")
finally:
cursor.execute("DROP TABLE test_primary")
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests")
class ParsingTests(TestCase):
def parse_definition(self, sql, columns):
"""Parse a column or constraint definition."""
statement = sqlparse.parse(sql)[0]
tokens = (token for token in statement.flatten() if not token.is_whitespace)
with connection.cursor():
return connection.introspection._parse_column_or_constraint_definition(
tokens, set(columns)
)
def assertConstraint(self, constraint_details, cols, unique=False, check=False):
self.assertEqual(
constraint_details,
{
"unique": unique,
"columns": cols,
"primary_key": False,
"foreign_key": None,
"check": check,
"index": False,
},
)
def test_unique_column(self):
tests = (
('"ref" integer UNIQUE,', ["ref"]),
("ref integer UNIQUE,", ["ref"]),
('"customname" integer UNIQUE,', ["customname"]),
("customname integer UNIQUE,", ["customname"]),
)
for sql, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertIsNone(constraint)
self.assertConstraint(details, columns, unique=True)
self.assertIsNone(check)
def test_unique_constraint(self):
tests = (
('CONSTRAINT "ref" UNIQUE ("ref"),', "ref", ["ref"]),
("CONSTRAINT ref UNIQUE (ref),", "ref", ["ref"]),
(
'CONSTRAINT "customname1" UNIQUE ("customname2"),',
"customname1",
["customname2"],
),
(
"CONSTRAINT customname1 UNIQUE (customname2),",
"customname1",
["customname2"],
),
)
for sql, constraint_name, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertEqual(constraint, constraint_name)
self.assertConstraint(details, columns, unique=True)
self.assertIsNone(check)
def test_unique_constraint_multicolumn(self):
tests = (
(
'CONSTRAINT "ref" UNIQUE ("ref", "customname"),',
"ref",
["ref", "customname"],
),
("CONSTRAINT ref UNIQUE (ref, customname),", "ref", ["ref", "customname"]),
)
for sql, constraint_name, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertEqual(constraint, constraint_name)
self.assertConstraint(details, columns, unique=True)
self.assertIsNone(check)
def test_check_column(self):
tests = (
('"ref" varchar(255) CHECK ("ref" != \'test\'),', ["ref"]),
("ref varchar(255) CHECK (ref != 'test'),", ["ref"]),
(
'"customname1" varchar(255) CHECK ("customname2" != \'test\'),',
["customname2"],
),
(
"customname1 varchar(255) CHECK (customname2 != 'test'),",
["customname2"],
),
)
for sql, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertIsNone(constraint)
self.assertIsNone(details)
self.assertConstraint(check, columns, check=True)
def test_check_constraint(self):
tests = (
('CONSTRAINT "ref" CHECK ("ref" != \'test\'),', "ref", ["ref"]),
("CONSTRAINT ref CHECK (ref != 'test'),", "ref", ["ref"]),
(
'CONSTRAINT "customname1" CHECK ("customname2" != \'test\'),',
"customname1",
["customname2"],
),
(
"CONSTRAINT customname1 CHECK (customname2 != 'test'),",
"customname1",
["customname2"],
),
)
for sql, constraint_name, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertEqual(constraint, constraint_name)
self.assertIsNone(details)
self.assertConstraint(check, columns, check=True)
def test_check_column_with_operators_and_functions(self):
tests = (
('"ref" integer CHECK ("ref" BETWEEN 1 AND 10),', ["ref"]),
('"ref" varchar(255) CHECK ("ref" LIKE \'test%\'),', ["ref"]),
(
'"ref" varchar(255) CHECK (LENGTH(ref) > "max_length"),',
["ref", "max_length"],
),
)
for sql, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertIsNone(constraint)
self.assertIsNone(details)
self.assertConstraint(check, columns, check=True)
def test_check_and_unique_column(self):
tests = (
('"ref" varchar(255) CHECK ("ref" != \'test\') UNIQUE,', ["ref"]),
("ref varchar(255) UNIQUE CHECK (ref != 'test'),", ["ref"]),
)
for sql, columns in tests:
with self.subTest(sql=sql):
constraint, details, check, _ = self.parse_definition(sql, columns)
self.assertIsNone(constraint)
self.assertConstraint(details, columns, unique=True)
self.assertConstraint(check, columns, check=True)
|