dump.py 2.8 KB

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