def test_keyfs_sqlite(gentmp): from devpi_server import keyfs_sqlite tmp = gentmp() keyfs = KeyFS(tmp, keyfs_sqlite.Storage) with keyfs.transaction(write=True) as tx: assert tx.conn.io_file_os_path('foo') is None tx.conn.io_file_set('foo', b'bar') tx.conn._sqlconn.commit() with keyfs.transaction(write=False) as tx: assert tx.conn.io_file_os_path('foo') is None assert tx.conn.io_file_get('foo') == b'bar' assert [x.basename for x in tmp.listdir()] == ['.sqlite_db']
def test_keyfs_sqlite_fs(gentmp): from devpi_server import keyfs_sqlite_fs tmp = gentmp() keyfs = KeyFS(tmp, keyfs_sqlite_fs.Storage) with keyfs.transaction(write=True) as tx: assert tx.conn.io_file_os_path('foo') == tmp.join('foo').strpath tx.conn.io_file_set('foo', b'bar') tx.conn._sqlconn.commit() with keyfs.transaction(write=False) as tx: assert tx.conn.io_file_get('foo') == b'bar' with open(tx.conn.io_file_os_path('foo'), 'rb') as f: assert f.read() == b'bar' assert sorted(x.basename for x in tmp.listdir()) == ['.sqlite', 'foo']
def test_keyfs_readonly(self, storage, tmpdir): keyfs = KeyFS(tmpdir, storage, readonly=True) with pytest.raises(keyfs.ReadOnly): with keyfs.transaction(write=True): pass assert not hasattr(keyfs, "tx") with pytest.raises(keyfs.ReadOnly): with keyfs.transaction(): keyfs.restart_as_write_transaction() with pytest.raises(keyfs.ReadOnly): keyfs.begin_transaction_in_thread(write=True) with keyfs.transaction(): pass
def test_import_changes(self, keyfs, storage, tmpdir): D = keyfs.add_key("NAME", "hello", dict) with keyfs.transaction(write=True): D.set({1:1}) with keyfs.transaction(write=True): D.delete() with keyfs.transaction(write=True): D.set({2:2}) with keyfs._storage.get_connection() as conn: serial = conn.last_changelog_serial assert serial == 2 # load entries into new keyfs instance new_keyfs = KeyFS(tmpdir.join("newkeyfs"), storage) D2 = new_keyfs.add_key("NAME", "hello", dict) for serial in range(3): with keyfs.transaction() as tx: changes = tx.conn.get_changes(serial) new_keyfs.import_changes(serial, changes) with new_keyfs.transaction() as tx: assert tx.get_value_at(D2, 0) == {1:1} with pytest.raises(KeyError): assert tx.get_value_at(D2, 1) assert tx.get_value_at(D2, 2) == {2:2}