Skip to content

Commit

Permalink
Allow testing against mongomock
Browse files Browse the repository at this point in the history
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. :)
  • Loading branch information
francoisfreitag committed Jul 11, 2024
1 parent d6349de commit 9468277
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dev =
SQLAlchemy
sqlalchemy_utils
mongoengine
mongomock
wheel>=0.32.0
tox
zest.releaser[recommended]
Expand Down
30 changes: 26 additions & 4 deletions tests/test_mongoengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest

try:
import mongoengine
import mongoengine.connection
except ImportError:
raise unittest.SkipTest("mongodb tests disabled.")

Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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)
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9468277

Please sign in to comment.