From 8aaa29be8ad67abf5649a55d88478373c7db44cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sat, 17 Aug 2024 17:47:44 +0200 Subject: [PATCH 1/6] Improve readability of alchemy checker Avoid broad `except AttributeError`. --- factory/alchemy.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/factory/alchemy.py b/factory/alchemy.py index f934ce5d..e782fbd8 100644 --- a/factory/alchemy.py +++ b/factory/alchemy.py @@ -24,11 +24,8 @@ def _check_sqlalchemy_session_persistence(self, meta, value): @staticmethod def _check_has_sqlalchemy_session_set(meta, value): - try: - if value and meta.sqlalchemy_session: - raise RuntimeError("Provide either a sqlalchemy_session or a sqlalchemy_session_factory, not both") - except AttributeError: - pass + if value is not None and getattr(meta, "sqlalchemy_session", None) is not None: + raise RuntimeError("Provide either a sqlalchemy_session or a sqlalchemy_session_factory, not both") def _build_default_options(self): return super()._build_default_options() + [ From f8456f5e3965bd1d9ea1dd5b6299e0d73783c382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 18 Aug 2024 18:47:44 +0200 Subject: [PATCH 2/6] Remove various (obsolete) warning exemption flags Most of those warnings are no longer an issue with current dependency versions. --- Makefile | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Makefile b/Makefile index 9e0210bc..4d52a184 100644 --- a/Makefile +++ b/Makefile @@ -59,12 +59,6 @@ test: -b \ -X dev \ -Werror \ - -Wdefault:"the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses":DeprecationWarning:distutils: \ - -Wdefault:"Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working":DeprecationWarning:: \ - -Wdefault:"set_output_charset() is deprecated":DeprecationWarning:: \ - -Wdefault:"parameter codeset is deprecated":DeprecationWarning:: \ - -Wdefault:"'cgi' is deprecated and slated for removal in Python 3.13":DeprecationWarning:: \ - -Wdefault:"datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version.":DeprecationWarning:: \ -m unittest # DOC: Test the examples From 819acce99374b55ba9c57a224669b7c8920ed474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 18 Aug 2024 18:50:23 +0200 Subject: [PATCH 3/6] Run the test suite against Mongomock See: #1083 Closes: #1081 --- .github/workflows/test.yml | 5 ----- Makefile | 3 +++ docs/changelog.rst | 1 + docs/spelling_wordlist.txt | 1 + setup.cfg | 1 + tests/test_mongoengine.py | 3 +++ tox.ini | 4 +++- 7 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9d734533..29a7e881 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,11 +31,6 @@ jobs: - "postgres" services: - mongodb: - image: mongo - ports: - - 27017:27017 - postgresdb: image: postgres:alpine ports: diff --git a/Makefile b/Makefile index 4d52a184..778fd785 100644 --- a/Makefile +++ b/Makefile @@ -59,6 +59,9 @@ test: -b \ -X dev \ -Werror \ + -Wignore:::mongomock: \ + -Wignore:::mongomock.__version__: \ + -Wignore:::pkg_resources: \ -m unittest # DOC: Test the examples diff --git a/docs/changelog.rst b/docs/changelog.rst index c5fc0767..10abcc52 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -11,6 +11,7 @@ ChangeLog - Add support for Django 5.0 - Add support for Python 3.12 - :issue:`903`: Add basic typing annotations +- Run the test suite against ``mongomock`` instead of an actual MongoDB server *Bugfix:* diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index 95d4be7f..1c62c8cd 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -19,6 +19,7 @@ kwargs metaclass misconfiguration Mogo +MongoDB mongoengine pre prepend diff --git a/setup.cfg b/setup.cfg index 4f93b98c..1882a205 100644 --- a/setup.cfg +++ b/setup.cfg @@ -51,6 +51,7 @@ dev = SQLAlchemy sqlalchemy_utils mongoengine + mongomock wheel>=0.32.0 tox zest.releaser[recommended] diff --git a/tests/test_mongoengine.py b/tests/test_mongoengine.py index bc930fca..ea1ae687 100644 --- a/tests/test_mongoengine.py +++ b/tests/test_mongoengine.py @@ -10,6 +10,8 @@ except ImportError: raise unittest.SkipTest("mongodb tests disabled.") +import mongomock + import factory from factory.mongoengine import MongoEngineFactory @@ -52,6 +54,7 @@ def setUpClass(cls): db=cls.db_name, host=cls.db_host, port=cls.db_port, + mongo_client_class=mongomock.MongoClient, # PyMongo>=2.1 requires an explicit read_preference. read_preference=mongo_rp.ReadPreference.PRIMARY, # PyMongo>=2.1 has a 20s timeout, use 100ms instead diff --git a/tox.ini b/tox.ini index 17dfdb50..ca21be5c 100644 --- a/tox.ini +++ b/tox.ini @@ -30,7 +30,6 @@ DATABASE_TYPE = [testenv] passenv = - MONGO_HOST POSTGRES_HOST POSTGRES_DATABASE deps = @@ -38,6 +37,9 @@ deps = alchemy: SQLAlchemy alchemy: sqlalchemy_utils mongo: mongoengine + mongo: mongomock + # mongomock imports pkg_resources, provided by setuptools. + mongo: setuptools>=66.1.1 django{32,42,50,main}: Pillow django32: Django>=3.2,<3.3 django42: Django>=4.2,<5.0 From c2188f714361de781acaefe45aa05cc4d59f1756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 18 Aug 2024 18:59:50 +0200 Subject: [PATCH 4/6] Stop testing against PostgreSQL factory_boy never interacts with the database directly; all access is mediated through mature ORMs (Django, SQLAlchemy). Any difference in behaviour regarding the databases would have to be handled by those ORMs, not on our level. This reduces the size of the test matrix, and simplifies the test setup code. See: #1077 --- .github/workflows/test.yml | 13 +----------- README.rst | 2 -- setup.cfg | 1 - tests/alchemyapp/models.py | 25 +---------------------- tests/djapp/settings_pg.py | 41 -------------------------------------- tests/test_alchemy.py | 13 ------------ tox.ini | 25 ++++++----------------- 7 files changed, 8 insertions(+), 112 deletions(-) delete mode 100644 tests/djapp/settings_pg.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 29a7e881..2b381638 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ concurrency: jobs: tests: - name: Python ${{ matrix.python-version }}, Database ${{ matrix.database-type }} + name: Python ${{ matrix.python-version }} runs-on: ubuntu-latest strategy: @@ -26,17 +26,6 @@ jobs: - "3.12" - "pypy-3.9" - "pypy-3.10" - database-type: - - "sqlite" - - "postgres" - - services: - postgresdb: - image: postgres:alpine - ports: - - 5432:5432 - env: - POSTGRES_PASSWORD: password steps: - uses: actions/checkout@v4 diff --git a/README.rst b/README.rst index 3d3677bf..cd926690 100644 --- a/README.rst +++ b/README.rst @@ -405,11 +405,9 @@ To test with a specific framework version, you may use a ``tox`` target: # run tests inside a specific environment (django) $ tox -e py310-djangomain - $ tox -e py310-djangomain-postgres # run tests inside a specific environment (alchemy) $ tox -e py310-alchemy - $ tox -e py310-alchemy-postgres # run tests inside a specific environment (mongoengine) $ tox -e py310-mongo diff --git a/setup.cfg b/setup.cfg index 1882a205..d22ae33e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,7 +49,6 @@ dev = mypy Pillow SQLAlchemy - sqlalchemy_utils mongoengine mongomock wheel>=0.32.0 diff --git a/tests/alchemyapp/models.py b/tests/alchemyapp/models.py index 20e60aab..42e05176 100644 --- a/tests/alchemyapp/models.py +++ b/tests/alchemyapp/models.py @@ -2,34 +2,11 @@ """Helpers for testing SQLAlchemy apps.""" -import os from sqlalchemy import Column, Integer, Unicode, create_engine from sqlalchemy.orm import declarative_base, scoped_session, sessionmaker -try: - import psycopg2 # noqa: F401 - USING_POSTGRES = True -except ImportError: - try: - # pypy does not support `psycopg2` or `psycopg2-binary` - # This is a package that only gets installed with pypy, and it needs to be - # initialized for it to work properly. It mimic `psycopg2` 1-to-1 - from psycopg2cffi import compat - compat.register() - USING_POSTGRES = True - except ImportError: - USING_POSTGRES = False - -if USING_POSTGRES: - pg_database = 'alch_' + os.environ.get('POSTGRES_DATABASE', 'factory_boy_test') - pg_user = os.environ.get('POSTGRES_USER', 'postgres') - pg_password = os.environ.get('POSTGRES_PASSWORD', 'password') - pg_host = os.environ.get('POSTGRES_HOST', 'localhost') - pg_port = os.environ.get('POSTGRES_PORT', '5432') - engine_name = f'postgresql+psycopg2://{pg_user}:{pg_password}@{pg_host}:{pg_port}/{pg_database}' -else: - engine_name = 'sqlite://' +engine_name = 'sqlite://' session = scoped_session(sessionmaker()) engine = create_engine(engine_name) diff --git a/tests/djapp/settings_pg.py b/tests/djapp/settings_pg.py deleted file mode 100644 index de54922c..00000000 --- a/tests/djapp/settings_pg.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright: See the LICENSE file. - -"""Settings for factory_boy/Django tests.""" - -import os - -from .settings import * # noqa: F401, F403 - -try: - # pypy does not support `psycopg2` or `psycopg2-binary` - # This is a package that only gets installed with pypy, and it needs to be - # initialized for it to work properly. It mimic `psycopg2` 1-to-1 - from psycopg2cffi import compat - compat.register() -except ImportError: - pass - -postgres_user = os.environ.get('POSTGRES_USER', 'postgres') -postgres_name = os.environ.get('POSTGRES_DATABASE', 'factory_boy_test') -postgres_password = os.environ.get('POSTGRES_PASSWORD', 'password') -postgres_host = os.environ.get('POSTGRES_HOST', 'localhost') -postgres_port = os.environ.get('POSTGRES_PORT', '5432') - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': postgres_name, - 'USER': postgres_user, - 'PASSWORD': postgres_password, - 'HOST': postgres_host, - 'PORT': postgres_port, - }, - 'replica': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': postgres_name + '_rp', - 'USER': postgres_user, - 'PASSWORD': postgres_password, - 'HOST': postgres_host, - 'PORT': postgres_port, - } -} diff --git a/tests/test_alchemy.py b/tests/test_alchemy.py index 19e4f5ee..6b568ce4 100644 --- a/tests/test_alchemy.py +++ b/tests/test_alchemy.py @@ -10,8 +10,6 @@ except ImportError: raise unittest.SkipTest("sqlalchemy tests disabled.") -from sqlalchemy_utils import create_database, database_exists, drop_database - import factory from factory.alchemy import SQLAlchemyModelFactory @@ -77,17 +75,6 @@ class Meta: text = factory.Sequence(lambda n: "text%s" % n) -if models.USING_POSTGRES: - # sqlite test database gets created/destroyed automatically, postgres does not. - - def setUpModule(): - if not database_exists(models.engine.url): - create_database(models.engine.url) - - def tearDownModule(): - drop_database(models.engine.url) - - class TransactionTestCase(unittest.TestCase): def setUp(self): models.Base.metadata.create_all(models.engine) diff --git a/tox.ini b/tox.ini index ca21be5c..4d5438e1 100644 --- a/tox.ini +++ b/tox.ini @@ -5,13 +5,12 @@ envlist = docs examples linkcheck - py{38,39,310,311,312,py39,py310}-sqlite - py{38,39,310,311,py39,py310}-django32-mongo-alchemy-{sqlite,postgres} - py{38,39,310,311,312}-django42-mongo-alchemy-{sqlite,postgres} - py{py39,py310}-django42-mongo-alchemy-sqlite, - # py{py39,py310}-django42-mongo-alchemy-postgres # TODO: Fix me! - py{310,311,312}-django50-mongo-alchemy-{sqlite,postgres} - py310-djangomain-mongo-alchemy-{sqlite,postgres} + py{38,39,310,311,312,py39,py310} + py{38,39,310,311,py39,py310}-django32-mongo-alchemy + py{38,39,310,311,312}-django42-mongo-alchemy + py{py39,py310}-django42-mongo-alchemy + py{310,311,312}-django50-mongo-alchemy + py310-djangomain-mongo-alchemy [gh-actions] python = @@ -23,19 +22,10 @@ python = pypy-3.9: pypy39 pypy-3.10: pypy310 -[gh-actions:env] -DATABASE_TYPE = - sqlite: sqlite - postgres: postgres - [testenv] -passenv = - POSTGRES_HOST - POSTGRES_DATABASE deps = mypy alchemy: SQLAlchemy - alchemy: sqlalchemy_utils mongo: mongoengine mongo: mongomock # mongomock imports pkg_resources, provided by setuptools. @@ -45,12 +35,9 @@ deps = django42: Django>=4.2,<5.0 django50: Django>=5.0,<5.1 djangomain: https://github.com/django/django/archive/main.tar.gz - py{38,39,310,311,312}-postgres: psycopg2-binary - pypy{39,310}-postgres: psycopg2cffi setenv = py: DJANGO_SETTINGS_MODULE=tests.djapp.settings - postgres: DJANGO_SETTINGS_MODULE=tests.djapp.settings_pg allowlist_externals = make commands = make test From ce3911451b4571903d5e74871e911739eaa9481c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 18 Aug 2024 19:07:28 +0200 Subject: [PATCH 5/6] Stop testing for Django 3.2 Upstream support has been dropped in April 2024. --- docs/changelog.rst | 3 +-- setup.cfg | 1 - tox.ini | 4 +--- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 10abcc52..0b8e4dd6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -20,8 +20,7 @@ ChangeLog *Removed:* -- Drop support for Django 4.0 -- Stop advertising and verifying support for Django 4.1 +- Stop advertising and verifying support for Django 3.2, 4.0, 4.1 3.3.0 (2023-07-19) ------------------ diff --git a/setup.cfg b/setup.cfg index d22ae33e..dd1c00e9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,6 @@ license = MIT classifiers = Development Status :: 5 - Production/Stable Framework :: Django - Framework :: Django :: 3.2 Framework :: Django :: 4.2 Framework :: Django :: 5.0 Intended Audience :: Developers diff --git a/tox.ini b/tox.ini index 4d5438e1..95b88d47 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,6 @@ envlist = examples linkcheck py{38,39,310,311,312,py39,py310} - py{38,39,310,311,py39,py310}-django32-mongo-alchemy py{38,39,310,311,312}-django42-mongo-alchemy py{py39,py310}-django42-mongo-alchemy py{310,311,312}-django50-mongo-alchemy @@ -30,8 +29,7 @@ deps = mongo: mongomock # mongomock imports pkg_resources, provided by setuptools. mongo: setuptools>=66.1.1 - django{32,42,50,main}: Pillow - django32: Django>=3.2,<3.3 + django{42,50,main}: Pillow django42: Django>=4.2,<5.0 django50: Django>=5.0,<5.1 djangomain: https://github.com/django/django/archive/main.tar.gz From a7d06b93a5ad87c3222be8b4380c83af8d4bb3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Sun, 18 Aug 2024 19:11:54 +0200 Subject: [PATCH 6/6] Target Django 5.x tests at version 5.1 --- docs/changelog.rst | 2 +- setup.cfg | 2 +- tox.ini | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0b8e4dd6..9df2a88e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,7 +8,7 @@ ChangeLog *New:* - Add support for Django 4.2 -- Add support for Django 5.0 +- Add support for Django 5.1 - Add support for Python 3.12 - :issue:`903`: Add basic typing annotations - Run the test suite against ``mongomock`` instead of an actual MongoDB server diff --git a/setup.cfg b/setup.cfg index dd1c00e9..ccbfe6fb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,7 +16,7 @@ classifiers = Development Status :: 5 - Production/Stable Framework :: Django Framework :: Django :: 4.2 - Framework :: Django :: 5.0 + Framework :: Django :: 5.1 Intended Audience :: Developers License :: OSI Approved :: MIT License Operating System :: OS Independent diff --git a/tox.ini b/tox.ini index 95b88d47..2bca8a08 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,8 @@ envlist = py{38,39,310,311,312,py39,py310} py{38,39,310,311,312}-django42-mongo-alchemy py{py39,py310}-django42-mongo-alchemy - py{310,311,312}-django50-mongo-alchemy + py{310,311,312}-django51-mongo-alchemy + pypy310-django51-mongo-alchemy py310-djangomain-mongo-alchemy [gh-actions] @@ -29,9 +30,9 @@ deps = mongo: mongomock # mongomock imports pkg_resources, provided by setuptools. mongo: setuptools>=66.1.1 - django{42,50,main}: Pillow + django{42,51,main}: Pillow django42: Django>=4.2,<5.0 - django50: Django>=5.0,<5.1 + django51: Django>=5.1,<5.2 djangomain: https://github.com/django/django/archive/main.tar.gz setenv =