Skip to content
Open
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
14 changes: 14 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# Change log

## v0.3.1

- Reuse mongod session between tests for performance improvements
- Drops database instead of
- Generic tmp folder
- Add support for async fixtures

## v0.3.0

- Add support for Windows, Debian, Ubuntu 16.04, and Ubuntu 20.04.
- Improve the readme.
- Add section about how `pytest-motor` works.
- Add installation instructions.

## v0.2.0

- Add MacOS support.
- Change the mongod binary download to be async.
- Change mongod connection from using unix sockets to ports.
Expand All @@ -15,21 +24,26 @@
- Add MIT license.

## v0.1.4

- Change to async fixtures.
- Fix package URL.
- Refactor internal fixtures.

## v0.1.3

- Improve the readme.
- Add an example.
- Add a link to pytest.
- Add a link to Motor.

## v0.1.2

- Add more package classifiers.

## v0.1.1

- Fix package description.

## v0.1.0

Initial release.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.3.1
10 changes: 7 additions & 3 deletions pytest_motor/mongod_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def current_platform(self) -> str:
distro_name = distro.id()
if distro_name == 'ubuntu':
return 'linux-x86_64-' + MongodBinary.__select_ubuntu_version()
if distro_name == 'debian':
elif distro_name == 'debian':
return 'linux-x86_64-' + MongodBinary.__select_debian_version()
else:
return "linux-x86_64-" + "ubuntu1804"
# FUTURE: distros that may be added: Amazon Linux, CentOS, SUSE
raise OSError(f"Your distro ({distro_name}) is unsupported.")

Expand Down Expand Up @@ -135,12 +137,14 @@ def __select_debian_version() -> str:
raise OSError(
"Your Debian version is too old. Upgrade at least to 9.") # pragma: no cover

if distro.version() == '9.2':
if distro.version().startswith('9'):
MongodBinary.warn_untested_os()
return 'debian92'
if distro.version() == '10.0':
if distro.version().startswith('10'):
MongodBinary.warn_untested_os()
return 'debian10'
if distro.version().startswith('11'):
return 'ubuntu1804'

warnings.warn("Can't detect your Debian version. Fallback to 9.2.")
return 'debian92'
Expand Down
46 changes: 27 additions & 19 deletions pytest_motor/plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""A pytest plugin which helps test applications using Motor."""
import asyncio
import socket
import tempfile
from pathlib import Path
from typing import AsyncIterator, Iterator, List

import pytest
import pytest_asyncio
from _pytest.config import Config as PytestConfig
from motor.motor_asyncio import AsyncIOMotorClient

Expand All @@ -26,17 +28,13 @@ def _event_loop() -> Iterator[asyncio.AbstractEventLoop]:
event_loop = pytest.fixture(fixture_function=_event_loop, scope='session', name="event_loop")


async def _root_directory(pytestconfig: PytestConfig) -> Path:
@pytest_asyncio.fixture(scope='session')
async def root_directory(pytestconfig: PytestConfig) -> Path:
"""Return the root path of pytest."""
return pytestconfig.rootpath


root_directory = pytest.fixture(fixture_function=_root_directory,
scope='session',
name='root_directory')


@pytest.fixture(scope='session')
@pytest_asyncio.fixture(scope='session')
async def mongod_binary(root_directory: Path) -> Path:
# pylint: disable=redefined-outer-name
"""Return a path to a mongod binary."""
Expand All @@ -47,27 +45,24 @@ async def mongod_binary(root_directory: Path) -> Path:
return binary.path


@pytest.fixture(scope='function')
@pytest.fixture(scope='session')
def new_port() -> int:
"""Return an unused port for mongod to run on."""
port: int = 27017
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as opened_socket:
opened_socket.bind(('127.0.0.1', 0)) # system will automaticly assign port
opened_socket.bind(("127.0.0.1", 0)) # system will automaticly assign port
port = opened_socket.getsockname()[1]
return port


async def _database_path(tmp_path: Path) -> AsyncIterator[Path]:
@pytest.fixture(scope="session")
def database_path() -> Iterator[str]:
"""Yield a database path for a mongod process to store data."""
yield tmp_path


database_path = pytest.fixture(fixture_function=_database_path,
scope='function',
name='database_path')
with tempfile.TemporaryDirectory() as tmpdirname:
yield tmpdirname


@pytest.fixture(scope='function')
@pytest_asyncio.fixture(scope='session')
async def mongod_socket(new_port: int, database_path: Path,
mongod_binary: Path) -> AsyncIterator[str]:
# pylint: disable=redefined-outer-name
Expand All @@ -93,8 +88,8 @@ async def mongod_socket(new_port: int, database_path: Path,
pass


@pytest.fixture(scope='function')
async def motor_client(mongod_socket: str) -> AsyncIterator[AsyncIOMotorClient]:
@pytest.fixture(scope="session")
def __motor_client(mongod_socket: str) -> AsyncIterator[AsyncIOMotorClient]:
# pylint: disable=redefined-outer-name
"""Yield a Motor client."""
connection_string = f'mongodb://{mongod_socket}'
Expand All @@ -105,3 +100,16 @@ async def motor_client(mongod_socket: str) -> AsyncIterator[AsyncIOMotorClient]:
yield motor_client_

motor_client_.close()


@pytest_asyncio.fixture(scope='function')
async def motor_client(__motor_client: AsyncIterator[AsyncIOMotorClient]) -> AsyncIterator[AsyncIOMotorClient]:
# pylint: disable=redefined-outer-name
"""Yield a Motor client."""
yield __motor_client

dbs = await __motor_client.list_database_names()

for db in dbs:
if db not in ["config", "admin", "local"]:
await __motor_client.drop_database(db)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
'Typing :: Typed',
],
python_requires='>=3.6',
install_requires=['pytest', 'motor', 'aiohttp[speedups]', 'distro'],
install_requires=['pytest', 'pytest-asyncio', 'motor', 'aiohttp[speedups]', 'distro'],
)