Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.old_file = norm_file(
os.path.join(os.path.dirname(__file__),
'db', 'migrate.sqlite'))
self.new_file = norm_file(
os.path.join(os.path.dirname(__file__),
'db', 'migrate_new.sqlite'))
self.table_name = 'unnamed'
MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS %s (key TEXT PRIMARY KEY, value BLOB)' % self.table_name
self.conn_old = sqlitedict.SqliteMultithread(self.old_file, autocommit=True, journal_mode="DELETE")
self.conn_old.execute(MAKE_TABLE)
self.conn_old.commit()
self.conn_new = sqlitedict.SqliteMultithread(self.new_file, autocommit=True, journal_mode="DELETE")
self.conn_new.execute(MAKE_TABLE)
self.conn_new.commit()
def setUp(self):
self.old_file = norm_file(
os.path.join(os.path.dirname(__file__),
'db', 'migrate.sqlite'))
self.new_file = norm_file(
os.path.join(os.path.dirname(__file__),
'db', 'migrate_new.sqlite'))
self.table_name = 'unnamed'
MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS %s (key TEXT PRIMARY KEY, value BLOB)' % self.table_name
self.conn_old = sqlitedict.SqliteMultithread(self.old_file, autocommit=True, journal_mode="DELETE")
self.conn_old.execute(MAKE_TABLE)
self.conn_old.commit()
self.conn_new = sqlitedict.SqliteMultithread(self.new_file, autocommit=True, journal_mode="DELETE")
self.conn_new.execute(MAKE_TABLE)
self.conn_new.commit()
def test_terminate_instead_close(self):
''' make terminate() instead of close()
'''
d = sqlitedict.open('tests/db/sqlitedict-terminate.sqlite')
d['abc'] = 'def'
d.commit()
self.assertEqual(d['abc'], 'def')
d.terminate()
self.assertFalse(os.path.isfile('tests/db/sqlitedict-terminate.sqlite'))
def test_terminate_instead_close(self):
''' make terminate() instead of close()
'''
d = sqlitedict.open('tests/db/sqlitedict-terminate.sqlite')
d['abc'] = 'def'
d.commit()
self.assertEqual(d['abc'], 'def')
d.terminate()
self.assertFalse(os.path.isfile('tests/db/sqlitedict-terminate.sqlite'))
def assertIterationDataRecorded(self, expected, tolerance, root):
if self.comm.rank != 0:
return
db = SqliteDict(self.filename, self.tablename_iterations)
_assertIterationDataRecorded(self, db, expected, tolerance)
db.close()
def test_migrate(self):
# adding manually items with plain text keys
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
# migrating the DB
sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
# checking migrated DB via sqlitedict
d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
self.assertEqual(d[b"abcd"], u"abcd")
self.assertEqual(d[b"xyz"], 24)
# jquast: please note, previously when db[u"unicode"] was stored, it
# was returned as utf-8 encoded bytes. Going forward, it will store
# as u"unicode", so any dependent code must make its own further
# migration to re-encode all key-as-bytes as key-as-unicode.
self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
d.terminate()
def test_migrate(self):
# adding manually items with plain text keys
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
# migrating the DB
sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
# checking migrated DB via sqlitedict
d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
self.assertEqual(d[b"abcd"], u"abcd")
self.assertEqual(d[b"xyz"], 24)
# jquast: please note, previously when db[u"unicode"] was stored, it
# was returned as utf-8 encoded bytes. Going forward, it will store
# as u"unicode", so any dependent code must make its own further
# migration to re-encode all key-as-bytes as key-as-unicode.
self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
d.terminate()
def setUp(self):
# create database of v1.2 values
self.filename = norm_file(
os.path.join(os.path.dirname(__file__),
'db', 'unmigrated.sqlite'))
self.db = sqlitedict.SqliteDict(filename=self.filename, flag='n')
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.db.tablename
keyval = (b"bytes", sqlitedict.encode(('some', 'value')))
self.db.conn.execute(ADD_ITEM, keyval)
def test_migrate(self):
# adding manually items with plain text keys
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"abcd", sqlitedict.encode(u"abcd")))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (b"xyz", sqlitedict.encode(24)))
ADD_ITEM = 'REPLACE INTO %s (key, value) VALUES (?,?)' % self.table_name
self.conn_old.execute(ADD_ITEM, (u"+ěščřžýáíé%123456789úů".encode('utf8'), sqlitedict.encode(u"special")))
# migrating the DB
sqlite_migration.migrate(self.table_name, self.conn_old, self.conn_new)
# checking migrated DB via sqlitedict
d = sqlitedict.SqliteDict(filename=self.new_file, tablename=self.table_name, autocommit=True)
self.assertEqual(d[b"abcd"], u"abcd")
self.assertEqual(d[b"xyz"], 24)
# jquast: please note, previously when db[u"unicode"] was stored, it
# was returned as utf-8 encoded bytes. Going forward, it will store
# as u"unicode", so any dependent code must make its own further
# migration to re-encode all key-as-bytes as key-as-unicode.
self.assertEqual(d[u"+ěščřžýáíé%123456789úů".encode('utf8')], u"special")
d.terminate()
def test_tablenames(self):
fname = norm_file('tests/db/tablenames-test-1.sqlite')
SqliteDict(fname)
self.assertEqual(SqliteDict.get_tablenames(fname), ['unnamed'])
fname = norm_file('tests/db/tablenames-test-2.sqlite')
with SqliteDict(fname,tablename='table1') as db1:
self.assertEqual(SqliteDict.get_tablenames(fname), ['table1'])
with SqliteDict(fname,tablename='table2') as db2:
self.assertEqual(SqliteDict.get_tablenames(fname), ['table1','table2'])
tablenames = SqliteDict.get_tablenames('tests/db/tablenames-test-2.sqlite')
self.assertEqual(tablenames, ['table1','table2'])