1 | n/a | # Author: Paul Kippes <kippesp@gmail.com> |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import sqlite3 as sqlite |
---|
5 | n/a | |
---|
6 | n/a | class DumpTests(unittest.TestCase): |
---|
7 | n/a | def setUp(self): |
---|
8 | n/a | self.cx = sqlite.connect(":memory:") |
---|
9 | n/a | self.cu = self.cx.cursor() |
---|
10 | n/a | |
---|
11 | n/a | def tearDown(self): |
---|
12 | n/a | self.cx.close() |
---|
13 | n/a | |
---|
14 | n/a | def CheckTableDump(self): |
---|
15 | n/a | expected_sqls = [ |
---|
16 | n/a | """CREATE TABLE "index"("index" blob);""" |
---|
17 | n/a | , |
---|
18 | n/a | """INSERT INTO "index" VALUES(X'01');""" |
---|
19 | n/a | , |
---|
20 | n/a | """CREATE TABLE "quoted""table"("quoted""field" text);""" |
---|
21 | n/a | , |
---|
22 | n/a | """INSERT INTO "quoted""table" VALUES('quoted''value');""" |
---|
23 | n/a | , |
---|
24 | n/a | "CREATE TABLE t1(id integer primary key, s1 text, " \ |
---|
25 | n/a | "t1_i1 integer not null, i2 integer, unique (s1), " \ |
---|
26 | n/a | "constraint t1_idx1 unique (i2));" |
---|
27 | n/a | , |
---|
28 | n/a | "INSERT INTO \"t1\" VALUES(1,'foo',10,20);" |
---|
29 | n/a | , |
---|
30 | n/a | "INSERT INTO \"t1\" VALUES(2,'foo2',30,30);" |
---|
31 | n/a | , |
---|
32 | n/a | "CREATE TABLE t2(id integer, t2_i1 integer, " \ |
---|
33 | n/a | "t2_i2 integer, primary key (id)," \ |
---|
34 | n/a | "foreign key(t2_i1) references t1(t1_i1));" |
---|
35 | n/a | , |
---|
36 | n/a | "CREATE TRIGGER trigger_1 update of t1_i1 on t1 " \ |
---|
37 | n/a | "begin " \ |
---|
38 | n/a | "update t2 set t2_i1 = new.t1_i1 where t2_i1 = old.t1_i1; " \ |
---|
39 | n/a | "end;" |
---|
40 | n/a | , |
---|
41 | n/a | "CREATE VIEW v1 as select * from t1 left join t2 " \ |
---|
42 | n/a | "using (id);" |
---|
43 | n/a | ] |
---|
44 | n/a | [self.cu.execute(s) for s in expected_sqls] |
---|
45 | n/a | i = self.cx.iterdump() |
---|
46 | n/a | actual_sqls = [s for s in i] |
---|
47 | n/a | expected_sqls = ['BEGIN TRANSACTION;'] + expected_sqls + \ |
---|
48 | n/a | ['COMMIT;'] |
---|
49 | n/a | [self.assertEqual(expected_sqls[i], actual_sqls[i]) |
---|
50 | n/a | for i in range(len(expected_sqls))] |
---|
51 | n/a | |
---|
52 | n/a | def CheckUnorderableRow(self): |
---|
53 | n/a | # iterdump() should be able to cope with unorderable row types (issue #15545) |
---|
54 | n/a | class UnorderableRow: |
---|
55 | n/a | def __init__(self, cursor, row): |
---|
56 | n/a | self.row = row |
---|
57 | n/a | def __getitem__(self, index): |
---|
58 | n/a | return self.row[index] |
---|
59 | n/a | self.cx.row_factory = UnorderableRow |
---|
60 | n/a | CREATE_ALPHA = """CREATE TABLE "alpha" ("one");""" |
---|
61 | n/a | CREATE_BETA = """CREATE TABLE "beta" ("two");""" |
---|
62 | n/a | expected = [ |
---|
63 | n/a | "BEGIN TRANSACTION;", |
---|
64 | n/a | CREATE_ALPHA, |
---|
65 | n/a | CREATE_BETA, |
---|
66 | n/a | "COMMIT;" |
---|
67 | n/a | ] |
---|
68 | n/a | self.cu.execute(CREATE_BETA) |
---|
69 | n/a | self.cu.execute(CREATE_ALPHA) |
---|
70 | n/a | got = list(self.cx.iterdump()) |
---|
71 | n/a | self.assertEqual(expected, got) |
---|
72 | n/a | |
---|
73 | n/a | def suite(): |
---|
74 | n/a | return unittest.TestSuite(unittest.makeSuite(DumpTests, "Check")) |
---|
75 | n/a | |
---|
76 | n/a | def test(): |
---|
77 | n/a | runner = unittest.TextTestRunner() |
---|
78 | n/a | runner.run(suite()) |
---|
79 | n/a | |
---|
80 | n/a | if __name__ == "__main__": |
---|
81 | n/a | test() |
---|