-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for Elasticsearch document store (#88)
- Loading branch information
Showing
2 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import tarfile | ||
import time | ||
import urllib.request | ||
from subprocess import Popen, PIPE, STDOUT | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def elasticsearch_dir(tmpdir_factory): | ||
return tmpdir_factory.mktemp('elasticsearch') | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def elasticsearch_fixture(elasticsearch_dir): | ||
thetarfile = "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1-linux-x86_64.tar.gz" | ||
ftpstream = urllib.request.urlopen(thetarfile) | ||
thetarfile = tarfile.open(fileobj=ftpstream, mode="r|gz") | ||
thetarfile.extractall(path=elasticsearch_dir) | ||
es_server = Popen([elasticsearch_dir / "elasticsearch-7.6.1/bin/elasticsearch"], stdout=PIPE, stderr=STDOUT) | ||
time.sleep(30) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
from time import sleep | ||
|
||
from haystack.database.elasticsearch import ElasticsearchDocumentStore | ||
from haystack.database.sql import SQLDocumentStore | ||
from haystack.indexing.io import write_documents_to_db | ||
|
||
|
||
def test_db_write_read(): | ||
def test_sql_write_read(): | ||
sql_document_store = SQLDocumentStore() | ||
write_documents_to_db(document_store=sql_document_store, document_dir="samples/docs") | ||
documents = sql_document_store.get_all_documents() | ||
assert len(documents) == 2 | ||
doc = sql_document_store.get_document_by_id("1") | ||
assert doc.id | ||
assert doc.text | ||
|
||
|
||
def test_elasticsearch_write_read(elasticsearch_fixture): | ||
document_store = ElasticsearchDocumentStore() | ||
write_documents_to_db(document_store=document_store, document_dir="samples/docs") | ||
sleep(2) # wait for documents to be available for query | ||
documents = document_store.get_all_documents() | ||
assert len(documents) == 2 | ||
assert documents[0].id | ||
assert documents[0].text |