-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from sanders41/fixtures
Add fixtures
- Loading branch information
Showing
8 changed files
with
246 additions
and
48 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,16 @@ | ||
from functools import lru_cache | ||
|
||
|
||
def determine_client_scope(*, fixture_name, config): | ||
return config.getini("meilisearch_client_scope") | ||
|
||
|
||
@lru_cache | ||
def determine_clear_indexes(config): | ||
clear = config.getini("meilisearch_clear_indexes").lower() | ||
valid = ("none", "async", "sync") | ||
if clear not in valid: | ||
raise ValueError( | ||
f"'{clear}' is not a valid meilisearch_clear_indexes value. Valid values: none, async, or sync" | ||
) | ||
return clear |
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,79 @@ | ||
import asyncio | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
from meilisearch_python_sdk import AsyncClient, Client | ||
|
||
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(scope=determine_client_scope) # type: ignore | ||
async def async_client(pytestconfig, meilisearch_url): | ||
"""Creates a meilisearch_python_sdk.AsyncClient for asyncronous testing.""" | ||
async with AsyncClient( | ||
meilisearch_url, pytestconfig.getvalue("meilisearch_master_key") | ||
) as client: | ||
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. | ||
A name for the index uid can be passed in. By default the id will be created with a uuid | ||
""" | ||
|
||
async def index_maker(uid=str(uuid4())): | ||
return await async_client.create_index(uid=uid) | ||
|
||
return index_maker | ||
|
||
|
||
@pytest.fixture | ||
def empty_index(client): | ||
"""Create an empty meilisearch_python_sdk.Index. | ||
A name for the index uid can be passed in. By default the id will be created with a uuid | ||
""" | ||
|
||
def index_maker(uid=str(uuid4())): | ||
return client.create_index(uid=uid) | ||
|
||
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(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() |
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,44 @@ | ||
from pytest_meilisearch.fixtures import ( # noqa: F401 | ||
async_clear_indexes, | ||
async_client, | ||
async_empty_index, | ||
clear_indexes, | ||
client, | ||
empty_index, | ||
meilisearch_url, | ||
) | ||
|
||
|
||
def pytest_addoption(parser): | ||
group = parser.getgroup("meilisearch") | ||
group.addoption( | ||
"--meilisearch-host", | ||
action="store", | ||
default="http://127.0.0.1", | ||
type=str, | ||
help="The host where Meilisearch is running.", | ||
) | ||
group.addoption( | ||
"--meilisearch-port", | ||
action="store", | ||
default=7700, | ||
type=int, | ||
help="The port Meilisearch is running on.", | ||
) | ||
group.addoption( | ||
"--meilisearch-master-key", | ||
action="store", | ||
default="masterKey", | ||
type=str, | ||
help="The master key for Meilisearch.", | ||
) | ||
parser.addini( | ||
"meilisearch_client_scope", | ||
"Modify the scope of the async_client and client fixtures.", | ||
default="session", | ||
) | ||
parser.addini( | ||
"meilisearch_clear_indexes", | ||
"Modify the autouse setting for the async_clear_indexes and clear_indexes fixtures.", | ||
default="none", | ||
) |
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,13 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def event_loop(): | ||
try: | ||
loop = asyncio.get_running_loop() | ||
except RuntimeError: | ||
loop = asyncio.new_event_loop() | ||
yield loop | ||
loop.close() |
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,23 @@ | ||
from uuid import uuid4 | ||
|
||
|
||
async def test_async_client(async_client): | ||
result = await async_client.health() | ||
assert result.status == "available" | ||
|
||
|
||
async def test_async_empty_index(async_empty_index): | ||
uid = str(uuid4()) | ||
index = await async_empty_index(uid) | ||
assert index.uid == uid | ||
|
||
|
||
def test_client(client): | ||
result = client.health() | ||
assert result.status == "available" | ||
|
||
|
||
def test_empty_index(empty_index): | ||
uid = str(uuid4()) | ||
index = empty_index(uid) | ||
assert index.uid == uid |