summaryrefslogtreecommitdiffstats
path: root/Lib/sqlite3/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/sqlite3/test')
-rw-r--r--Lib/sqlite3/test/dbapi.py18
-rw-r--r--Lib/sqlite3/test/factory.py34
-rw-r--r--Lib/sqlite3/test/regression.py10
-rw-r--r--Lib/sqlite3/test/types.py15
4 files changed, 62 insertions, 15 deletions
diff --git a/Lib/sqlite3/test/dbapi.py b/Lib/sqlite3/test/dbapi.py
index b7ec1ad..04649fc 100644
--- a/Lib/sqlite3/test/dbapi.py
+++ b/Lib/sqlite3/test/dbapi.py
@@ -28,6 +28,9 @@ try:
except ImportError:
threading = None
+from test.support import TESTFN, unlink
+
+
class ModuleTests(unittest.TestCase):
def CheckAPILevel(self):
self.assertEqual(sqlite.apilevel, "2.0",
@@ -163,6 +166,21 @@ class ConnectionTests(unittest.TestCase):
with self.assertRaises(AttributeError):
self.cx.in_transaction = True
+ def CheckOpenUri(self):
+ if sqlite.sqlite_version_info < (3, 7, 7):
+ with self.assertRaises(sqlite.NotSupportedError):
+ sqlite.connect(':memory:', uri=True)
+ return
+ self.addCleanup(unlink, TESTFN)
+ with sqlite.connect(TESTFN) as cx:
+ cx.execute('create table test(id integer)')
+ with sqlite.connect('file:' + TESTFN, uri=True) as cx:
+ cx.execute('insert into test(id) values(0)')
+ with sqlite.connect('file:' + TESTFN + '?mode=ro', uri=True) as cx:
+ with self.assertRaises(sqlite.OperationalError):
+ cx.execute('insert into test(id) values(1)')
+
+
class CursorTests(unittest.TestCase):
def setUp(self):
self.cx = sqlite.connect(":memory:")
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py
index 1013755..a8348b4 100644
--- a/Lib/sqlite3/test/factory.py
+++ b/Lib/sqlite3/test/factory.py
@@ -23,6 +23,7 @@
import unittest
import sqlite3 as sqlite
+from collections.abc import Sequence
class MyConnection(sqlite.Connection):
def __init__(self, *args, **kwargs):
@@ -96,9 +97,19 @@ class RowFactoryTests(unittest.TestCase):
self.assertEqual(col1, 1, "by name: wrong result for column 'A'")
self.assertEqual(col2, 2, "by name: wrong result for column 'B'")
- col1, col2 = row[0], row[1]
- self.assertEqual(col1, 1, "by index: wrong result for column 0")
- self.assertEqual(col2, 2, "by index: wrong result for column 1")
+ self.assertEqual(row[0], 1, "by index: wrong result for column 0")
+ self.assertEqual(row[1], 2, "by index: wrong result for column 1")
+ self.assertEqual(row[-1], 2, "by index: wrong result for column -1")
+ self.assertEqual(row[-2], 1, "by index: wrong result for column -2")
+
+ with self.assertRaises(IndexError):
+ row['c']
+ with self.assertRaises(IndexError):
+ row[2]
+ with self.assertRaises(IndexError):
+ row[-3]
+ with self.assertRaises(IndexError):
+ row[2**1000]
def CheckSqliteRowIter(self):
"""Checks if the row object is iterable"""
@@ -142,6 +153,23 @@ class RowFactoryTests(unittest.TestCase):
self.assertNotEqual(row_1, row_3)
self.assertNotEqual(hash(row_1), hash(row_3))
+ def CheckSqliteRowAsSequence(self):
+ """ Checks if the row object can act like a sequence """
+ self.con.row_factory = sqlite.Row
+ row = self.con.execute("select 1 as a, 2 as b").fetchone()
+
+ as_tuple = tuple(row)
+ self.assertEqual(list(reversed(row)), list(reversed(as_tuple)))
+ self.assertIsInstance(row, Sequence)
+
+ def CheckFakeCursorClass(self):
+ # Issue #24257: Incorrect use of PyObject_IsInstance() caused
+ # segmentation fault.
+ class FakeCursor(str):
+ __class__ = sqlite.Cursor
+ cur = self.con.cursor(factory=FakeCursor)
+ self.assertRaises(TypeError, sqlite.Row, cur, ())
+
def tearDown(self):
self.con.close()
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index c557ab6..eaaaa2c 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -336,6 +336,16 @@ class RegressionTests(unittest.TestCase):
sqlite.connect, ":memory:", isolation_level=123)
+ def CheckNullCharacter(self):
+ # Issue #21147
+ con = sqlite.connect(":memory:")
+ self.assertRaises(ValueError, con, "\0select 1")
+ self.assertRaises(ValueError, con, "select 1\0")
+ cur = con.cursor()
+ self.assertRaises(ValueError, cur.execute, " \0select 2")
+ self.assertRaises(ValueError, cur.execute, "select 2\0")
+
+
def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check")
return unittest.TestSuite((regression_suite,))
diff --git a/Lib/sqlite3/test/types.py b/Lib/sqlite3/test/types.py
index a8fdad9..adad571 100644
--- a/Lib/sqlite3/test/types.py
+++ b/Lib/sqlite3/test/types.py
@@ -88,19 +88,10 @@ class DeclTypesTests(unittest.TestCase):
_val = _val.decode('utf-8')
self.val = _val
- def __cmp__(self, other):
- if not isinstance(other, DeclTypesTests.Foo):
- raise ValueError
- if self.val == other.val:
- return 0
- else:
- return 1
-
def __eq__(self, other):
- c = self.__cmp__(other)
- if c is NotImplemented:
- return c
- return c == 0
+ if not isinstance(other, DeclTypesTests.Foo):
+ return NotImplemented
+ return self.val == other.val
def __conform__(self, protocol):
if protocol is sqlite.PrepareProtocol: