2023-08-09 13:09:36 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from haystack.preview.dataclasses import Document
|
2023-08-12 08:44:36 +02:00
|
|
|
from haystack.preview.testing.factory import document_store_class
|
|
|
|
from haystack.preview.document_stores.decorator import document_store
|
2023-08-09 13:09:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_default():
|
|
|
|
MyStore = document_store_class("MyStore")
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert store.count_documents() == 0
|
|
|
|
assert store.filter_documents() == []
|
|
|
|
assert store.write_documents([]) is None
|
|
|
|
assert store.delete_documents([]) is None
|
2023-11-17 13:46:23 +00:00
|
|
|
assert store.to_dict() == {"type": "haystack.preview.testing.factory.MyStore", "init_parameters": {}}
|
2023-08-29 18:15:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
|
|
|
def test_document_store_from_dict():
|
|
|
|
MyStore = document_store_class("MyStore")
|
|
|
|
|
2023-11-17 13:46:23 +00:00
|
|
|
store = MyStore.from_dict({"type": "haystack.preview.testing.factory.MyStore", "init_parameters": {}})
|
2023-08-29 18:15:07 +02:00
|
|
|
assert isinstance(store, MyStore)
|
2023-08-09 13:09:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_is_registered():
|
|
|
|
MyStore = document_store_class("MyStore")
|
2023-11-17 13:46:23 +00:00
|
|
|
assert document_store.registry["haystack.preview.testing.factory.MyStore"] == MyStore
|
2023-08-09 13:09:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_with_documents():
|
2023-10-31 12:44:04 +01:00
|
|
|
doc = Document(id="fake_id", content="This is a document")
|
2023-08-12 08:44:36 +02:00
|
|
|
MyStore = document_store_class("MyStore", documents=[doc])
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert store.count_documents() == 1
|
|
|
|
assert store.filter_documents() == [doc]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_with_documents_count():
|
|
|
|
MyStore = document_store_class("MyStore", documents_count=100)
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert store.count_documents() == 100
|
|
|
|
assert store.filter_documents() == []
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_with_documents_and_documents_count():
|
2023-10-31 12:44:04 +01:00
|
|
|
doc = Document(id="fake_id", content="This is a document")
|
2023-08-12 08:44:36 +02:00
|
|
|
MyStore = document_store_class("MyStore", documents=[doc], documents_count=100)
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert store.count_documents() == 100
|
|
|
|
assert store.filter_documents() == [doc]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_with_bases():
|
|
|
|
MyStore = document_store_class("MyStore", bases=(Exception,))
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert isinstance(store, Exception)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.unit
|
2023-08-12 08:44:36 +02:00
|
|
|
def test_document_store_class_with_extra_fields():
|
|
|
|
MyStore = document_store_class("MyStore", extra_fields={"my_field": 10})
|
2023-08-09 13:09:36 +02:00
|
|
|
store = MyStore()
|
|
|
|
assert store.my_field == 10
|