Skip to content

Commit

Permalink
Merge pull request #2804 from bagerard/remove_setup_py_test
Browse files Browse the repository at this point in the history
Remove 'python setup.py test' usage in tox
  • Loading branch information
bagerard committed Mar 10, 2024
2 parents ce9e108 + bd58f95 commit 9e3f1d9
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 126 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ env:
PYMONGO_3_11: 3.11
PYMONGO_3_12: 3.12
PYMONGO_4_0: 4.0
PYMONGO_4_3: 4.3.2
PYMONGO_4_3: 4.3.3
PYMONGO_4_4: 4.4.1
PYMONGO_4_6: 4.6.0
PYMONGO_4_6: 4.6.2

MAIN_PYTHON_VERSION: 3.7
MAIN_PYTHON_VERSION: 3.9

jobs:
linting:
Expand All @@ -53,7 +53,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8, 3.9, "3.10", 3.11, "pypy3.9"]
python-version: [3.7, 3.8, 3.9, "3.10", 3.11, pypy3.9, pypy3.10]
MONGODB: [$MONGODB_4_0]
PYMONGO: [$PYMONGO_3_11]
include:
Expand Down Expand Up @@ -91,9 +91,9 @@ jobs:
bash .github/workflows/install_ci_python_dep.sh
bash .github/workflows/start_mongo.sh ${{ matrix.MONGODB }}
- name: tox dry-run (to pre-install venv)
run: tox -e $(echo py${{ matrix.python-version }}-mg${{ matrix.PYMONGO }} | tr -d . | sed -e 's/pypypy/pypy/') -- -a "-k=test_ci_placeholder"
run: tox -e $(echo py${{ matrix.python-version }}-mg${{ matrix.PYMONGO }} | tr -d . | sed -e 's/pypypy/pypy/') -- "-k=test_ci_placeholder"
- name: Run test suite
run: tox -e $(echo py${{ matrix.python-version }}-mg${{ matrix.PYMONGO }} | tr -d . | sed -e 's/pypypy/pypy/') -- -a "--cov=mongoengine"
run: tox -e $(echo py${{ matrix.python-version }}-mg${{ matrix.PYMONGO }} | tr -d . | sed -e 's/pypypy/pypy/') -- "--cov=mongoengine"
- name: Send coverage to Coveralls
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
fail_fast: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: debug-statements
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/ambv/black
rev: 23.9.1
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
- importlib_metadata<5
- repo: https://github.com/asottile/pyupgrade
rev: v3.14.0
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ General Guidelines
should adapt to the breaking change in docs/upgrade.rst.
- Write inline documentation for new classes and methods.
- Write tests and make sure they pass (make sure you have a mongod
running on the default port, then execute ``python setup.py test``
running on the default port, then execute ``pytest tests/``
from the cmd line to run the test suite).
- Ensure tests pass on all supported Python, PyMongo, and MongoDB versions.
You can test various Python and PyMongo versions locally by executing
Expand Down
12 changes: 1 addition & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ Some simple examples of what MongoEngine code looks like:
Tests
=====
To run the test suite, ensure you are running a local instance of MongoDB on
the standard port and have ``pytest`` installed. Then, run ``python setup.py test``
or simply ``pytest``.
the standard port and have ``pytest`` installed. Then, run ``pytest tests/``.

To run the test suite on every supported Python and PyMongo version, you can
use ``tox``. You'll need to make sure you have each supported Python version
Expand All @@ -139,15 +138,6 @@ installed in your environment and then:
# Run the test suites
$ tox
If you wish to run a subset of tests, use the pytest convention:

