PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/sqlite3/test/dbapi.py

https://bitbucket.org/mirror/python-trunk/
Python | 781 lines | 721 code | 31 blank | 29 comment | 7 complexity | 5975ea32ed136c74f2adaf56886d4939 MD5 | raw file
Possible License(s): BSD-3-Clause, 0BSD
  1. #-*- coding: ISO-8859-1 -*-
  2. # pysqlite2/test/dbapi.py: tests for DB-API compliance
  3. #
  4. # Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
  5. #
  6. # This file is part of pysqlite.
  7. #
  8. # This software is provided 'as-is', without any express or implied
  9. # warranty. In no event will the authors be held liable for any damages
  10. # arising from the use of this software.
  11. #
  12. # Permission is granted to anyone to use this software for any purpose,
  13. # including commercial applications, and to alter it and redistribute it
  14. # freely, subject to the following restrictions:
  15. #
  16. # 1. The origin of this software must not be misrepresented; you must not
  17. # claim that you wrote the original software. If you use this software
  18. # in a product, an acknowledgment in the product documentation would be
  19. # appreciated but is not required.
  20. # 2. Altered source versions must be plainly marked as such, and must not be
  21. # misrepresented as being the original software.
  22. # 3. This notice may not be removed or altered from any source distribution.
  23. import unittest
  24. import sys
  25. import threading
  26. import sqlite3 as sqlite
  27. class ModuleTests(unittest.TestCase):
  28. def CheckAPILevel(self):
  29. self.assertEqual(sqlite.apilevel, "2.0",
  30. "apilevel is %s, should be 2.0" % sqlite.apilevel)
  31. def CheckThreadSafety(self):
  32. self.assertEqual(sqlite.threadsafety, 1,
  33. "threadsafety is %d, should be 1" % sqlite.threadsafety)
  34. def CheckParamStyle(self):
  35. self.assertEqual(sqlite.paramstyle, "qmark",
  36. "paramstyle is '%s', should be 'qmark'" %
  37. sqlite.paramstyle)
  38. def CheckWarning(self):
  39. self.assert_(issubclass(sqlite.Warning, StandardError),
  40. "Warning is not a subclass of StandardError")
  41. def CheckError(self):
  42. self.assertTrue(issubclass(sqlite.Error, StandardError),
  43. "Error is not a subclass of StandardError")
  44. def CheckInterfaceError(self):
  45. self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error),
  46. "InterfaceError is not a subclass of Error")
  47. def CheckDatabaseError(self):
  48. self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error),
  49. "DatabaseError is not a subclass of Error")
  50. def CheckDataError(self):
  51. self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError),
  52. "DataError is not a subclass of DatabaseError")
  53. def CheckOperationalError(self):
  54. self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError),
  55. "OperationalError is not a subclass of DatabaseError")
  56. def CheckIntegrityError(self):
  57. self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError),
  58. "IntegrityError is not a subclass of DatabaseError")
  59. def CheckInternalError(self):
  60. self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError),
  61. "InternalError is not a subclass of DatabaseError")
  62. def CheckProgrammingError(self):
  63. self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError),
  64. "ProgrammingError is not a subclass of DatabaseError")
  65. def CheckNotSupportedError(self):
  66. self.assertTrue(issubclass(sqlite.NotSupportedError,
  67. sqlite.DatabaseError),
  68. "NotSupportedError is not a subclass of DatabaseError")
  69. class ConnectionTests(unittest.TestCase):
  70. def setUp(self):
  71. self.cx = sqlite.connect(":memory:")
  72. cu = self.cx.cursor()
  73. cu.execute("create table test(id integer primary key, name text)")
  74. cu.execute("insert into test(name) values (?)", ("foo",))
  75. def tearDown(self):
  76. self.cx.close()
  77. def CheckCommit(self):
  78. self.cx.commit()
  79. def CheckCommitAfterNoChanges(self):
  80. """
  81. A commit should also work when no changes were made to the database.
  82. """
  83. self.cx.commit()
  84. self.cx.commit()
  85. def CheckRollback(self):
  86. self.cx.rollback()
  87. def CheckRollbackAfterNoChanges(self):
  88. """
  89. A rollback should also work when no changes were made to the database.
  90. """
  91. self.cx.rollback()
  92. self.cx.rollback()
  93. def CheckCursor(self):
  94. cu = self.cx.cursor()
  95. def CheckFailedOpen(self):
  96. YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db"
  97. try:
  98. con = sqlite.connect(YOU_CANNOT_OPEN_THIS)
  99. except sqlite.OperationalError:
  100. return
  101. self.fail("should have raised an OperationalError")
  102. def CheckClose(self):
  103. self.cx.close()
  104. def CheckExceptions(self):
  105. # Optional DB-API extension.
  106. self.assertEqual(self.cx.Warning, sqlite.Warning)
  107. self.assertEqual(self.cx.Error, sqlite.Error)
  108. self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError)
  109. self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError)
  110. self.assertEqual(self.cx.DataError, sqlite.DataError)
  111. self.assertEqual(self.cx.OperationalError, sqlite.OperationalError)
  112. self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError)
  113. self.assertEqual(self.cx.InternalError, sqlite.InternalError)
  114. self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError)
  115. self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError)
  116. class CursorTests(unittest.TestCase):
  117. def setUp(self):
  118. self.cx = sqlite.connect(":memory:")
  119. self.cu = self.cx.cursor()
  120. self.cu.execute("create table test(id integer primary key, name text, income number)")
  121. self.cu.execute("insert into test(name) values (?)", ("foo",))
  122. def tearDown(self):
  123. self.cu.close()
  124. self.cx.close()
  125. def CheckExecuteNoArgs(self):
  126. self.cu.execute("delete from test")
  127. def CheckExecuteIllegalSql(self):
  128. try:
  129. self.cu.execute("select asdf")
  130. self.fail("should have raised an OperationalError")
  131. except sqlite.OperationalError:
  132. return
  133. except:
  134. self.fail("raised wrong exception")
  135. def CheckExecuteTooMuchSql(self):
  136. try:
  137. self.cu.execute("select 5+4; select 4+5")
  138. self.fail("should have raised a Warning")
  139. except sqlite.Warning:
  140. return
  141. except:
  142. self.fail("raised wrong exception")
  143. def CheckExecuteTooMuchSql2(self):
  144. self.cu.execute("select 5+4; -- foo bar")
  145. def CheckExecuteTooMuchSql3(self):
  146. self.cu.execute("""
  147. select 5+4;
  148. /*
  149. foo
  150. */
  151. """)
  152. def CheckExecuteWrongSqlArg(self):
  153. try:
  154. self.cu.execute(42)
  155. self.fail("should have raised a ValueError")
  156. except ValueError:
  157. return
  158. except:
  159. self.fail("raised wrong exception.")
  160. def CheckExecuteArgInt(self):
  161. self.cu.execute("insert into test(id) values (?)", (42,))
  162. def CheckExecuteArgFloat(self):
  163. self.cu.execute("insert into test(income) values (?)", (2500.32,))
  164. def CheckExecuteArgString(self):
  165. self.cu.execute("insert into test(name) values (?)", ("Hugo",))
  166. def CheckExecuteWrongNoOfArgs1(self):
  167. # too many parameters
  168. try:
  169. self.cu.execute("insert into test(id) values (?)", (17, "Egon"))
  170. self.fail("should have raised ProgrammingError")
  171. except sqlite.ProgrammingError:
  172. pass
  173. def CheckExecuteWrongNoOfArgs2(self):
  174. # too little parameters
  175. try:
  176. self.cu.execute("insert into test(id) values (?)")
  177. self.fail("should have raised ProgrammingError")
  178. except sqlite.ProgrammingError:
  179. pass
  180. def CheckExecuteWrongNoOfArgs3(self):
  181. # no parameters, parameters are needed
  182. try:
  183. self.cu.execute("insert into test(id) values (?)")
  184. self.fail("should have raised ProgrammingError")
  185. except sqlite.ProgrammingError:
  186. pass
  187. def CheckExecuteParamList(self):
  188. self.cu.execute("insert into test(name) values ('foo')")
  189. self.cu.execute("select name from test where name=?", ["foo"])
  190. row = self.cu.fetchone()
  191. self.assertEqual(row[0], "foo")
  192. def CheckExecuteParamSequence(self):
  193. class L(object):
  194. def __len__(self):
  195. return 1
  196. def __getitem__(self, x):
  197. assert x == 0
  198. return "foo"
  199. self.cu.execute("insert into test(name) values ('foo')")
  200. self.cu.execute("select name from test where name=?", L())
  201. row = self.cu.fetchone()
  202. self.assertEqual(row[0], "foo")
  203. def CheckExecuteDictMapping(self):
  204. self.cu.execute("insert into test(name) values ('foo')")
  205. self.cu.execute("select name from test where name=:name", {"name": "foo"})
  206. row = self.cu.fetchone()
  207. self.assertEqual(row[0], "foo")
  208. def CheckExecuteDictMapping_Mapping(self):
  209. # Test only works with Python 2.5 or later
  210. if sys.version_info < (2, 5, 0):
  211. return
  212. class D(dict):
  213. def __missing__(self, key):
  214. return "foo"
  215. self.cu.execute("insert into test(name) values ('foo')")
  216. self.cu.execute("select name from test where name=:name", D())
  217. row = self.cu.fetchone()
  218. self.assertEqual(row[0], "foo")
  219. def CheckExecuteDictMappingTooLittleArgs(self):
  220. self.cu.execute("insert into test(name) values ('foo')")
  221. try:
  222. self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"})
  223. self.fail("should have raised ProgrammingError")
  224. except sqlite.ProgrammingError:
  225. pass
  226. def CheckExecuteDictMappingNoArgs(self):
  227. self.cu.execute("insert into test(name) values ('foo')")
  228. try:
  229. self.cu.execute("select name from test where name=:name")
  230. self.fail("should have raised ProgrammingError")
  231. except sqlite.ProgrammingError:
  232. pass
  233. def CheckExecuteDictMappingUnnamed(self):
  234. self.cu.execute("insert into test(name) values ('foo')")
  235. try:
  236. self.cu.execute("select name from test where name=?", {"name": "foo"})
  237. self.fail("should have raised ProgrammingError")
  238. except sqlite.ProgrammingError:
  239. pass
  240. def CheckClose(self):
  241. self.cu.close()
  242. def CheckRowcountExecute(self):
  243. self.cu.execute("delete from test")
  244. self.cu.execute("insert into test(name) values ('foo')")
  245. self.cu.execute("insert into test(name) values ('foo')")
  246. self.cu.execute("update test set name='bar'")
  247. self.assertEqual(self.cu.rowcount, 2)
  248. def CheckRowcountSelect(self):
  249. """
  250. pysqlite does not know the rowcount of SELECT statements, because we
  251. don't fetch all rows after executing the select statement. The rowcount
  252. has thus to be -1.
  253. """
  254. self.cu.execute("select 5 union select 6")
  255. self.assertEqual(self.cu.rowcount, -1)
  256. def CheckRowcountExecutemany(self):
  257. self.cu.execute("delete from test")
  258. self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
  259. self.assertEqual(self.cu.rowcount, 3)
  260. def CheckTotalChanges(self):
  261. self.cu.execute("insert into test(name) values ('foo')")
  262. self.cu.execute("insert into test(name) values ('foo')")
  263. if self.cx.total_changes < 2:
  264. self.fail("total changes reported wrong value")
  265. # Checks for executemany:
  266. # Sequences are required by the DB-API, iterators
  267. # enhancements in pysqlite.
  268. def CheckExecuteManySequence(self):
  269. self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)])
  270. def CheckExecuteManyIterator(self):
  271. class MyIter:
  272. def __init__(self):
  273. self.value = 5
  274. def next(self):
  275. if self.value == 10:
  276. raise StopIteration
  277. else:
  278. self.value += 1
  279. return (self.value,)
  280. self.cu.executemany("insert into test(income) values (?)", MyIter())
  281. def CheckExecuteManyGenerator(self):
  282. def mygen():
  283. for i in range(5):
  284. yield (i,)
  285. self.cu.executemany("insert into test(income) values (?)", mygen())
  286. def CheckExecuteManyWrongSqlArg(self):
  287. try:
  288. self.cu.executemany(42, [(3,)])
  289. self.fail("should have raised a ValueError")
  290. except ValueError:
  291. return
  292. except:
  293. self.fail("raised wrong exception.")
  294. def CheckExecuteManySelect(self):
  295. try:
  296. self.cu.executemany("select ?", [(3,)])
  297. self.fail("should have raised a ProgrammingError")
  298. except sqlite.ProgrammingError:
  299. return
  300. except:
  301. self.fail("raised wrong exception.")
  302. def CheckExecuteManyNotIterable(self):
  303. try:
  304. self.cu.executemany("insert into test(income) values (?)", 42)
  305. self.fail("should have raised a TypeError")
  306. except TypeError:
  307. return
  308. except Exception, e:
  309. print "raised", e.__class__
  310. self.fail("raised wrong exception.")
  311. def CheckFetchIter(self):
  312. # Optional DB-API extension.
  313. self.cu.execute("delete from test")
  314. self.cu.execute("insert into test(id) values (?)", (5,))
  315. self.cu.execute("insert into test(id) values (?)", (6,))
  316. self.cu.execute("select id from test order by id")
  317. lst = []
  318. for row in self.cu:
  319. lst.append(row[0])
  320. self.assertEqual(lst[0], 5)
  321. self.assertEqual(lst[1], 6)
  322. def CheckFetchone(self):
  323. self.cu.execute("select name from test")
  324. row = self.cu.fetchone()
  325. self.assertEqual(row[0], "foo")
  326. row = self.cu.fetchone()
  327. self.assertEqual(row, None)
  328. def CheckFetchoneNoStatement(self):
  329. cur = self.cx.cursor()
  330. row = cur.fetchone()
  331. self.assertEqual(row, None)
  332. def CheckArraySize(self):
  333. # must default ot 1
  334. self.assertEqual(self.cu.arraysize, 1)
  335. # now set to 2
  336. self.cu.arraysize = 2
  337. # now make the query return 3 rows
  338. self.cu.execute("delete from test")
  339. self.cu.execute("insert into test(name) values ('A')")
  340. self.cu.execute("insert into test(name) values ('B')")
  341. self.cu.execute("insert into test(name) values ('C')")
  342. self.cu.execute("select name from test")
  343. res = self.cu.fetchmany()
  344. self.assertEqual(len(res), 2)
  345. def CheckFetchmany(self):
  346. self.cu.execute("select name from test")
  347. res = self.cu.fetchmany(100)
  348. self.assertEqual(len(res), 1)
  349. res = self.cu.fetchmany(100)
  350. self.assertEqual(res, [])
  351. def CheckFetchmanyKwArg(self):
  352. """Checks if fetchmany works with keyword arguments"""
  353. self.cu.execute("select name from test")
  354. res = self.cu.fetchmany(size=100)
  355. self.assertEqual(len(res), 1)
  356. def CheckFetchall(self):
  357. self.cu.execute("select name from test")
  358. res = self.cu.fetchall()
  359. self.assertEqual(len(res), 1)
  360. res = self.cu.fetchall()
  361. self.assertEqual(res, [])
  362. def CheckSetinputsizes(self):
  363. self.cu.setinputsizes([3, 4, 5])
  364. def CheckSetoutputsize(self):
  365. self.cu.setoutputsize(5, 0)
  366. def CheckSetoutputsizeNoColumn(self):
  367. self.cu.setoutputsize(42)
  368. def CheckCursorConnection(self):
  369. # Optional DB-API extension.
  370. self.assertEqual(self.cu.connection, self.cx)
  371. def CheckWrongCursorCallable(self):
  372. try:
  373. def f(): pass
  374. cur = self.cx.cursor(f)
  375. self.fail("should have raised a TypeError")
  376. except TypeError:
  377. return
  378. self.fail("should have raised a ValueError")
  379. def CheckCursorWrongClass(self):
  380. class Foo: pass
  381. foo = Foo()
  382. try:
  383. cur = sqlite.Cursor(foo)
  384. self.fail("should have raised a ValueError")
  385. except TypeError:
  386. pass
  387. class ThreadTests(unittest.TestCase):
  388. def setUp(self):
  389. self.con = sqlite.connect(":memory:")
  390. self.cur = self.con.cursor()
  391. self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)")
  392. def tearDown(self):
  393. self.cur.close()
  394. self.con.close()
  395. def CheckConCursor(self):
  396. def run(con, errors):
  397. try:
  398. cur = con.cursor()
  399. errors.append("did not raise ProgrammingError")
  400. return
  401. except sqlite.ProgrammingError:
  402. return
  403. except:
  404. errors.append("raised wrong exception")
  405. errors = []
  406. t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
  407. t.start()
  408. t.join()
  409. if len(errors) > 0:
  410. self.fail("\n".join(errors))
  411. def CheckConCommit(self):
  412. def run(con, errors):
  413. try:
  414. con.commit()
  415. errors.append("did not raise ProgrammingError")
  416. return
  417. except sqlite.ProgrammingError:
  418. return
  419. except:
  420. errors.append("raised wrong exception")
  421. errors = []
  422. t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
  423. t.start()
  424. t.join()
  425. if len(errors) > 0:
  426. self.fail("\n".join(errors))
  427. def CheckConRollback(self):
  428. def run(con, errors):
  429. try:
  430. con.rollback()
  431. errors.append("did not raise ProgrammingError")
  432. return
  433. except sqlite.ProgrammingError:
  434. return
  435. except:
  436. errors.append("raised wrong exception")
  437. errors = []
  438. t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
  439. t.start()
  440. t.join()
  441. if len(errors) > 0:
  442. self.fail("\n".join(errors))
  443. def CheckConClose(self):
  444. def run(con, errors):
  445. try:
  446. con.close()
  447. errors.append("did not raise ProgrammingError")
  448. return
  449. except sqlite.ProgrammingError:
  450. return
  451. except:
  452. errors.append("raised wrong exception")
  453. errors = []
  454. t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors})
  455. t.start()
  456. t.join()
  457. if len(errors) > 0:
  458. self.fail("\n".join(errors))
  459. def CheckCurImplicitBegin(self):
  460. def run(cur, errors):
  461. try:
  462. cur.execute("insert into test(name) values ('a')")
  463. errors.append("did not raise ProgrammingError")
  464. return
  465. except sqlite.ProgrammingError:
  466. return
  467. except:
  468. errors.append("raised wrong exception")
  469. errors = []
  470. t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
  471. t.start()
  472. t.join()
  473. if len(errors) > 0:
  474. self.fail("\n".join(errors))
  475. def CheckCurClose(self):
  476. def run(cur, errors):
  477. try:
  478. cur.close()
  479. errors.append("did not raise ProgrammingError")
  480. return
  481. except sqlite.ProgrammingError:
  482. return
  483. except:
  484. errors.append("raised wrong exception")
  485. errors = []
  486. t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
  487. t.start()
  488. t.join()
  489. if len(errors) > 0:
  490. self.fail("\n".join(errors))
  491. def CheckCurExecute(self):
  492. def run(cur, errors):
  493. try:
  494. cur.execute("select name from test")
  495. errors.append("did not raise ProgrammingError")
  496. return
  497. except sqlite.ProgrammingError:
  498. return
  499. except:
  500. errors.append("raised wrong exception")
  501. errors = []
  502. self.cur.execute("insert into test(name) values ('a')")
  503. t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
  504. t.start()
  505. t.join()
  506. if len(errors) > 0:
  507. self.fail("\n".join(errors))
  508. def CheckCurIterNext(self):
  509. def run(cur, errors):
  510. try:
  511. row = cur.fetchone()
  512. errors.append("did not raise ProgrammingError")
  513. return
  514. except sqlite.ProgrammingError:
  515. return
  516. except:
  517. errors.append("raised wrong exception")
  518. errors = []
  519. self.cur.execute("insert into test(name) values ('a')")
  520. self.cur.execute("select name from test")
  521. t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors})
  522. t.start()
  523. t.join()
  524. if len(errors) > 0:
  525. self.fail("\n".join(errors))
  526. class ConstructorTests(unittest.TestCase):
  527. def CheckDate(self):
  528. d = sqlite.Date(2004, 10, 28)
  529. def CheckTime(self):
  530. t = sqlite.Time(12, 39, 35)
  531. def CheckTimestamp(self):
  532. ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35)
  533. def CheckDateFromTicks(self):
  534. d = sqlite.DateFromTicks(42)
  535. def CheckTimeFromTicks(self):
  536. t = sqlite.TimeFromTicks(42)
  537. def CheckTimestampFromTicks(self):
  538. ts = sqlite.TimestampFromTicks(42)
  539. def CheckBinary(self):
  540. b = sqlite.Binary(chr(0) + "'")
  541. class ExtensionTests(unittest.TestCase):
  542. def CheckScriptStringSql(self):
  543. con = sqlite.connect(":memory:")
  544. cur = con.cursor()
  545. cur.executescript("""
  546. -- bla bla
  547. /* a stupid comment */
  548. create table a(i);
  549. insert into a(i) values (5);
  550. """)
  551. cur.execute("select i from a")
  552. res = cur.fetchone()[0]
  553. self.assertEqual(res, 5)
  554. def CheckScriptStringUnicode(self):
  555. con = sqlite.connect(":memory:")
  556. cur = con.cursor()
  557. cur.executescript(u"""
  558. create table a(i);
  559. insert into a(i) values (5);
  560. select i from a;
  561. delete from a;
  562. insert into a(i) values (6);
  563. """)
  564. cur.execute("select i from a")
  565. res = cur.fetchone()[0]
  566. self.assertEqual(res, 6)
  567. def CheckScriptErrorIncomplete(self):
  568. con = sqlite.connect(":memory:")
  569. cur = con.cursor()
  570. raised = False
  571. try:
  572. cur.executescript("create table test(sadfsadfdsa")
  573. except sqlite.ProgrammingError:
  574. raised = True
  575. self.assertEqual(raised, True, "should have raised an exception")
  576. def CheckScriptErrorNormal(self):
  577. con = sqlite.connect(":memory:")
  578. cur = con.cursor()
  579. raised = False
  580. try:
  581. cur.executescript("create table test(sadfsadfdsa); select foo from hurz;")
  582. except sqlite.OperationalError:
  583. raised = True
  584. self.assertEqual(raised, True, "should have raised an exception")
  585. def CheckConnectionExecute(self):
  586. con = sqlite.connect(":memory:")
  587. result = con.execute("select 5").fetchone()[0]
  588. self.assertEqual(result, 5, "Basic test of Connection.execute")
  589. def CheckConnectionExecutemany(self):
  590. con = sqlite.connect(":memory:")
  591. con.execute("create table test(foo)")
  592. con.executemany("insert into test(foo) values (?)", [(3,), (4,)])
  593. result = con.execute("select foo from test order by foo").fetchall()
  594. self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany")
  595. self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany")
  596. def CheckConnectionExecutescript(self):
  597. con = sqlite.connect(":memory:")
  598. con.executescript("create table test(foo); insert into test(foo) values (5);")
  599. result = con.execute("select foo from test").fetchone()[0]
  600. self.assertEqual(result, 5, "Basic test of Connection.executescript")
  601. class ClosedTests(unittest.TestCase):
  602. def setUp(self):
  603. pass
  604. def tearDown(self):
  605. pass
  606. def CheckClosedConCursor(self):
  607. con = sqlite.connect(":memory:")
  608. con.close()
  609. try:
  610. cur = con.cursor()
  611. self.fail("Should have raised a ProgrammingError")
  612. except sqlite.ProgrammingError:
  613. pass
  614. except:
  615. self.fail("Should have raised a ProgrammingError")
  616. def CheckClosedConCommit(self):
  617. con = sqlite.connect(":memory:")
  618. con.close()
  619. try:
  620. con.commit()
  621. self.fail("Should have raised a ProgrammingError")
  622. except sqlite.ProgrammingError:
  623. pass
  624. except:
  625. self.fail("Should have raised a ProgrammingError")
  626. def CheckClosedConRollback(self):
  627. con = sqlite.connect(":memory:")
  628. con.close()
  629. try:
  630. con.rollback()
  631. self.fail("Should have raised a ProgrammingError")
  632. except sqlite.ProgrammingError:
  633. pass
  634. except:
  635. self.fail("Should have raised a ProgrammingError")
  636. def CheckClosedCurExecute(self):
  637. con = sqlite.connect(":memory:")
  638. cur = con.cursor()
  639. con.close()
  640. try:
  641. cur.execute("select 4")
  642. self.fail("Should have raised a ProgrammingError")
  643. except sqlite.ProgrammingError:
  644. pass
  645. except:
  646. self.fail("Should have raised a ProgrammingError")
  647. def suite():
  648. module_suite = unittest.makeSuite(ModuleTests, "Check")
  649. connection_suite = unittest.makeSuite(ConnectionTests, "Check")
  650. cursor_suite = unittest.makeSuite(CursorTests, "Check")
  651. thread_suite = unittest.makeSuite(ThreadTests, "Check")
  652. constructor_suite = unittest.makeSuite(ConstructorTests, "Check")
  653. ext_suite = unittest.makeSuite(ExtensionTests, "Check")
  654. closed_suite = unittest.makeSuite(ClosedTests, "Check")
  655. return unittest.TestSuite((module_suite, connection_suite, cursor_suite, thread_suite, constructor_suite, ext_suite, closed_suite))
  656. def test():
  657. runner = unittest.TextTestRunner()
  658. runner.run(suite())
  659. if __name__ == "__main__":
  660. test()