diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5bf500b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,16 @@ +version: 2 +updates: + # GitHub Actions + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + groups: + actions: + patterns: + - "*" + # Python + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index f2d70d2..6269e3f 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -32,7 +32,6 @@ jobs: - uses: extractions/setup-just@v2 - run: just install - run: just lint - - run: uv run pre-commit run --hook-stage manual --all-files - run: just docs - run: just doctest build: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f1d9d86..1808b53 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,27 @@ repos: args: ["-L", "nd"] stages: [manual] +- repo: https://github.com/adamchainz/blacken-docs + rev: "1.19.1" + hooks: + - id: blacken-docs + additional_dependencies: [black==24.*] + +- repo: https://github.com/pre-commit/pygrep-hooks + rev: "v1.10.0" + hooks: + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal + +- repo: https://github.com/hukkin/mdformat + rev: 0.7.21 + hooks: + - id: mdformat + # Optionally add plugins + additional_dependencies: + - mdformat-gfm + - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. rev: v0.9.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index d3b5cf9..3db728b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,184 +2,184 @@ ## 3.0.0: Unreleased - - Support Flask 3.0+ and PyMongo 4.0+. - - Support Python 3.9-3.13. - - Support MongoDB 4.4+. - - Add support for `~flask.json.jsonify()`. +- Support Flask 3.0+ and PyMongo 4.0+. +- Support Python 3.9-3.13. +- Support MongoDB 4.4+. +- Add support for `~flask.json.jsonify()`. ## 2.3.0: April 24, 2019 - - Update version compatibility matrix in tests, drop official support - for PyMongo less than 3.3.x. +- Update version compatibility matrix in tests, drop official support + for PyMongo less than 3.3.x. ## 2.2.0: November 1, 2018 - - [\#117](https://github.com/dcrosta/flask-pymongo/pull/117) Allow - URIs without database name. +- [#117](https://github.com/dcrosta/flask-pymongo/pull/117) Allow + URIs without database name. ## 2.1.0: August 6, 2018 - - [\#114](https://github.com/dcrosta/flask-pymongo/pull/114) Accept - keyword arguments to `~flask_pymongo.PyMongo.save_file` (Andrew C. - Hawkins). +- [#114](https://github.com/dcrosta/flask-pymongo/pull/114) Accept + keyword arguments to `~flask_pymongo.PyMongo.save_file` (Andrew C. + Hawkins). ## 2.0.1: July 17, 2018 - - [\#113](https://github.com/dcrosta/flask-pymongo/pull/113) Make the - `app` argument to `PyMongo` optional (yarobob). +- [#113](https://github.com/dcrosta/flask-pymongo/pull/113) Make the + `app` argument to `PyMongo` optional (yarobob). ## 2.0.0: July 2, 2018 - **This release is not compatible with Flask-PyMongo 0.5.x or any - earlier version.** You can see an explanation of the reasoning and - changes in [issue - \#110](https://github.com/dcrosta/flask-pymongo/issues/110). +**This release is not compatible with Flask-PyMongo 0.5.x or any +earlier version.** You can see an explanation of the reasoning and +changes in [issue +#110](https://github.com/dcrosta/flask-pymongo/issues/110). - - Only support configuration via URI. - - Don't connect to MongoDB by default. - - Clarify version support of Python, Flask, PyMongo, and MongoDB. - - Readability improvement to `README.md` (MinJae Kwon). +- Only support configuration via URI. +- Don't connect to MongoDB by default. +- Clarify version support of Python, Flask, PyMongo, and MongoDB. +- Readability improvement to `README.md` (MinJae Kwon). ## 0.5.2: May 19, 2018 - - [\#102](https://github.com/dcrosta/flask-pymongo/pull/102) Return - 404, not 400, when given an invalid input to - BSONObjectIdConverter (Abraham Toriz - Cruz). +- [#102](https://github.com/dcrosta/flask-pymongo/pull/102) Return + 404, not 400, when given an invalid input to + BSONObjectIdConverter (Abraham Toriz + Cruz). ## 0.5.1: May 24, 2017 - - [\#93](https://github.com/dcrosta/flask-pymongo/pull/93) Supply a - default `MONGO_AUTH_MECHANISM` (Mark Unsworth). +- [#93](https://github.com/dcrosta/flask-pymongo/pull/93) Supply a + default `MONGO_AUTH_MECHANISM` (Mark Unsworth). ## 0.5.0: May 21, 2017 - > **This will be the last 0.x series release.** The next non-bugfix - > release will be Flask-PyMongo 2.0, which will introduce backwards - > breaking changes, and will be the foundation for improvements and - > changes going forward. Flask-PyMongo 2.0 will no longer support - > Python 2.6, but will support Python 2.7 and Python 3.3+. - - - [\#44](https://github.com/dcrosta/flask-pymongo/issues/44), - [\#51](https://github.com/dcrosta/flask-pymongo/pull/51) Redirect - `/` to `/HomePage` in the wiki example (David Awad) - - [\#76](https://github.com/dcrosta/flask-pymongo/pull/76) Build on - more modern Python versions (Robson Roberto Souza Peixoto) - - [\#79](https://github.com/dcrosta/flask-pymongo/pull/79), - [\#84](https://github.com/dcrosta/flask-pymongo/issues/84), - [\#85](https://github.com/dcrosta/flask-pymongo/pull/85) Don't use - `flask.ext` import paths any more (ratson, juliascript) - - [\#40](https://github.com/dcrosta/flask-pymongo/issues/40), - [\#83](https://github.com/dcrosta/flask-pymongo/pull/83), - [\#86](https://github.com/dcrosta/flask-pymongo/pull/86) Fix options - parsing from `MONGO_URI` (jobou) - - [\#72](https://github.com/dcrosta/flask-pymongo/issues/72), - [\#80](https://github.com/dcrosta/flask-pymongo/pull/80) Support - `MONGO_SERVER_SELECTION_TIMEOUT_MS` (Henrik Blidh) - - [\#34](https://github.com/dcrosta/flask-pymongo/issues/34), - [\#64](https://github.com/dcrosta/flask-pymongo/pull/64), - [\#88](https://github.com/dcrosta/flask-pymongo/pull/88) Support - from `MONGO_AUTH_SOURCE` and `MONGO_AUTH_MECHANISM` (Craig Davis) - - [\#74](https://github.com/dcrosta/flask-pymongo/issues/74), - [\#77](https://github.com/dcrosta/flask-pymongo/issues/77), - [\#78](https://github.com/dcrosta/flask-pymongo/pull/78) Fixed - `maxPoolSize` in PyMongo 3.0+ (Henrik Blidh) - - [\#82](https://github.com/dcrosta/flask-pymongo/issues/82) Fix - "another user is already authenticated" error message. - - [\#54](https://github.com/dcrosta/flask-pymongo/issues/54) - Authenticate against "admin" database if no `MONGO_DBNAME` is - provided. +> **This will be the last 0.x series release.** The next non-bugfix +> release will be Flask-PyMongo 2.0, which will introduce backwards +> breaking changes, and will be the foundation for improvements and +> changes going forward. Flask-PyMongo 2.0 will no longer support +> Python 2.6, but will support Python 2.7 and Python 3.3+. + +- [#44](https://github.com/dcrosta/flask-pymongo/issues/44), + [#51](https://github.com/dcrosta/flask-pymongo/pull/51) Redirect + `/` to `/HomePage` in the wiki example (David Awad) +- [#76](https://github.com/dcrosta/flask-pymongo/pull/76) Build on + more modern Python versions (Robson Roberto Souza Peixoto) +- [#79](https://github.com/dcrosta/flask-pymongo/pull/79), + [#84](https://github.com/dcrosta/flask-pymongo/issues/84), + [#85](https://github.com/dcrosta/flask-pymongo/pull/85) Don't use + `flask.ext` import paths any more (ratson, juliascript) +- [#40](https://github.com/dcrosta/flask-pymongo/issues/40), + [#83](https://github.com/dcrosta/flask-pymongo/pull/83), + [#86](https://github.com/dcrosta/flask-pymongo/pull/86) Fix options + parsing from `MONGO_URI` (jobou) +- [#72](https://github.com/dcrosta/flask-pymongo/issues/72), + [#80](https://github.com/dcrosta/flask-pymongo/pull/80) Support + `MONGO_SERVER_SELECTION_TIMEOUT_MS` (Henrik Blidh) +- [#34](https://github.com/dcrosta/flask-pymongo/issues/34), + [#64](https://github.com/dcrosta/flask-pymongo/pull/64), + [#88](https://github.com/dcrosta/flask-pymongo/pull/88) Support + from `MONGO_AUTH_SOURCE` and `MONGO_AUTH_MECHANISM` (Craig Davis) +- [#74](https://github.com/dcrosta/flask-pymongo/issues/74), + [#77](https://github.com/dcrosta/flask-pymongo/issues/77), + [#78](https://github.com/dcrosta/flask-pymongo/pull/78) Fixed + `maxPoolSize` in PyMongo 3.0+ (Henrik Blidh) +- [#82](https://github.com/dcrosta/flask-pymongo/issues/82) Fix + "another user is already authenticated" error message. +- [#54](https://github.com/dcrosta/flask-pymongo/issues/54) + Authenticate against "admin" database if no `MONGO_DBNAME` is + provided. ## 0.4.1: January 25, 2016 - - Add the connect keyword: - [\#67](https://github.com/dcrosta/flask-pymongo/pull/67). +- Add the connect keyword: + [#67](https://github.com/dcrosta/flask-pymongo/pull/67). ## 0.4.0: October 19, 2015 - - Flask-Pymongo is now compatible with pymongo 3.0+: - [\#63](https://github.com/dcrosta/flask-pymongo/pull/63). +- Flask-Pymongo is now compatible with pymongo 3.0+: + [#63](https://github.com/dcrosta/flask-pymongo/pull/63). ## 0.3.1: April 9, 2015 - - Flask-PyMongo is now tested against Python 2.6, 2.7, 3.3, and 3.4. - - Flask-PyMongo installation now no longer depends on - [nose](https://pypi.python.org/pypi/nose/). - - [\#58](https://github.com/dcrosta/flask-pymongo/pull/58) Update - requirements for PyMongo 3.x (Emmanuel Valette). - - [\#43](https://github.com/dcrosta/flask-pymongo/pull/43) Ensure - error is raised when URI database name is parsed as 'None' (Ben - Jeffrey). - - [\#50](https://github.com/dcrosta/flask-pymongo/pull/50) Fix a bug - in read preference handling (Kevin Funk). - - [\#46](https://github.com/dcrosta/flask-pymongo/issues/46) Cannot - use multiple replicaset instances which run on different ports (Mark - Unsworth). - - [\#30](https://github.com/dcrosta/flask-pymongo/issues/30) - ConfiguationError with MONGO_READ_PREFERENCE (Mark Unsworth). +- Flask-PyMongo is now tested against Python 2.6, 2.7, 3.3, and 3.4. +- Flask-PyMongo installation now no longer depends on + [nose](https://pypi.python.org/pypi/nose/). +- [#58](https://github.com/dcrosta/flask-pymongo/pull/58) Update + requirements for PyMongo 3.x (Emmanuel Valette). +- [#43](https://github.com/dcrosta/flask-pymongo/pull/43) Ensure + error is raised when URI database name is parsed as 'None' (Ben + Jeffrey). +- [#50](https://github.com/dcrosta/flask-pymongo/pull/50) Fix a bug + in read preference handling (Kevin Funk). +- [#46](https://github.com/dcrosta/flask-pymongo/issues/46) Cannot + use multiple replicaset instances which run on different ports (Mark + Unsworth). +- [#30](https://github.com/dcrosta/flask-pymongo/issues/30) + ConfiguationError with MONGO_READ_PREFERENCE (Mark Unsworth). ## 0.3.0: July 4, 2013 - - This is a minor version bump which introduces backwards breaking - changes! Please read these change notes carefully. - - Removed read preference constants from Flask-PyMongo; to set a read - preference, use the string name or import constants directly from - `pymongo.read_preferences.ReadPreference`. - - [\#22 (partial)](https://github.com/dcrosta/flask-pymongo/pull/22) - Add support for `MONGO_SOCKET_TIMEOUT_MS` and - `MONGO_CONNECT_TIMEOUT_MS` options (ultrabug). - - [\#27 (partial)](https://github.com/dcrosta/flask-pymongo/pull/27) - Make Flask-PyMongo compatible with Python 3 (Vizzy). +- This is a minor version bump which introduces backwards breaking + changes! Please read these change notes carefully. +- Removed read preference constants from Flask-PyMongo; to set a read + preference, use the string name or import constants directly from + `pymongo.read_preferences.ReadPreference`. +- [#22 (partial)](https://github.com/dcrosta/flask-pymongo/pull/22) + Add support for `MONGO_SOCKET_TIMEOUT_MS` and + `MONGO_CONNECT_TIMEOUT_MS` options (ultrabug). +- [#27 (partial)](https://github.com/dcrosta/flask-pymongo/pull/27) + Make Flask-PyMongo compatible with Python 3 (Vizzy). ## 0.2.1: December 22, 2012 - - [\#19](https://github.com/dcrosta/flask-pymongo/pull/19) Added - `MONGO_DOCUMENT_CLASS` config option (jeverling). +- [#19](https://github.com/dcrosta/flask-pymongo/pull/19) Added + `MONGO_DOCUMENT_CLASS` config option (jeverling). ## 0.2.0: December 15, 2012 - - This is a minor version bump which may introduce backwards breaking - changes! Please read these change notes carefully. - - [\#17](https://github.com/dcrosta/flask-pymongo/pull/17) Now using - PyMongo 2.4's `MongoClient` and `MongoReplicaSetClient` objects - instead of `Connection` and `ReplicaSetConnection` classes - (tang0th). - - [\#17](https://github.com/dcrosta/flask-pymongo/pull/17) Now - requiring at least PyMongo version 2.4 (tang0th). - - [\#17](https://github.com/dcrosta/flask-pymongo/pull/17) The wrapper - class `flask_pymongo.wrappers.Connection` is renamed to - `flask_pymongo.wrappers.MongoClient` (tang0th). - - [\#17](https://github.com/dcrosta/flask-pymongo/pull/17) The wrapper - class `flask_pymongo.wrappers.ReplicaSetConnection` is renamed to - `flask_pymongo.wrappers.MongoReplicaSetClient` (tang0th). - - [\#18](https://github.com/dcrosta/flask-pymongo/issues/18) - `MONGO_AUTO_START_REQUEST` now defaults to `False` when connecting - using a URI. +- This is a minor version bump which may introduce backwards breaking + changes! Please read these change notes carefully. +- [#17](https://github.com/dcrosta/flask-pymongo/pull/17) Now using + PyMongo 2.4's `MongoClient` and `MongoReplicaSetClient` objects + instead of `Connection` and `ReplicaSetConnection` classes + (tang0th). +- [#17](https://github.com/dcrosta/flask-pymongo/pull/17) Now + requiring at least PyMongo version 2.4 (tang0th). +- [#17](https://github.com/dcrosta/flask-pymongo/pull/17) The wrapper + class `flask_pymongo.wrappers.Connection` is renamed to + `flask_pymongo.wrappers.MongoClient` (tang0th). +- [#17](https://github.com/dcrosta/flask-pymongo/pull/17) The wrapper + class `flask_pymongo.wrappers.ReplicaSetConnection` is renamed to + `flask_pymongo.wrappers.MongoReplicaSetClient` (tang0th). +- [#18](https://github.com/dcrosta/flask-pymongo/issues/18) + `MONGO_AUTO_START_REQUEST` now defaults to `False` when connecting + using a URI. ## 0.1.4: December 15, 2012 - - [\#15](https://github.com/dcrosta/flask-pymongo/pull/15) Added - support for `MONGO_MAX_POOL_SIZE` (Fabrice Aneche) +- [#15](https://github.com/dcrosta/flask-pymongo/pull/15) Added + support for `MONGO_MAX_POOL_SIZE` (Fabrice Aneche) ## 0.1.3: September 22, 2012 - - Added support for configuration from MongoDB URI. +- Added support for configuration from MongoDB URI. ## 0.1.2: June 18, 2012 - - Updated wiki example application - - [\#14](https://github.com/dcrosta/flask-pymongo/issues/14) Added - examples and docs to PyPI package. +- Updated wiki example application +- [#14](https://github.com/dcrosta/flask-pymongo/issues/14) Added + examples and docs to PyPI package. ## 0.1.1: May 26, 2012 - - Added support for PyMongo 2.2's "auto start request" feature, by way - of the `MONGO_AUTO_START_REQUEST` configuration flag. - - [\#13](https://github.com/dcrosta/flask-pymongo/pull/13) Added - BSONObjectIdConverter (Christoph Herr) - - [\#12](https://github.com/dcrosta/flask-pymongo/pull/12) Corrected - documentation typo (Thor Adam) +- Added support for PyMongo 2.2's "auto start request" feature, by way + of the `MONGO_AUTO_START_REQUEST` configuration flag. +- [#13](https://github.com/dcrosta/flask-pymongo/pull/13) Added + BSONObjectIdConverter (Christoph Herr) +- [#12](https://github.com/dcrosta/flask-pymongo/pull/12) Corrected + documentation typo (Thor Adam) ## 0.1: December 21, 2011 - - Initial Release +- Initial Release diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7531912..73395cc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,7 +2,6 @@ Thank you for considering contributing to Flask-PyMongo! - ## Support questions For help and general questions, please consider using the [flask-pymongo @@ -11,7 +10,6 @@ tag](https://stackoverflow.com/questions/tagged/flask-pymongo) on the GitHub project. This will keep the issues in the project focused on actual bugs and improvement requests. - ## Reporting issues - Describe what you expected to happen. @@ -23,7 +21,6 @@ actual bugs and improvement requests. - List your Flask-PyMongo, PyMongo, and MongoDB versions. If possible, check if this issue is already fixed in the repository. - ## Submitting patches - All new features must include a test. Flask-PyMongo is tested against a @@ -31,7 +28,6 @@ actual bugs and improvement requests. ensure that your change works for all users. - Use [Sphinx](http://www.sphinx-doc.org/en/master/)-style docstrings - ## Recommended development environment We use [justfile](https://just.systems/man/en/packages.html) for task running @@ -39,12 +35,11 @@ and [uv](https://docs.astral.sh/uv/getting-started/installation/) for python pro To set up your dev environment, run `just install`. -To run the tests, run `just test`. You can pass arguments through to `pytest`. +To run the tests, run `just test`. You can pass arguments through to `pytest`. To run the linters, run `just lint`. -To build the docs, run `just docs` and open `_build/html/index.html` in your browser to view the docs. - +To build the docs, run `just docs` and open `_build/html/index.html` in your browser to view the docs. ## Contributors diff --git a/README.md b/README.md index da9b637..6e8a0fc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Flask-PyMongo -PyMongo support for Flask applications. Requires `flask>=3.0` and `pymongo>=4.0` +PyMongo support for Flask applications. Requires `flask>=3.0` and `pymongo>=4.0` ## Quickstart @@ -12,17 +12,21 @@ app = Flask(__name__) app.config["MONGO_URI"] = "mongodb://localhost:27017/myDatabase" mongo = PyMongo(app) -@app.route('/') + +@app.route("/") def home_page(): - online_users = mongo.db.users.find({'online': True}) - return render_template('index.html', online_users=online_users) + online_users = mongo.db.users.find({"online": True}) + return render_template("index.html", online_users=online_users) ``` ## More Info -* [Flask-PyMongo Documentation](https://flask-pymongo.readthedocs.org/) -* [PyMongo Documentation](https://pymongo.readthedocs.io/en/stable/) -* [Flask Documentation](https://flask.palletsprojects.com/) +- [Flask-PyMongo Documentation](https://flask-pymongo.readthedocs.org/) + +- [PyMongo Documentation](https://pymongo.readthedocs.io/en/stable/) + +- [Flask Documentation](https://flask.palletsprojects.com/) + +- [Quart-Motor (Motor for the Quart Framework)] (https://github.com/marirs/quart-motor) -* [Quart-Motor (Motor for the Quart Framework)] (https://github.com/marirs/quart-motor) -* [Motor Non-Blocking mongodb driver for asyncio applications] (https://github.com/mongodb/motor) +- [Motor Non-Blocking mongodb driver for asyncio applications] (https://github.com/mongodb/motor) diff --git a/docs/index.rst b/docs/index.rst index 5357915..6d900fc 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -43,8 +43,7 @@ You can use :attr:`~flask_pymongo.PyMongo.db` directly in views: @app.route("/") def home_page(): online_users = mongo.db.users.find({"online": True}) - return render_template("index.html", - online_users=online_users) + return render_template("index.html", online_users=online_users) .. note:: @@ -163,7 +162,7 @@ attribute and item accessors. Wrapping the PyMongo classes in this way lets Flask-PyMongo add methods to ``Collection`` while allowing user code to use MongoDB-style dotted expressions. -.. code-block:: python +.. code-block:: pycon >>> type(mongo.cx) diff --git a/flask_pymongo/helpers.py b/flask_pymongo/helpers.py index ed09d11..2cfe74b 100644 --- a/flask_pymongo/helpers.py +++ b/flask_pymongo/helpers.py @@ -87,6 +87,7 @@ def json_route(cart_id): results = mongo.db.carts.find({"_id": cart_id}) return jsonify(results) + # returns a Response with JSON body and application/json content-type: # '[{"count":12,"item":"egg"},{"count":1,"item":"apple"}]' diff --git a/flask_pymongo/wrappers.py b/flask_pymongo/wrappers.py index fc543df..8a71c5a 100644 --- a/flask_pymongo/wrappers.py +++ b/flask_pymongo/wrappers.py @@ -103,8 +103,7 @@ def find_one_or_404(self, *args: Any, **kwargs: Any) -> Any: @app.route("/user/") def user_profile(username): user = mongo.db.users.find_one_or_404({"_id": username}) - return render_template("user.html", - user=user) + return render_template("user.html", user=user) """ found = self.find_one(*args, **kwargs) diff --git a/justfile b/justfile index ca10047..1270741 100644 --- a/justfile +++ b/justfile @@ -14,8 +14,7 @@ test *args: uv run pytest {{args}} lint: - uv run pre-commit run ruff --files - uv run pre-commit run ruff-format --files + uv run pre-commit run --hook-stage manual --all-files docs: uv run sphinx-build -T -b html {{sphinx_opts}} {{docs_build}} diff --git a/pyproject.toml b/pyproject.toml index bcc9b12..63b33ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,16 @@ include = [ "/flask_pymongo", ] +[tool.pytest.ini_options] +minversion = "8.0" +addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] +xfail_strict = true +filterwarnings = ["error"] +log_cli_level = "info" +testpaths = [ + "tests", +] + [tool.mypy] python_version = "3.9" strict = true diff --git a/tests/test_config.py b/tests/test_config.py index ddf5fe3..fdfe586 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -48,8 +48,9 @@ def test_config_with_uri_in_flask_conf_var(self): mongo = flask_pymongo.PyMongo(self.app, connect=True) _wait_until_connected(mongo) - assert mongo.db is not None assert mongo.cx is not None + self.addCleanup(mongo.cx.close) + assert mongo.db is not None assert mongo.db.name == self.dbname assert ("localhost", self.port) == mongo.cx.address or ( "127.0.0.1", @@ -62,8 +63,9 @@ def test_config_with_uri_passed_directly(self): mongo = flask_pymongo.PyMongo(self.app, uri, connect=True) _wait_until_connected(mongo) - assert mongo.db is not None assert mongo.cx is not None + self.addCleanup(mongo.cx.close) + assert mongo.db is not None assert mongo.db.name == self.dbname assert ("localhost", self.port) == mongo.cx.address or ( "127.0.0.1", @@ -91,6 +93,8 @@ class CustomDict(dict[str, Any]): uri = f"mongodb://localhost:{self.port}/{self.dbname}" mongo = flask_pymongo.PyMongo(self.app, uri, document_class=CustomDict) + assert mongo.cx is not None + self.addCleanup(mongo.cx.close) assert mongo.db is not None assert mongo.db.things.find_one() is None, "precondition failed"