Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _nonexistent_table_helper(executor, tr):
query = 'SELECT * FROM nonexistent_table'
try:
executor(query)
assert False
except apsw.SQLError:
#tr._start_new_session()
assert tr._check_error_entries(tr.session_id) > 0
def test_cast_to_qmol(self):
c = self.db.cursor()
c.execute("select qmol('CCC')")
c.execute("select qmol('C[*]C')")
c.execute("select qmol('[C,N]')")
self.assertRaises(apsw.SQLError,lambda : c.execute("select qmol('BAD')"))
bdb.execute('ANALYZE t_cc MODELS 0-2 FOR 10 ITERATIONS WAIT')
# How to properly use the estimate_pairwise_similarity function.
parallel.estimate_pairwise_similarity(
bdb_file.name, 't', 't_cc'
)
# Should complain with bad core value
with pytest.raises(BLE):
parallel.estimate_pairwise_similarity(
bdb_file.name, 't', 't_cc', cores=0
)
# Should complain if overwrite flag is not set, but t_similarity
# exists
with pytest.raises(SQLError):
parallel.estimate_pairwise_similarity(
bdb_file.name, 't', 't_cc'
)
# Should complain if model and table don't exist
with pytest.raises(SQLError):
parallel.estimate_pairwise_similarity(
bdb_file.name, 'foo', 'foo_cc'
)
# Should complain if bdb_file doesn't exist
with tempfile.NamedTemporaryFile() as does_not_exist:
with pytest.raises(SQLError):
parallel.estimate_pairwise_similarity(
does_not_exist.name, 't', 't_cc'
)
# Should run fine if overwrite flag is set
def test_insert_error_handler(self):
error = apsw.ConstraintError("audit_evt.request_id")
wrong_error = apsw.ConstraintError()
query = "iNserT something audit_evt.request_id"
wrong_query = "select"
values = ()
class Sqlite3WorkerMock:
def __init__(self):
pass
with mock.patch('evt.evt_pool_manager.logger') as logger_mock:
worker_mock = Sqlite3WorkerMock()
EventPoolManager.insert_error_handler(worker_mock, query, values=values, err=error)
self.assertTrue(logger_mock.warning.called)
self.assertFalse(logger_mock.error.called)
with mock.patch('evt.evt_pool_manager.logger') as logger_mock:
# Misses proper error message
def test_insert_error_handler(self):
error = apsw.ConstraintError("audit_evt.request_id")
wrong_error = apsw.ConstraintError()
query = "iNserT something audit_evt.request_id"
wrong_query = "select"
values = ()
class Sqlite3WorkerMock:
def __init__(self):
pass
with mock.patch('evt.evt_pool_manager.logger') as logger_mock:
worker_mock = Sqlite3WorkerMock()
EventPoolManager.insert_error_handler(worker_mock, query, values=values, err=error)
self.assertTrue(logger_mock.warning.called)
self.assertFalse(logger_mock.error.called)
with mock.patch('evt.evt_pool_manager.logger') as logger_mock:
result = self.worker.execute("select * from evt_status")
# The result is string if the database is locked (caused by previously failed tests)
self.assertFalse(isinstance(result, str))
self.assertEqual(len(result), 5,
'We are expecting 5 event type records. There are {0}'.format(len(result)))
new_value = [x for x in result if x['id'] == 'AS'][0]
self.assertEqual(new_value, original_value,
"The original value changed after the insert")
# This should stay at the very end after the worker thread has been merged
self.worker.close()
self.assertTrue(logger_mock.error.called)
args, _ = logger_mock.error.call_args
self.assertTrue(isinstance(args[3], apsw.ConstraintError))
countem = 'select count(*) from %s' % (qt,)
assert not bayesdb_has_table(bdb, t)
with pytest.raises(ValueError):
bayesdb_read_pandas_df(bdb, t, df, index=index)
bayesdb_read_pandas_df(bdb, t, df, create=True, ifnotexists=False,
index=index)
assert len(df.index) == bdb.execute(countem).fetchvalue()
with pytest.raises(ValueError):
bayesdb_read_pandas_df(bdb, t, df, create=True, ifnotexists=False,
index=index)
assert 4 == bdb.execute(countem).fetchvalue()
with pytest.raises(apsw.ConstraintError):
bayesdb_read_pandas_df(bdb, t, df, create=True, ifnotexists=True,
index=index)
assert 4 == bdb.execute(countem).fetchvalue()
@raises(CantOpenError)
def test_open_db_connection_no_permission(self):
os.chmod(os.path.join(self.session_base_dir), 0)
sqlite_test_2 = SQLiteCacheDB(os.path.join(self.session_base_dir, "test_db.db"))
continue
if args[0]=="batch":
self.interactive=False
args=args[1:]
continue
if args[0] in ("separator", "nullvalue", "encoding"):
if len(args)<2:
raise self.Error("You need to specify a value after -"+args[0])
getattr(self, "command_"+args[0])([args[1]])
args=args[2:]
continue
if args[0]=="version":
self.write(self.stdout, apsw.sqlitelibversion()+"\n")
# A pretty gnarly thing to do
sys.exit(0)
if args[0]=="help":
self.write(self.stderr, self.usage())
sys.exit(0)
if args[0] in ("no-colour", "no-color", "nocolour", "nocolor"):
self.colour_scheme="off"
self._out_colour()
args=args[1:]
continue
# only remaining known args are output modes
if getattr(self, "output_"+args[0], None):
self.command_mode(args[:1])
continue
if args[0]=="batch":
self.interactive=False
args=args[1:]
continue
if args[0] in ("separator", "nullvalue", "encoding"):
if len(args)<2:
raise self.Error("You need to specify a value after -"+args[0])
getattr(self, "command_"+args[0])([args[1]])
args=args[2:]
continue
if args[0]=="version":
self.write(self.stdout, apsw.sqlitelibversion()+"\n")
# A pretty gnarly thing to do
sys.exit(0)
if args[0]=="help":
self.write(self.stderr, self.usage())
sys.exit(0)
if args[0] in ("no-colour", "no-color", "nocolour", "nocolor"):
self.colour_scheme="off"
self._out_colour()
args=args[1:]
continue
# only remaining known args are output modes
if getattr(self, "output_"+args[0], None):
self.command_mode(args[:1])