.. code-block:: shell
# Run all the tests in a particular test file
$ pytest tests/fields/test_fields.py
# Run only particular test class in that file
$ pytest tests/fields/test_fields.py::TestField
Community
=========
- `MongoEngine Users mailing list
Expand Down
14 changes: 4 additions & 10 deletions benchmarks/test_basic_doc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,9 @@ def save_book():
print("Save to database: %.3fus" % (timeit(save_book, 100) * 10**6))

son = b.to_mongo()
print(
"Load from SON: %.3fus" % (timeit(lambda: Book._from_son(son), 1000) * 10**6)
)
print("Load from SON: %.3fus" % (timeit(lambda: Book._from_son(son), 1000) * 10**6))

print(
"Load from database: %.3fus" % (timeit(lambda: Book.objects[0], 100) * 10**6)
)
print("Load from database: %.3fus" % (timeit(lambda: Book.objects[0], 100) * 10**6))

def create_and_delete_book():
b = init_book()
Expand Down Expand Up @@ -116,13 +112,11 @@ def save_company():

son = company.to_mongo()
print(
"Load from SON: %.3fms"
% (timeit(lambda: Company._from_son(son), 100) * 10**3)
"Load from SON: %.3fms" % (timeit(lambda: Company._from_son(son), 100) * 10**3)
)

print(
"Load from database: %.3fms"
% (timeit(lambda: Company.objects[0], 100) * 10**3)
"Load from database: %.3fms" % (timeit(lambda: Company.objects[0], 100) * 10**3)
)

def create_and_delete_company():
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Development
===========
- (Fill this out as you fix issues and develop your features).
- Switch tox to use pytest instead of legacy `python setup.py test` #2804

Changes in 0.28.2
=================
Expand Down
1 change: 1 addition & 0 deletions mongoengine/mongodb_support.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Helper functions, constants, and types to aid with MongoDB version support
"""

from mongoengine.connection import get_connection

# Constant that can be used to compare the version retrieved with
Expand Down
1 change: 1 addition & 0 deletions mongoengine/pymongo_support.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Helper functions, constants, and types to aid with PyMongo support.
"""

import pymongo
from bson import binary, json_util
from pymongo.errors import OperationFailure
Expand Down
6 changes: 3 additions & 3 deletions mongoengine/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def _fail(self, *args, **kwargs):
)

send = lambda *a, **kw: None # noqa
connect = (
disconnect
) = has_receivers_for = receivers_for = temporarily_connected_to = _fail
connect = disconnect = has_receivers_for = receivers_for = (
temporarily_connected_to
) = _fail
del _fail


Expand Down
4 changes: 1 addition & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
black
flake8
pre-commit
pytest
ipdb
ipython
tox
-e .[test]
97 changes: 13 additions & 84 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import os
import sys

from pkg_resources import normalize_path
from setuptools import find_packages, setup
from setuptools.command.test import test as TestCommand

# Hack to silence atexit traceback in newer python versions
try:
import multiprocessing # noqa: F401
except ImportError:
pass

DESCRIPTION = "MongoEngine is a Python Object-Document Mapper for working with MongoDB."

Expand All @@ -27,62 +18,6 @@ def get_version(version_tuple):
return ".".join(map(str, version_tuple))


class PyTest(TestCommand):
"""Will force pytest to search for tests inside the build directory
for 2to3 converted code (used by tox), instead of the current directory.
Required as long as we need 2to3
Known Limitation: https://tox.readthedocs.io/en/latest/example/pytest.html#known-issues-and-limitations
Source: https://www.hackzine.org/python-testing-with-pytest-and-2to3-plus-tox-and-travis-ci.html
"""

# https://pytest.readthedocs.io/en/2.7.3/goodpractises.html#integration-with-setuptools-test-commands
# Allows to provide pytest command argument through the test runner command `python setup.py test`
# e.g: `python setup.py test -a "-k=test"`
# This only works for 1 argument though
user_options = [("pytest-args=", "a", "Arguments to pass to py.test")]

def initialize_options(self):
TestCommand.initialize_options(self)
self.pytest_args = ""

def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = ["tests"]
self.test_suite = True

def run_tests(self):
# import here, cause outside the eggs aren't loaded
import pytest
from pkg_resources import _namespace_packages

# Purge modules under test from sys.modules. The test loader will
# re-import them from the build location. Required when 2to3 is used
# with namespace packages.
if sys.version_info >= (3,) and getattr(self.distribution, "use_2to3", False):
module = self.test_args[-1].split(".")[0]
if module in _namespace_packages:
del_modules = []
if module in sys.modules:
del_modules.append(module)
module += "."
for name in sys.modules:
if name.startswith(module):
del_modules.append(name)
map(sys.modules.__delitem__, del_modules)

# Run on the build directory for 2to3-built code
# This will prevent the old 2.x code from being found
# by py.test discovery mechanism, that apparently
# ignores sys.path..
ei_cmd = self.get_finalized_command("egg_info")
self.test_args = [normalize_path(ei_cmd.egg_base)]

cmd_args = self.test_args + ([self.pytest_args] if self.pytest_args else [])
errno = pytest.main(cmd_args)
sys.exit(errno)


# Dirty hack to get version number from monogengine/__init__.py - we can't
# import it as it depends on PyMongo and PyMongo isn't installed until this
# file is read
Expand All @@ -107,22 +42,14 @@ def run_tests(self):
"Topic :: Software Development :: Libraries :: Python Modules",
]

extra_opts = {
"packages": find_packages(exclude=["tests", "tests.*"]),
"tests_require": [
"pytest",
"pytest-cov",
"coverage",
"blinker",
"Pillow>=7.0.0",
],
}

if "test" in sys.argv:
extra_opts["packages"] = find_packages()
extra_opts["package_data"] = {
"tests": ["fields/mongoengine.png", "fields/mongodb_leaf.png"]
}
install_require = ["pymongo>=3.4,<5.0"]
tests_require = [
"pytest",
"pytest-cov",
"coverage",
"blinker",
"Pillow>=7.0.0",
]

setup(
name="mongoengine",
Expand All @@ -140,7 +67,9 @@ def run_tests(self):
platforms=["any"],
classifiers=CLASSIFIERS,
python_requires=">=3.7",
install_requires=["pymongo>=3.4,<5.0"],
cmdclass={"test": PyTest},
**extra_opts
install_requires=install_require,
extras_require={
"test": tests_require,
},
packages=find_packages(exclude=["tests", "tests.*"]),
)
1 change: 1 addition & 0 deletions tests/all_warnings/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
only get triggered on first hit. This way we can ensure its imported into the
top level and called first by the test suite.
"""

import unittest
import warnings

Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
[tox]
envlist = pypy3-{mg34,mg36,mg39,mg311,mg312,mg4,mg432,mg441,mg460}
skipsdist = True

[testenv]
commands =
python setup.py test {posargs}
pytest tests/ {posargs}
deps =
-rrequirements-dev.txt
mg39: pymongo>=3.9,<3.10
mg311: pymongo>=3.11,<3.12
mg312: pymongo>=3.12,<3.13
mg4: pymongo>=4.0,<4.1
mg432: pymongo>=4.3,<4.4
mg433: pymongo>=4.3,<4.4
mg441: pymongo>=4.4,<4.5
mg460: pymongo>=4.6,<4.7
mg462: pymongo>=4.6,<4.7
setenv =
PYTHON_EGG_CACHE = {envdir}/python-eggs

0 comments on commit 9e3f1d9

Please sign in to comment.