Skip to content

Commit

Permalink
Update tests to handle new connection mechanics correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
insspb committed Jul 1, 2022
1 parent b6f531b commit 3a24265
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ def app():
def db(app):
app.config["MONGODB_HOST"] = "mongodb://localhost:27017/flask_mongoengine_test_db"
test_db = MongoEngine(app)
db_name = test_db.connection.get_database("flask_mongoengine_test_db").name
db_name = (
test_db.connection["default"].get_database("flask_mongoengine_test_db").name
)

if not db_name.endswith("_test_db"):
raise RuntimeError(
f"DATABASE_URL must point to testing db, not to master db ({db_name})"
)

# Clear database before tests, for cases when some test failed before.
test_db.connection.drop_database(db_name)
test_db.connection["default"].drop_database(db_name)

yield test_db

# Clear database after tests, for graceful exit.
test_db.connection.drop_database(db_name)
test_db.connection["default"].drop_database(db_name)


@pytest.fixture()
Expand Down
34 changes: 7 additions & 27 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mongoengine
import pymongo
import pytest
from mongoengine.connection import ConnectionFailure
from mongoengine.context_managers import switch_db
Expand Down Expand Up @@ -281,48 +280,29 @@ class Todo(db.Document):
assert doc is not None


def test_ingnored_mongodb_prefix_config(app):
"""Config starting by MONGODB_ but not used by flask-mongoengine
should be ignored.
"""
def test_incorrect_value_with_mongodb_prefix__should_trigger_mongoengine_raise(app):
db = MongoEngine()
app.config[
"MONGODB_HOST"
] = "mongodb://localhost:27017/flask_mongoengine_test_db_prod"
# Invalid host, should trigger exception if used
app.config["MONGODB_TEST_HOST"] = "dummy://localhost:27017/test"
db.init_app(app)

connection = mongoengine.get_connection()
mongo_engine_db = mongoengine.get_db()
assert isinstance(mongo_engine_db, Database)
assert isinstance(connection, MongoClient)
assert mongo_engine_db.name == "flask_mongoengine_test_db_prod"
assert connection.HOST == "localhost"
assert connection.PORT == 27017
with pytest.raises(ConnectionFailure):
db.init_app(app)


def test_connection_kwargs(app):
"""Make sure additional connection kwargs work."""

# Figure out whether to use "MAX_POOL_SIZE" or "MAXPOOLSIZE" based
# on PyMongo version (former was changed to the latter as described
# in https://jira.mongodb.org/browse/PYTHON-854)
# TODO remove once PyMongo < 3.0 support is dropped
if pymongo.version_tuple[0] >= 3:
MAX_POOL_SIZE_KEY = "MAXPOOLSIZE"
else:
MAX_POOL_SIZE_KEY = "MAX_POOL_SIZE"

app.config["MONGODB_SETTINGS"] = {
"ALIAS": "tz_aware_true",
"DB": "flask_mongoengine_testing_tz_aware",
"TZ_AWARE": True,
"READ_PREFERENCE": ReadPreference.SECONDARY,
MAX_POOL_SIZE_KEY: 10,
"MAXPOOLSIZE": 10,
}
db = MongoEngine(app)

assert db.connection.codec_options.tz_aware
# assert db.connection.max_pool_size == 10
assert db.connection.read_preference == ReadPreference.SECONDARY
assert db.connection["tz_aware_true"].codec_options.tz_aware
assert db.connection["tz_aware_true"].max_pool_size == 10
assert db.connection["tz_aware_true"].read_preference == ReadPreference.SECONDARY
8 changes: 5 additions & 3 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ def extended_db(app):
app.json_encoder = DummyEncoder
app.config["MONGODB_HOST"] = "mongodb://localhost:27017/flask_mongoengine_test_db"
test_db = MongoEngine(app)
db_name = test_db.connection.get_database("flask_mongoengine_test_db").name
db_name = (
test_db.connection["default"].get_database("flask_mongoengine_test_db").name
)

if not db_name.endswith("_test_db"):
raise RuntimeError(
f"DATABASE_URL must point to testing db, not to master db ({db_name})"
)

# Clear database before tests, for cases when some test failed before.
test_db.connection.drop_database(db_name)
test_db.connection["default"].drop_database(db_name)

yield test_db

# Clear database after tests, for graceful exit.
test_db.connection.drop_database(db_name)
test_db.connection["default"].drop_database(db_name)


class DummyEncoder(flask.json.JSONEncoder):
Expand Down

0 comments on commit 3a24265

Please sign in to comment.