Skip to content

Commit

Permalink
Fix: failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidberenstein1957 committed Jun 18, 2024
1 parent 24259c4 commit 31ddcd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
12 changes: 8 additions & 4 deletions haystack/document_stores/in_memory/document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,16 @@ def load_from_disk(cls, path: str) -> "InMemoryDocumentStore":
try:
with open(path, "r") as f:
data = json.load(f)
documents = data.pop("documents")
cls_object = default_from_dict(cls, data)
cls_object.write_documents(documents=[Document(**doc) for doc in documents])
return cls_object
except Exception as e:
raise Exception(f"Error loading InMemoryDocumentStore from disk. error: {e}")

documents = data.pop("documents")
cls_object = default_from_dict(cls, data)
cls_object.write_documents(
documents=[Document(**doc) for doc in documents], policy=DuplicatePolicy.OVERWRITE
)
return cls_object

else:
raise FileNotFoundError(f"File {path} not found.")

Expand Down
17 changes: 10 additions & 7 deletions test/document_stores/test_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class TestMemoryDocumentStore(DocumentStoreBaseTests): # pylint: disable=R0904
Test InMemoryDocumentStore's specific features
"""

@pytest.fixture
def tmp_dir(self):
with tempfile.TemporaryDirectory() as tmp_dir:
yield tmp_dir

@pytest.fixture
def document_store(self) -> InMemoryDocumentStore:
return InMemoryDocumentStore(bm25_algorithm="BM25L")
Expand Down Expand Up @@ -75,18 +80,16 @@ def test_from_dict(self, mock_regex):
assert store.bm25_parameters == {"key": "value"}
assert store.index == "my_cool_index"

def test_save_to_disk_and_load_from_disk(self, document_store: InMemoryDocumentStore):
def test_save_to_disk_and_load_from_disk(self, tmp_dir: str):
docs = [Document(content="Hello world"), Document(content="Haystack supports multiple languages")]
document_store = InMemoryDocumentStore()
document_store.write_documents(docs)

with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir = tmp_dir + "/tmp.json"
document_store.save_to_disk(tmp_dir)
document_store_loaded = InMemoryDocumentStore.load_from_disk(tmp_dir)
tmp_dir = tmp_dir + "/document_store.json"
document_store.save_to_disk(tmp_dir)
document_store_loaded = InMemoryDocumentStore.load_from_disk(tmp_dir)

assert document_store_loaded.count_documents() == 2
assert document_store_loaded.storage.values() == docs
assert list(document_store_loaded.storage.values()) == docs
assert document_store_loaded.to_dict() == document_store.to_dict()

def test_invalid_bm25_algorithm(self):
Expand Down

0 comments on commit 31ddcd3

Please sign in to comment.