def test_executescript_step_through_select(self): with memory_database() as con: values = [(v, ) for v in range(5)] with con: con.execute("create table t(t)") con.executemany("insert into t values(?)", values) steps = [] con.create_function("step", 1, lambda x: steps.append((x, ))) con.executescript("select step(t) from t") self.assertEqual(steps, values)
def test_table_lock_cursor_dealloc(self): with memory_database() as con: con.execute("create table t(t)") con.executemany("insert into t values(?)", ((v, ) for v in range(5))) con.commit() cur = con.execute("select t from t") del cur con.execute("drop table t") con.commit()
def test_large_sql(self): msg = "query string is too large" with memory_database() as cx, cx_limit(cx) as lim: cu = cx.cursor() cx("select 1".ljust(lim)) # use a different SQL statement; don't reuse from the LRU cache cu.execute("select 2".ljust(lim)) sql = "select 3".ljust(lim + 1) self.assertRaisesRegex(sqlite.DataError, msg, cx, sql) self.assertRaisesRegex(sqlite.DataError, msg, cu.execute, sql)
def test_trace_expanded_sql(self): expected = [ "create table t(t)", "BEGIN ", "insert into t values(0)", "insert into t values(1)", "insert into t values(2)", "COMMIT", ] with memory_database() as cx, self.check_stmt_trace(cx, expected): with cx: cx.execute("create table t(t)") cx.executemany("insert into t values(?)", ((v, ) for v in range(3)))
def test_table_lock_cursor_non_readonly_select(self): with memory_database() as con: con.execute("create table t(t)") con.executemany("insert into t values(?)", ((v, ) for v in range(5))) con.commit() def dup(v): con.execute("insert into t values(?)", (v, )) return con.create_function("dup", 1, dup) cur = con.execute("select dup(t) from t") del cur con.execute("drop table t") con.commit()
def test_trace_too_much_expanded_sql(self): # If the expanded string is too large, we'll fall back to the # unexpanded SQL statement (for SQLite 3.14.0 and newer). # The resulting string length is limited by the runtime limit # SQLITE_LIMIT_LENGTH. template = "select 1 as a where a=" category = sqlite.SQLITE_LIMIT_LENGTH with memory_database() as cx, cx_limit(cx, category=category) as lim: ok_param = "a" bad_param = "a" * lim unexpanded_query = template + "?" expected = [unexpanded_query] if sqlite.sqlite_version_info < (3, 14, 0): expected = [] with self.check_stmt_trace(cx, expected): cx.execute(unexpanded_query, (bad_param, )) expanded_query = f"{template}'{ok_param}'" with self.check_stmt_trace(cx, [expanded_query]): cx.execute(unexpanded_query, (ok_param, ))
def test_isolation_level_none(self): with memory_database(isolation_level=None) as cx: self._run_test(cx) self.assertEqual(self.traced, [self.INSERT])
def test_isolation_level_exclusive(self): with memory_database(isolation_level="EXCLUSIVE") as cx: self._run_test(cx) self.assertEqual(self.traced, ["BEGIN EXCLUSIVE", self.INSERT, "COMMIT"])
def test_isolation_level_immediate(self): with memory_database(isolation_level="IMMEDIATE") as cx: self._run_test(cx) self.assertEqual(self.traced, ["BEGIN IMMEDIATE", self.INSERT, "COMMIT"])
def test_isolation_level_deferred(self): with memory_database(isolation_level="DEFERRED") as cx: self._run_test(cx) self.assertEqual(self.traced, ["BEGIN DEFERRED", self.INSERT, "COMMIT"])
def test_isolation_level_begin(self): with memory_database(isolation_level="") as cx: self._run_test(cx) self.assertEqual(self.traced, ["BEGIN ", self.INSERT, "COMMIT"])
def test_trace_bad_handler(self): with memory_database() as cx: cx.set_trace_callback(lambda stmt: 5 / 0) cx.execute("select 1")