diff options
Diffstat (limited to 'tests/backends/sqlite/test_introspection.py')
| -rw-r--r-- | tests/backends/sqlite/test_introspection.py | 125 |
1 files changed, 83 insertions, 42 deletions
diff --git a/tests/backends/sqlite/test_introspection.py b/tests/backends/sqlite/test_introspection.py index 9331b5bb1a..2997ac9595 100644 --- a/tests/backends/sqlite/test_introspection.py +++ b/tests/backends/sqlite/test_introspection.py @@ -6,7 +6,7 @@ from django.db import connection from django.test import TestCase -@unittest.skipUnless(connection.vendor == 'sqlite', 'SQLite tests') +@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") class IntrospectionTests(TestCase): def test_get_primary_key_column(self): """ @@ -14,19 +14,26 @@ class IntrospectionTests(TestCase): quotation. """ testable_column_strings = ( - ('id', 'id'), ('[id]', 'id'), ('`id`', 'id'), ('"id"', 'id'), - ('[id col]', 'id col'), ('`id col`', 'id col'), ('"id col"', 'id col') + ("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 + 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') + field = connection.introspection.get_primary_key_column( + cursor, "test_primary" + ) self.assertEqual(field, expected_string) finally: - cursor.execute('DROP TABLE test_primary') + cursor.execute("DROP TABLE test_primary") def test_get_primary_key_column_pk_constraint(self): sql = """ @@ -41,38 +48,43 @@ class IntrospectionTests(TestCase): cursor.execute(sql) field = connection.introspection.get_primary_key_column( cursor, - 'test_primary', + "test_primary", ) - self.assertEqual(field, 'id') + self.assertEqual(field, "id") finally: - cursor.execute('DROP TABLE test_primary') + cursor.execute("DROP TABLE test_primary") -@unittest.skipUnless(connection.vendor == 'sqlite', 'SQLite tests') +@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)) + 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, - }) + 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']), + ('"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): @@ -83,10 +95,18 @@ class ParsingTests(TestCase): 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']), + ('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): @@ -97,8 +117,12 @@ class ParsingTests(TestCase): def test_unique_constraint_multicolumn(self): tests = ( - ('CONSTRAINT "ref" UNIQUE ("ref", "customname"),', 'ref', ['ref', 'customname']), - ('CONSTRAINT ref UNIQUE (ref, customname),', 'ref', ['ref', 'customname']), + ( + '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): @@ -109,10 +133,16 @@ class ParsingTests(TestCase): 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']), + ('"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): @@ -123,10 +153,18 @@ class ParsingTests(TestCase): 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']), + ('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): @@ -137,9 +175,12 @@ class ParsingTests(TestCase): 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']), + ('"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): @@ -150,8 +191,8 @@ class ParsingTests(TestCase): def test_check_and_unique_column(self): tests = ( - ('"ref" varchar(255) CHECK ("ref" != \'test\') UNIQUE,', ['ref']), - ('ref varchar(255) UNIQUE CHECK (ref != \'test\'),', ['ref']), + ('"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): |
