tests: Add some tests that use a tmpdir to store the index.

master
Damir Jelić 2019-10-01 18:50:09 +02:00
parent efbbd24baa
commit 6aa3e904f8
1 changed files with 32 additions and 5 deletions

View File

@ -7,12 +7,10 @@ from tantivy import Document, Index, SchemaBuilder, Schema
def schema(): def schema():
return SchemaBuilder().add_text_field("title", stored=True).add_text_field("body").build() return SchemaBuilder().add_text_field("title", stored=True).add_text_field("body").build()
def create_index(dir=None):
@pytest.fixture(scope="class")
def ram_index():
# assume all tests will use the same documents for now # assume all tests will use the same documents for now
# other methods may set up function-local indexes # other methods may set up function-local indexes
index = Index(schema()) index = Index(schema(), dir)
writer = index.writer() writer = index.writer()
# 2 ways of adding documents # 2 ways of adding documents
@ -63,8 +61,37 @@ def ram_index():
return index return index
@pytest.fixture()
def dir_index(tmpdir):
return (tmpdir, create_index(str(tmpdir)))
@pytest.fixture(scope="class")
def ram_index():
return create_index()
class TestClass(object): class TestClass(object):
def test_simple_search(self, ram_index): def test_simple_search_in_dir(self, dir_index):
_, index = dir_index
query = index.parse_query("sea whale", ["title", "body"])
top_docs = tantivy.TopDocs(10)
result = index.searcher().search(query, top_docs)
assert len(result) == 1
def test_simple_search_after_reuse(self, dir_index):
index_dir, _ = dir_index
index = Index(schema(), str(index_dir))
query = index.parse_query("sea whale", ["title", "body"])
top_docs = tantivy.TopDocs(10)
result = index.searcher().search(query, top_docs)
assert len(result) == 1
def test_simple_search_in_ram(self, ram_index):
index = ram_index index = ram_index
query = index.parse_query("sea whale", ["title", "body"]) query = index.parse_query("sea whale", ["title", "body"])