Skip to content
Merged
Show file tree
Hide file tree
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
bpo-34828: reformatted some tests in test_sqlite3/test.dump.py as wel…
…l as small formatting changes in sqlite3/dump.py
  • Loading branch information
itssme committed May 22, 2022
commit daed6e50fc65ef27a64d1be6fc3440e1ec884bfb
8 changes: 4 additions & 4 deletions Lib/sqlite3/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def _iterdump(connection):
schema_res = cu.execute(q)
sqlite_sequence = []
for table_name, type, sql in schema_res.fetchall():
if table_name == 'sqlite_sequence':
sqlite_sequence = ['DELETE FROM "sqlite_sequence"']
rows = cu.execute('SELECT * FROM "sqlite_sequence";').fetchall()
sqlite_sequence.extend(['INSERT INTO "sqlite_sequence" VALUES(\'{0}\',{1})'
if table_name == "sqlite_sequence":
sqlite_sequence = ["DELETE FROM \"sqlite_sequence\""]
rows = cu.execute("SELECT * FROM \"sqlite_sequence\";").fetchall()
sqlite_sequence.extend(["INSERT INTO \"sqlite_sequence\" VALUES('{0}',{1})"
.format(row[0], row[1]) for row in rows])
continue
elif table_name == 'sqlite_stat1':
Expand Down
79 changes: 32 additions & 47 deletions Lib/test/test_sqlite3/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import unittest
import sqlite3 as sqlite
from .test_dbapi import memory_database


class DumpTests(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -51,17 +53,11 @@ def test_table_dump(self):

def test_dump_autoincrement(self):
expected_sqls = [
"""CREATE TABLE "posts" (id int primary key);""",
"""INSERT INTO "posts" VALUES(0);""",
"""CREATE TABLE "tags" ( """
"""id integer primary key autoincrement,"""
"""tag varchar(256),"""
"""post int references posts);""",
"""INSERT INTO "tags" VALUES(NULL,'test',0);""",
"""CREATE TABLE "tags2" ( """
"""id integer primary key autoincrement,"""
"""tag varchar(256),"""
"""post int references posts);"""
"CREATE TABLE \"posts\" (id int primary key);",
"INSERT INTO \"posts\" VALUES(0);",
"CREATE TABLE \"tags\" (id integer primary key autoincrement, tag varchar(256), post int references posts);",
"INSERT INTO \"tags\" VALUES(NULL,'test',0);",
"CREATE TABLE \"tags2\" (id integer primary key autoincrement, tag varchar(256), post int references posts);"
]

for sql_statement in expected_sqls:
Expand All @@ -71,53 +67,42 @@ def test_dump_autoincrement(self):

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

for i in range(len(expected_sqls)):
self.assertEqual(expected_sqls[i], actual_sqls[i])
self.assertEqual(expected_sqls, actual_sqls)

def test_dump_autoincrement_create_new_db(self):
old_db = [
"""BEGIN TRANSACTION ;""",
"""CREATE TABLE "posts" (id int primary key);""",
"""INSERT INTO "posts" VALUES(0);""",
"""CREATE TABLE "tags" ( """
"""id integer primary key autoincrement,"""
"""tag varchar(256),"""
"""post int references posts);""",
"""CREATE TABLE "tags2" ( """
"""id integer primary key autoincrement,"""
"""tag varchar(256),"""
"""post int references posts);"""
"BEGIN TRANSACTION ;",
"CREATE TABLE \"posts\" (id int primary key);",
"INSERT INTO \"posts\" VALUES(0);",
"CREATE TABLE \"tags\" (id integer primary key autoincrement, tag varchar(256), post int references posts);",
"CREATE TABLE \"tags2\" (id integer primary key autoincrement, tag varchar(256), post int references posts);"
]
for i in range(1, 10):
old_db.append("""INSERT INTO "tags"
VALUES(NULL,'test{0}',0);""".format(i))
old_db.append("INSERT INTO \"tags\" VALUES(NULL,'test{0}',0);".format(i))
for i in range(1, 5):
old_db.append("""INSERT INTO "tags2"
VALUES(NULL,'test{0}',0);""".format(i))
old_db.append("INSERT INTO \"tags2\" VALUES(NULL,'test{0}',0);".format(i))
old_db.append("COMMIT;")

for sql_statement in old_db:
self.cu.execute(sql_statement)

cx2 = sqlite.connect(":memory:")
query = "".join(line for line in self.cx.iterdump())
cx2.executescript(query)
cu2 = cx2.cursor()
self.cu.executescript("".join(old_db))
query = "".join(self.cx.iterdump())

self.assertEqual(cu2.execute('SELECT "seq"'
'FROM "sqlite_sequence"'
'WHERE "name" == "tags"').fetchall()[0][0], 9)
self.assertEqual(cu2.execute('SELECT "seq" FROM'
'"sqlite_sequence"'
'WHERE "name" == "tags2"').fetchall()[0][0], 4)
with memory_database() as cx2:
cx2.executescript(query)
cu2 = cx2.cursor()

cu2.close()
cx2.close()
self.assertEqual(cu2.execute("SELECT \"seq\""
"FROM \"sqlite_sequence\""
"WHERE \"name\" == \"tags\"").fetchall()[0][0], 9)
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
self.assertEqual(cu2.execute("SELECT \"seq\" FROM"
"\"sqlite_sequence\""
"WHERE \"name\" == \"tags2\"").fetchall()[0][0], 4)

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