From 9468277de4604fe7cde556f51b18cd41ebfba66f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Sat, 6 Jul 2024 14:57:54 +0200 Subject: [PATCH] Allow testing against mongomock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ArchLinux and maybe other Linux distributions have interest in testing their package of the project, in an environment where MongoDB isn’t available. Support for that environment is provided on a best-effort basis. If this patch ends up being more trouble than it’s worth, please revert it. Otherwise, let’s be helpful to others. :) --- Makefile | 1 + setup.cfg | 1 + tests/test_mongoengine.py | 30 ++++++++++++++++++++++++++---- tox.ini | 3 +++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9e0210bc..76f5387f 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ test: -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:: \ + -Wdefault:"pkg_resources is deprecated as an API.":DeprecationWarning:: \ -m unittest # DOC: Test the examples 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..aeeb80fa 100644 --- a/tests/test_mongoengine.py +++ b/tests/test_mongoengine.py @@ -6,7 +6,7 @@ import unittest try: - import mongoengine + import mongoengine.connection except ImportError: raise unittest.SkipTest("mongodb tests disabled.") @@ -38,15 +38,14 @@ class Meta: address = factory.SubFactory(AddressFactory) -class MongoEngineTestCase(unittest.TestCase): - +class BaseMongoEngineTestCase: db_name = os.environ.get('MONGO_DATABASE', 'factory_boy_test') db_host = os.environ.get('MONGO_HOST', 'localhost') db_port = int(os.environ.get('MONGO_PORT', '27017')) server_timeout_ms = int(os.environ.get('MONGO_TIMEOUT', '300')) @classmethod - def setUpClass(cls): + def setUpClass(cls, **kwargs): from pymongo import read_preferences as mongo_rp cls.db = mongoengine.connect( db=cls.db_name, @@ -57,11 +56,15 @@ def setUpClass(cls): # PyMongo>=2.1 has a 20s timeout, use 100ms instead serverselectiontimeoutms=cls.server_timeout_ms, uuidRepresentation='standard', + **kwargs, ) @classmethod def tearDownClass(cls): cls.db.drop_database(cls.db_name) + mongoengine.connection.disconnect() + PersonFactory.reset_sequence() + AddressFactory.reset_sequence() def test_build(self): std = PersonFactory.build() @@ -74,3 +77,22 @@ def test_creation(self): self.assertEqual('name1', std1.name) self.assertEqual('street1', std1.address.street) self.assertIsNotNone(std1.id) + + +class MongoEngineTestCase(BaseMongoEngineTestCase, unittest.TestCase): + @classmethod + def setUpClass(cls): + if os.environ.get('MONGO_USE_MOCK', '0') == '1': + raise unittest.SkipTest("Using mongomock, do not run the test suite against MongoDB.") + super().setUpClass() + + +class MockMongoEngineTestCase(BaseMongoEngineTestCase, unittest.TestCase): + """ + In some environments, MongoDB isn’t available. + Run this smoke test instead. + """ + @classmethod + def setUpClass(cls): + import mongomock + super().setUpClass(mongo_client_class=mongomock.MongoClient) diff --git a/tox.ini b/tox.ini index 17dfdb50..989904e3 100644 --- a/tox.ini +++ b/tox.ini @@ -38,6 +38,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