From 9e3b48e718a852d664c241e066f87d0c521e3b26 Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Sat, 9 Mar 2024 18:20:20 +0100 Subject: [PATCH 1/5] Remove 'python setup.py test' usage in tox --- .github/workflows/github-actions.yml | 10 +-- requirements-dev.txt | 4 +- setup.py | 97 ++++------------------------ tox.ini | 4 +- 4 files changed, 22 insertions(+), 93 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index 8446a1e84..e5d765470 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -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: @@ -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: @@ -110,7 +110,7 @@ jobs: - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: - python-version: 3.7 + python-version: 3.9 check-latest: true - name: install python dep run: | diff --git a/requirements-dev.txt b/requirements-dev.txt index f9d0595f1..aa3d3a1ec 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,7 +1,5 @@ -black -flake8 pre-commit -pytest ipdb ipython tox +-e .[test] diff --git a/setup.py b/setup.py index aebabc42e..25d2a1a19 100644 --- a/setup.py +++ b/setup.py @@ -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." @@ -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 @@ -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", @@ -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.*"]), ) diff --git a/tox.ini b/tox.ini index 361b7b98c..daf97ad3e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,12 @@ [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 From cc5a49134442275f768b0356f2f5f2ef26f1d73a Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Sat, 9 Mar 2024 18:23:37 +0100 Subject: [PATCH 2/5] rollback py in doc build + fix tox exec for pytest --- .github/workflows/github-actions.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml index e5d765470..6913d5a54 100644 --- a/.github/workflows/github-actions.yml +++ b/.github/workflows/github-actions.yml @@ -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 }} @@ -110,7 +110,7 @@ jobs: - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: - python-version: 3.9 + python-version: 3.7 check-latest: true - name: install python dep run: | From d4df743a5a46d40c6b2d45a738aa28d088ee8263 Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Sat, 9 Mar 2024 18:32:50 +0100 Subject: [PATCH 3/5] fix tox vs pymongo version mapping --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index daf97ad3e..fb51d13b3 100644 --- a/tox.ini +++ b/tox.ini @@ -11,8 +11,8 @@ deps = 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 From 82cbd97e723ff8b2662cafc2b40f891b0598b1bb Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Sun, 10 Mar 2024 12:48:11 +0100 Subject: [PATCH 4/5] autoupdate pc hooks --- .pre-commit-config.yaml | 10 +++++----- benchmarks/test_basic_doc_ops.py | 14 ++++---------- mongoengine/mongodb_support.py | 1 + mongoengine/pymongo_support.py | 1 + mongoengine/signals.py | 6 +++--- tests/all_warnings/test_warnings.py | 1 + 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5e342679..27d1ef1f5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/benchmarks/test_basic_doc_ops.py b/benchmarks/test_basic_doc_ops.py index 8b8bf4aaf..3f02ef24d 100644 --- a/benchmarks/test_basic_doc_ops.py +++ b/benchmarks/test_basic_doc_ops.py @@ -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() @@ -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(): diff --git a/mongoengine/mongodb_support.py b/mongoengine/mongodb_support.py index 23c538d02..f15a72c96 100644 --- a/mongoengine/mongodb_support.py +++ b/mongoengine/mongodb_support.py @@ -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 diff --git a/mongoengine/pymongo_support.py b/mongoengine/pymongo_support.py index 7aa55676b..674a9b662 100644 --- a/mongoengine/pymongo_support.py +++ b/mongoengine/pymongo_support.py @@ -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 diff --git a/mongoengine/signals.py b/mongoengine/signals.py index 582b533dc..940209a57 100644 --- a/mongoengine/signals.py +++ b/mongoengine/signals.py @@ -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 diff --git a/tests/all_warnings/test_warnings.py b/tests/all_warnings/test_warnings.py index a99101212..187964eb0 100644 --- a/tests/all_warnings/test_warnings.py +++ b/tests/all_warnings/test_warnings.py @@ -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 From bd58f957d91c3d4f4396cacba2696e7e60858999 Mon Sep 17 00:00:00 2001 From: Bastien Gerard Date: Sun, 10 Mar 2024 12:52:21 +0100 Subject: [PATCH 5/5] update changelog and doc --- CONTRIBUTING.rst | 2 +- README.rst | 12 +----------- docs/changelog.rst | 1 + 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index fb1838871..5bda87613 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -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 diff --git a/README.rst b/README.rst index 64e43b6bc..d992da2b3 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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 diff --git a/docs/changelog.rst b/docs/changelog.rst index 81a0ce6dc..bcb465a87 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 =================