Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running the mongo test suite against mongomock #1083

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 29 additions & 7 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)
def cleanup():
cls.db.drop_database(cls.db_name)
mongoengine.connection.disconnect()
PersonFactory.reset_sequence()
AddressFactory.reset_sequence()
cls.addClassCleanup(cleanup)

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)
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ DATABASE_TYPE =
[testenv]
passenv =
MONGO_HOST
MONGO_USE_MOCK
POSTGRES_HOST
POSTGRES_DATABASE
deps =
mypy
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