Skip to content

gh-79009: sqlite3.iterdump now correctly handles tables with autoincrement #9621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup: tests
- reduce number of escape chars in string literals
- normalise variable names
- in test_dump_autoincrement_create_new_db, simplify setup
- use subTest in order to make it easier to assert more thoroughly
  • Loading branch information
erlend-aasland authored Jun 19, 2022
commit 103c50ffdcfe454a89fed09586524244cde68bdd
66 changes: 31 additions & 35 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,53 +52,49 @@ def test_table_dump(self):
for i in range(len(expected_sqls))]

def test_dump_autoincrement(self):
expected_sqls = [
"CREATE TABLE \"t1\" (id integer primary key autoincrement);",
"INSERT INTO \"t1\" VALUES(NULL);",
"CREATE TABLE \"t2\" (id integer primary key autoincrement);"
expected = [
'CREATE TABLE "t1" (id integer primary key autoincrement);',
'INSERT INTO "t1" VALUES(NULL);',
'CREATE TABLE "t2" (id integer primary key autoincrement);',
]

for sql_statement in expected_sqls:
self.cu.execute(sql_statement)
iterdump = self.cx.iterdump()
actual_sqls = [sql_statement for sql_statement in iterdump]
self.cu.executescript("".join(expected))

# the NULL value should now be automatically be set to 1
expected_sqls[1] = expected_sqls[1].replace("NULL", "1")
expected_sqls.insert(0, "BEGIN TRANSACTION;")
expected_sqls.extend([
"DELETE FROM \"sqlite_sequence\";",
"INSERT INTO \"sqlite_sequence\" VALUES('t1',1);",
"COMMIT;"
expected[1] = expected[1].replace("NULL", "1")
expected.insert(0, "BEGIN TRANSACTION;")
expected.extend([
'DELETE FROM "sqlite_sequence";',
'INSERT INTO "sqlite_sequence" VALUES(\'t1\',1);',
'COMMIT;',
])

self.assertEqual(expected_sqls, actual_sqls)
actual = [stmt for stmt in self.cx.iterdump()]
self.assertEqual(expected, actual)

def test_dump_autoincrement_create_new_db(self):
old_db = [
"BEGIN TRANSACTION ;",
"CREATE TABLE \"t1\" (id integer primary key autoincrement);",
"CREATE TABLE \"t2\" (id integer primary key autoincrement);"
]
for i in range(1, 10):
old_db.append("INSERT INTO \"t1\" VALUES(NULL);".format(i))
for i in range(1, 5):
old_db.append("INSERT INTO \"t2\" VALUES(NULL);".format(i))
old_db.append("COMMIT;")

self.cu.executescript("".join(old_db))
query = "".join(self.cx.iterdump())
self.cu.execute("BEGIN TRANSACTION")
self.cu.execute("CREATE TABLE t1 (id integer primary key autoincrement)")
self.cu.execute("CREATE TABLE t2 (id integer primary key autoincrement)")
self.cu.executemany("INSERT INTO t1 VALUES(?)", ((None,) for _ in range(9)))
self.cu.executemany("INSERT INTO t2 VALUES(?)", ((None,) for _ in range(4)))
self.cx.commit()

with memory_database() as cx2:
query = "".join(self.cx.iterdump())
cx2.executescript(query)
cu2 = cx2.cursor()

self.assertEqual(cu2.execute("SELECT \"seq\" "
"FROM \"sqlite_sequence\""
"WHERE \"name\" == 't1'").fetchall()[0][0], 9)
self.assertEqual(cu2.execute("SELECT \"seq\" "
"FROM \"sqlite_sequence\""
"WHERE \"name\" == 't2'").fetchall()[0][0], 4)
dataset = (
("t1", 9),
("t2", 4),
)
for table, seq in dataset:
with self.subTest(table=table, seq=seq):
res = cu2.execute("""
SELECT "seq" FROM "sqlite_sequence" WHERE "name" == ?
""", (table,))
rows = res.fetchall()
self.assertEqual(rows[0][0], seq)

def test_unorderable_row(self):
# iterdump() should be able to cope with unorderable row types (issue #15545)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy