OpenHands/tests/unit/test_storage.py
Robert Brennan dcb5d1ce0a
Add permanent storage option for EventStream (#1697)
* add storage classes

* add minio

* add event stream storage

* storage test working

* use fixture

* event stream test passing

* better serialization

* factor out serialization pkg

* move more serialization

* fix tests

* fix test

* remove __all__

* add rehydration test

* add more rehydration test

* fix fixture

* fix dict init

* update tests

* lock

* regenerate tests

* Update opendevin/events/stream.py

* revert tests

* revert old integration tests

* only add fields if present

* regen tests

* pin pyarrow

* fix unit tests

* remove cause from memories

* revert tests

* regen tests
2024-05-14 11:09:45 -04:00

68 lines
2.3 KiB
Python

import os
import shutil
import pytest
from opendevin.storage.local import LocalFileStore
from opendevin.storage.memory import InMemoryFileStore
@pytest.fixture
def setup_env():
os.makedirs('./_test_files_tmp', exist_ok=True)
yield
shutil.rmtree('./_test_files_tmp')
def test_basic_fileops(setup_env):
filename = 'test.txt'
for store in [LocalFileStore('./_test_files_tmp'), InMemoryFileStore()]:
store.write(filename, 'Hello, world!')
assert store.read(filename) == 'Hello, world!'
assert store.list('') == [filename]
store.delete(filename)
with pytest.raises(FileNotFoundError):
store.read(filename)
def test_complex_path_fileops(setup_env):
filenames = ['foo.bar.baz', './foo/bar/baz', 'foo/bar/baz', '/foo/bar/baz']
for store in [LocalFileStore('./_test_files_tmp'), InMemoryFileStore()]:
for filename in filenames:
store.write(filename, 'Hello, world!')
assert store.read(filename) == 'Hello, world!'
store.delete(filename)
with pytest.raises(FileNotFoundError):
store.read(filename)
def test_list(setup_env):
for store in [LocalFileStore('./_test_files_tmp'), InMemoryFileStore()]:
store.write('foo.txt', 'Hello, world!')
store.write('bar.txt', 'Hello, world!')
store.write('baz.txt', 'Hello, world!')
assert store.list('').sort() == ['foo.txt', 'bar.txt', 'baz.txt'].sort()
store.delete('foo.txt')
store.delete('bar.txt')
store.delete('baz.txt')
def test_deep_list(setup_env):
for store in [LocalFileStore('./_test_files_tmp'), InMemoryFileStore()]:
store.write('foo/bar/baz.txt', 'Hello, world!')
store.write('foo/bar/qux.txt', 'Hello, world!')
store.write('foo/bar/quux.txt', 'Hello, world!')
assert store.list('') == ['foo'], 'Expected foo, got {} for class {}'.format(
store.list(''), store.__class__
)
assert store.list('foo') == ['foo/bar']
assert (
store.list('foo/bar').sort()
== ['foo/bar/baz.txt', 'foo/bar/qux.txt', 'foo/bar/quux.txt'].sort()
)
store.delete('foo/bar/baz.txt')
store.delete('foo/bar/qux.txt')
store.delete('foo/bar/quux.txt')