Skip to content

Commit

Permalink
Merge pull request #2 from sanders41/fixtures
Browse files Browse the repository at this point in the history
Add fixtures
  • Loading branch information
sanders41 authored Oct 28, 2023
2 parents 0923b93 + 2977852 commit 0b26516
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 48 deletions.
79 changes: 40 additions & 39 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,54 @@ name: Testing
on:
push:
branches:
- main
- main
pull_request:
jobs:
linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Poetry
run: pipx install poetry
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.8"
cache: "poetry"
- name: Install Dependencies
run: poetry install
- name: Ruff format check
run: poetry run ruff format pytest_meilisearch tests --check
- name: Lint with ruff
run: poetry run ruff check .
- name: mypy check
run: poetry run mypy .
- uses: actions/checkout@v4
- name: Install Poetry
run: pipx install poetry
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.8"
cache: "poetry"
- name: Install Dependencies
run: poetry install
- name: Ruff format check
run: poetry run ruff format pytest_meilisearch tests --check
- name: Lint with ruff
run: poetry run ruff check .
- name: mypy check
run: poetry run mypy .
testing:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Poetry
run: pipx install poetry
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install Dependencies
run: poetry install
- name: Test with pytest
run: poetry run pytest
- uses: actions/checkout@v4
- name: Install Poetry
run: pipx install poetry
- name: Configure poetry
run: |
poetry config virtualenvs.create true
poetry config virtualenvs.in-project true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Install Dependencies
run: poetry install
- name: MeiliSearch setup with Docker
run: docker run -d --rm -p 7700:7700 getmeili/meilisearch:latest meilisearch --no-analytics --master-key=masterKey
- name: Test with pytest
run: poetry run pytest
32 changes: 25 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ pre-commit = ">=3.5.0"
pytest-cov = ">=4.1.0"
ruff = ">=0.1.3"
tomli = {version = ">=2.0.1", python = "<3.11"}
pytest-asyncio = ">=0.21.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.plugins."pytest11"]
"meilisearch" = "pytest_meilisearch.plugin"

[tool.mypy]
check_untyped_defs = true
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = ["tests.*"]
disallow_untyped_defs = false

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--cov=pytest_meilisearch --cov-report term-missing --no-cov-on-fail"
asyncio_mode = "auto"
meilisearch_clear_indexes = "async"

[tool.coverage.report]
exclude_lines = ["if __name__ == .__main__.:", "pragma: no cover"]
Expand Down
16 changes: 16 additions & 0 deletions pytest_meilisearch/_internal.py
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
79 changes: 79 additions & 0 deletions pytest_meilisearch/fixtures.py
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()
44 changes: 44 additions & 0 deletions pytest_meilisearch/plugin.py
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",
)
13 changes: 13 additions & 0 deletions tests/conftest.py
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()
23 changes: 23 additions & 0 deletions tests/test_fixtures.py
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

0 comments on commit 0b26516

Please sign in to comment.