Skip to content

Commit 40542ba

Browse files
author
Erlend E. Aasland
committed
Fix sqlite3 deterministic test
1 parent 5eb45d7 commit 40542ba

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Lib/sqlite3/test/userfunctions.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def setUp(self):
158158
self.con.create_function("isblob", 1, func_isblob)
159159
self.con.create_function("islonglong", 1, func_islonglong)
160160
self.con.create_function("spam", -1, func)
161+
self.con.execute("create table test(t text)")
161162

162163
def tearDown(self):
163164
self.con.close()
@@ -276,18 +277,24 @@ def CheckAnyArguments(self):
276277
val = cur.fetchone()[0]
277278
self.assertEqual(val, 2)
278279

280+
@unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "Requires SQLite 3.8.3 or higher")
279281
def CheckFuncNonDeterministic(self):
282+
"""Non-deterministic functions cannot be used with partial indices"""
280283
mock = unittest.mock.Mock(return_value=None)
281-
self.con.create_function("deterministic", 0, mock, deterministic=False)
282-
self.con.execute("select deterministic() = deterministic()")
283-
self.assertEqual(mock.call_count, 2)
284+
self.con.create_function("nondeterministic", 0, mock, deterministic=False)
285+
with self.assertRaises(sqlite.OperationalError) as cm:
286+
self.con.execute("create index t on test(t) where nondeterministic() is not null")
287+
self.assertEqual(str(cm.exception), "non-deterministic functions prohibited in partial index WHERE clauses")
284288

285-
@unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "deterministic parameter not supported")
289+
@unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "Requires SQLite 3.8.3 or higher")
286290
def CheckFuncDeterministic(self):
291+
"""Deterministic functions can be used with partial indices"""
287292
mock = unittest.mock.Mock(return_value=None)
288293
self.con.create_function("deterministic", 0, mock, deterministic=True)
289-
self.con.execute("select deterministic() = deterministic()")
290-
self.assertEqual(mock.call_count, 1)
294+
try:
295+
self.con.execute("create index t on test(t) where deterministic() is not null")
296+
except sqlite.OperationalError:
297+
self.fail("Unexpected failure while creating partial index")
291298

292299
@unittest.skipIf(sqlite.sqlite_version_info >= (3, 8, 3), "SQLite < 3.8.3 needed")
293300
def CheckFuncDeterministicNotSupported(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`sqlite3` unit test for deterministic functions.

0 commit comments

Comments
 (0)