def setUp(self): try: os.remove(get_db_path()) except: pass self.con1 = dbsql.connect(get_db_path(), timeout=0.1) self.cur1 = self.con1.cursor() self.con2 = dbsql.connect(get_db_path(), timeout=0.1) self.cur2 = self.con2.cursor()
def CheckFailedOpen(self): YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" try: con = dbsql.connect(YOU_CANNOT_OPEN_THIS) except dbsql.OperationalError: return self.fail("should have raised an OperationalError")
def CheckCollationIsUsed(self): if dbsql.version_info < (3, 2, 1): # old Dbsql versions crash on this test return def mycoll(x, y): # reverse order return -cmp(x, y) con = dbsql.connect(":memory:") con.create_collation("mycoll", mycoll) sql = """ select x from ( select 'a' as x union select 'b' as x union select 'c' as x ) order by x collate mycoll """ result = con.execute(sql).fetchall() if result[0][0] != "c" or result[1][0] != "b" or result[2][0] != "a": self.fail("the expected order was not returned") con.create_collation("mycoll", None) try: result = con.execute(sql).fetchall() self.fail("should have raised an OperationalError") except dbsql.OperationalError, e: self.failUnlessEqual(e.args[0].lower(), "no such collation sequence: mycoll")
def CheckCreateCollationNotAscii(self): con = dbsql.connect(":memory:") try: con.create_collation("collä", cmp) self.fail("should have raised a ProgrammingError") except dbsql.ProgrammingError, e: pass
def CheckCreateCollationNotCallable(self): con = dbsql.connect(":memory:") try: con.create_collation("X", 42) self.fail("should have raised a TypeError") except TypeError, e: self.failUnlessEqual(e.args[0], "parameter must be callable")
def CheckConnectionExecutemany(self): con = dbsql.connect(":memory:") con.execute("create table test(foo)") con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) result = con.execute("select foo from test order by foo").fetchall() self.failUnlessEqual(result[0][0], 3, "Basic test of Connection.executemany") self.failUnlessEqual(result[1][0], 4, "Basic test of Connection.executemany")
def setUp(self): self.con = dbsql.connect(":memory:") try: del dbsql.adapters[int] except: pass dbsql.register_adapter(int, ObjectAdaptationTests.cast) self.cur = self.con.cursor()
def setUp(self): self.con = dbsql.connect(":memory:", detect_types=dbsql.PARSE_COLNAMES) self.cur = self.con.cursor() self.cur.execute("create table test(x foo)") dbsql.converters["FOO"] = lambda x: "[%s]" % x dbsql.converters["BAR"] = lambda x: "<%s>" % x dbsql.converters["EXC"] = lambda x: 5 / 0 dbsql.converters["B1B1"] = lambda x: "MARKER"
def CheckScriptErrorIncomplete(self): con = dbsql.connect(":memory:") cur = con.cursor() raised = False try: cur.executescript("create table test(sadfsadfdsa") except dbsql.ProgrammingError: raised = True self.failUnlessEqual(raised, True, "should have raised an exception")
def CheckScriptErrorNormal(self): con = dbsql.connect(":memory:") cur = con.cursor() raised = False try: cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") except dbsql.OperationalError: raised = True self.failUnlessEqual(raised, True, "should have raised an exception")
def CheckPragmaSchemaVersion(self): # This still crashed pydbsql <= 2.2.1 con = dbsql.connect(":memory:", detect_types=dbsql.PARSE_COLNAMES) try: cur = self.con.cursor() cur.execute("pragma schema_version") finally: cur.close() con.close()
def CheckStatementAvailable(self): # pydbsql up to 2.3.2 crashed on this, because the active statement handle was not checked # before trying to fetch data from it. close() destroys the active statement ... con = dbsql.connect(":memory:", detect_types=dbsql.PARSE_DECLTYPES) cur = con.cursor() cur.execute("select 4 union select 5") cur.close() cur.fetchone() cur.fetchone()
def getcon(): #con = dbsql.connect("db", isolation_level=None, timeout=5.0) con = dbsql.connect(":memory:") cur = con.cursor() cur.execute("create table test(i, s)") for i in range(10): cur.execute("insert into test(i, s) values (?, 'asfd')", (i,)) con.commit() cur.close() return con
def CheckClosedConRollback(self): con = dbsql.connect(":memory:") con.close() try: con.rollback() self.fail("Should have raised a ProgrammingError") except dbsql.ProgrammingError: pass except: self.fail("Should have raised a ProgrammingError")
def CheckClosedCurExecute(self): con = dbsql.connect(":memory:") cur = con.cursor() con.close() try: cur.execute("select 4") self.fail("Should have raised a ProgrammingError") except dbsql.ProgrammingError: pass except: self.fail("Should have raised a ProgrammingError")
def setUp(self): self.con = dbsql.connect(":memory:", detect_types=dbsql.PARSE_DECLTYPES) self.cur = self.con.cursor() self.cur.execute("create table test(i int, s str, f float, b bool, u unicode, foo foo, bin blob)") # override float, make them always return the same number dbsql.converters["FLOAT"] = lambda x: 47.2 # and implement two custom ones dbsql.converters["BOOL"] = lambda x: bool(int(x)) dbsql.converters["FOO"] = DeclTypesTests.Foo dbsql.converters["WRONG"] = lambda x: "WRONG"
def CheckScriptStringSql(self): con = dbsql.connect(":memory:") cur = con.cursor() cur.executescript(""" -- bla bla /* a stupid comment */ create table a(i); insert into a(i) values (5); """) cur.execute("select i from a") res = cur.fetchone()[0] self.failUnlessEqual(res, 5)
def CheckCollationRegisterTwice(self): """ Register two different collation functions under the same name. Verify that the last one is actually used. """ con = dbsql.connect(":memory:") con.create_collation("mycoll", cmp) con.create_collation("mycoll", lambda x, y: -cmp(x, y)) result = con.execute(""" select x from (select 'a' as x union select 'b' as x) order by x collate mycoll """).fetchall() if result[0][0] != 'b' or result[1][0] != 'a': self.fail("wrong collation function is used")
def CheckScriptStringUnicode(self): con = dbsql.connect(":memory:") cur = con.cursor() cur.executescript(u""" create table a(i); insert into a(i) values (5); select i from a; delete from a; insert into a(i) values (6); """) cur.execute("select i from a") res = cur.fetchone()[0] self.failUnlessEqual(res, 6)
def setUp(self): self.con = dbsql.connect(":memory:") self.con.executescript(""" create table t1 (c1, c2); create table t2 (c1, c2); insert into t1 (c1, c2) values (1, 2); insert into t2 (c1, c2) values (4, 5); """) # For our security test: self.con.execute("select c2 from t2") self.con.set_authorizer(authorizer_cb)
def create_db(): con = dbsql.connect(":memory:") if use_autocommit: if use_pydbsql: con.isolation_level = None else: con.autocommit = True cur = con.cursor() cur.execute(""" create table test(v text, f float, i integer) """) cur.close() return con
def CheckStatementReset(self): # pydbsql 2.1.0 to 2.2.0 have the problem that not all statements are # reset before a rollback, but only those that are still in the # statement cache. The others are not accessible from the connection object. con = dbsql.connect(":memory:", cached_statements=5) cursors = [con.cursor() for x in xrange(5)] cursors[0].execute("create table test(x)") for i in range(10): cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in xrange(10)]) for i in range(5): cursors[i].execute(" " * i + "select x from test") con.rollback()
def CheckDeregisterCollation(self): """ Register a collation, then deregister it. Make sure an error is raised if we try to use it. """ con = dbsql.connect(":memory:") con.create_collation("mycoll", cmp) con.create_collation("mycoll", None) try: con.execute("select 'a' as x union select 'b' as x order by x collate mycoll") self.fail("should have raised an OperationalError") except dbsql.OperationalError, e: if not e.args[0].startswith("no such collation sequence"): self.fail("wrong OperationalError raised")
def setUp(self): self.con = dbsql.connect(":memory:") self.con.create_function("returntext", 0, func_returntext) self.con.create_function("returnunicode", 0, func_returnunicode) self.con.create_function("returnint", 0, func_returnint) self.con.create_function("returnfloat", 0, func_returnfloat) self.con.create_function("returnnull", 0, func_returnnull) self.con.create_function("returnblob", 0, func_returnblob) self.con.create_function("raiseexception", 0, func_raiseexception) self.con.create_function("isstring", 1, func_isstring) self.con.create_function("isint", 1, func_isint) self.con.create_function("isfloat", 1, func_isfloat) self.con.create_function("isnone", 1, func_isnone) self.con.create_function("isblob", 1, func_isblob)
def setUp(self): self.con = dbsql.connect(":memory:") cur = self.con.cursor() cur.execute(""" create table test( t text, i integer, f float, n, b blob ) """) cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)", ("foo", 5, 3.14, None, buffer("blob"),)) self.con.create_aggregate("nostep", 1, AggrNoStep) self.con.create_aggregate("nofinalize", 1, AggrNoFinalize) self.con.create_aggregate("excInit", 1, AggrExceptionInInit) self.con.create_aggregate("excStep", 1, AggrExceptionInStep) self.con.create_aggregate("excFinalize", 1, AggrExceptionInFinalize) self.con.create_aggregate("checkType", 2, AggrCheckType) self.con.create_aggregate("mysum", 1, AggrSum)
def setUp(self): self.con = dbsql.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)")
def setUp(self): self.con = dbsql.connect(":memory:")
def setUp(self): self.cx = dbsql.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute("create table test(id integer primary key, name text, income number)") self.cu.execute("insert into test(name) values (?)", ("foo",))
def CheckConnectionExecute(self): con = dbsql.connect(":memory:") result = con.execute("select 5").fetchone()[0] self.failUnlessEqual(result, 5, "Basic test of Connection.execute")
def CheckConnectionExecutescript(self): con = dbsql.connect(":memory:") con.executescript("create table test(foo); insert into test(foo) values (5);") result = con.execute("select foo from test").fetchone()[0] self.failUnlessEqual(result, 5, "Basic test of Connection.executescript")