Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async and sync index_with_documents fixtures #4

Merged
merged 1 commit into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 52 additions & 30 deletions pytest_meilisearch/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
from pytest_meilisearch._internal import determine_clear_indexes, determine_client_scope


@pytest.fixture(scope="session")
def meilisearch_url(pytestconfig):
return (
f"{pytestconfig.getvalue('meilisearch_host')}:{pytestconfig.getvalue('meilisearch_port')}"
)
@pytest.fixture(autouse=True)
async def async_clear_indexes(async_client, request):
"""Asyncronously clears all indexes."""

yield
if determine_clear_indexes(request.config) == "async":
indexes = await async_client.get_indexes()
if indexes:
tasks = await asyncio.gather(*[async_client.index(x.uid).delete() for x in indexes])
await asyncio.gather(*[async_client.wait_for_task(x.task_uid) for x in tasks])


@pytest.fixture(scope=determine_client_scope) # type: ignore
Expand All @@ -23,12 +28,6 @@ async def async_client(pytestconfig, meilisearch_url):
yield client


@pytest.fixture(scope=determine_client_scope) # type: ignore
def client(pytestconfig, meilisearch_url):
"""Creates a meilisearch_python_sdk.Client for testing."""
yield Client(meilisearch_url, pytestconfig.getvalue("meilisearch_master_key"))


@pytest.fixture
async def async_empty_index(async_client):
"""Create an empty meilisearch_python_sdk.AsyncIndex.
Expand All @@ -42,6 +41,35 @@ async def index_maker(uid=str(uuid4())):
return index_maker


@pytest.fixture
async def async_index_with_documents(async_client, async_empty_index):
async def index_maker(documents):
index = await async_empty_index()
response = await index.add_documents(documents)
await async_client.wait_for_task(response.task_uid)
return index

return index_maker


@pytest.fixture(autouse=True)
def clear_indexes(client, request):
"""Clears all indexes."""

yield
if determine_clear_indexes(request.config) == "sync":
indexes = client.get_indexes()
if indexes:
for index in indexes:
client.index(index.uid).delete()


@pytest.fixture(scope=determine_client_scope) # type: ignore
def client(pytestconfig, meilisearch_url):
"""Creates a meilisearch_python_sdk.Client for testing."""
yield Client(meilisearch_url, pytestconfig.getvalue("meilisearch_master_key"))


@pytest.fixture
def empty_index(client):
"""Create an empty meilisearch_python_sdk.Index.
Expand All @@ -55,25 +83,19 @@ def index_maker(uid=str(uuid4())):
return index_maker


@pytest.fixture(autouse=True)
async def async_clear_indexes(async_client, request):
"""Asyncronously clears all indexes."""

yield
if determine_clear_indexes(request.config) == "async":
indexes = await async_client.get_indexes()
if indexes:
tasks = await asyncio.gather(*[async_client.index(x.uid).delete() for x in indexes])
await asyncio.gather(*[async_client.wait_for_task(x.task_uid) for x in tasks])
@pytest.fixture
def index_with_documents(client, empty_index):
def index_maker(documents):
index = empty_index()
response = index.add_documents(documents)
client.wait_for_task(response.task_uid)
return index

return index_maker

@pytest.fixture(autouse=True)
def clear_indexes(client, request):
"""Clears all indexes."""

yield
if determine_clear_indexes(request.config) == "sync":
indexes = client.get_indexes()
if indexes:
for index in indexes:
client.index(index.uid).delete()
@pytest.fixture(scope="session")
def meilisearch_url(pytestconfig):
return (
f"{pytestconfig.getvalue('meilisearch_host')}:{pytestconfig.getvalue('meilisearch_port')}"
)
2 changes: 2 additions & 0 deletions pytest_meilisearch/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
async_clear_indexes,
async_client,
async_empty_index,
async_index_with_documents,
clear_indexes,
client,
empty_index,
index_with_documents,
meilisearch_url,
)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ async def test_async_empty_index(async_empty_index):
assert index.uid == uid


async def test_async_index_with_documents(async_index_with_documents):
docs = [{"id": 1, "title": "Title 1"}, {"id": 2, "title": "Title 2"}]
index = await async_index_with_documents(docs)
result = await index.get_documents()
assert len(result.results) == 2
titles = [x["title"] for x in result.results]
assert docs[0]["title"] in titles
assert docs[1]["title"] in titles


def test_client(client):
result = client.health()
assert result.status == "available"
Expand All @@ -21,3 +31,13 @@ def test_empty_index(empty_index):
uid = str(uuid4())
index = empty_index(uid)
assert index.uid == uid


def test_index_with_documents(index_with_documents):
docs = [{"id": 1, "title": "Title 1"}, {"id": 2, "title": "Title 2"}]
index = index_with_documents(docs)
result = index.get_documents()
assert len(result.results) == 2
titles = [x["title"] for x in result.results]
assert docs[0]["title"] in titles
assert docs[1]["title"] in titles