Skip to content

Commit 768ee18

Browse files
committed
Some base tests refactoring
1 parent 526b9eb commit 768ee18

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import mongoengine
2+
import pytest
3+
from flask import Flask
4+
5+
6+
@pytest.fixture()
7+
def app():
8+
app = Flask(__name__)
9+
app.config["TESTING"] = True
10+
11+
with app.app_context():
12+
yield app
13+
14+
mongoengine.connection.disconnect_all()
15+
16+
17+
@pytest.fixture()
18+
def todo(db):
19+
class Todo(db.Document):
20+
# meta = {"db_alias": alias}
21+
title = mongoengine.StringField(max_length=60)
22+
text = mongoengine.StringField()
23+
done = mongoengine.BooleanField(default=False)
24+
25+
return Todo

tests/test_base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Tests for base MongoEngine class."""
2+
from flask_mongoengine import MongoEngine
3+
import pytest
4+
5+
6+
def test_mongoengine_class__should_raise_type_error__if_config_not_dict():
7+
"""MongoEngine will handle None values, but will pass anything else as app."""
8+
input_value = "Not dict type"
9+
with pytest.raises(TypeError) as error:
10+
MongoEngine(input_value)
11+
assert str(error.value) == "Invalid Flask application instance"
12+
13+
14+
@pytest.mark.parametrize("input_value", [None, "Not dict type"])
15+
def test_init_app__should_raise_type_error__if_config_not_dict(input_value):
16+
db = MongoEngine()
17+
with pytest.raises(TypeError) as error:
18+
db.init_app(input_value)
19+
assert str(error.value) == "Invalid Flask application instance"

tests/test_connection.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
import mongoengine
2+
import pytest
3+
from mongoengine.connection import ConnectionFailure
4+
from pymongo.database import Database
5+
from pymongo.mongo_client import MongoClient
6+
from pymongo.errors import InvalidURI
7+
from flask_mongoengine import MongoEngine, current_mongoengine_instance
8+
9+
10+
def test_connection__should_use_defaults__if_no_settings_provided(app):
11+
"""Make sure a simple connection to a standalone MongoDB works."""
12+
db = MongoEngine()
13+
14+
# Verify no extension for Mongoengine yet created for app
15+
assert app.extensions == {}
16+
assert current_mongoengine_instance() is None
17+
18+
# Create db connection. Should return None.
19+
assert db.init_app(app) is None
20+
21+
# Verify db added to Flask extensions.
22+
assert current_mongoengine_instance() == db
23+
24+
# Verify db settings passed to pymongo driver.
25+
# Default mongoengine db is 'default', default Flask-Mongoengine db is 'test'.
26+
connection = mongoengine.get_connection()
27+
mongo_engine_db = mongoengine.get_db()
28+
assert isinstance(mongo_engine_db, Database)
29+
assert isinstance(connection, MongoClient)
30+
assert mongo_engine_db.name == "test"
31+
assert connection.HOST == "localhost"
32+
assert connection.PORT == 27017
33+
34+
35+
@pytest.mark.parametrize(
36+
("config_extension"),
37+
[
38+
{
39+
"MONGODB_SETTINGS": {
40+
"ALIAS": "simple_conn",
41+
"HOST": "localhost",
42+
"PORT": 27017,
43+
"DB": "flask_mongoengine_test_db",
44+
}
45+
},
46+
{
47+
"MONGODB_HOST": "localhost",
48+
"MONGODB_PORT": 27017,
49+
"MONGODB_DB": "flask_mongoengine_test_db",
50+
"MONGODB_ALIAS": "simple_conn",
51+
},
52+
],
53+
ids=("Dict format", "Config variable format"),
54+
)
55+
def test_connection__should_pass_alias__if_provided(app, config_extension):
56+
"""Make sure a simple connection pass ALIAS setting variable."""
57+
db = MongoEngine()
58+
app.config.update(config_extension)
59+
60+
# Verify no extension for Mongoengine yet created for app
61+
assert app.extensions == {}
62+
assert current_mongoengine_instance() is None
63+
64+
# Create db connection. Should return None.
65+
assert db.init_app(app) is None
66+
67+
# Verify db added to Flask extensions.
68+
assert current_mongoengine_instance() == db
69+
70+
# Verify db settings passed to pymongo driver.
71+
# ALIAS is used to find correct connection.
72+
# As we do not use default alias, default call to mongoengine.get_connection
73+
# should raise.
74+
with pytest.raises(ConnectionFailure):
75+
mongoengine.get_connection()
76+
77+
connection = mongoengine.get_connection("simple_conn")
78+
mongo_engine_db = mongoengine.get_db("simple_conn")
79+
assert isinstance(mongo_engine_db, Database)
80+
assert isinstance(connection, MongoClient)
81+
assert mongo_engine_db.name == "flask_mongoengine_test_db"
82+
assert connection.HOST == "localhost"
83+
assert connection.PORT == 27017
84+
85+
86+
@pytest.mark.parametrize(
87+
("config_extension"),
88+
[
89+
{
90+
"MONGODB_SETTINGS": {
91+
"HOST": "mongodb://localhost:27017/flask_mongoengine_test_db"
92+
}
93+
},
94+
{
95+
"MONGODB_HOST": "mongodb://localhost:27017/flask_mongoengine_test_db",
96+
"MONGODB_PORT": 27017,
97+
"MONGODB_DB": "should_ignore_it",
98+
},
99+
],
100+
ids=("Dict format", "Config variable format"),
101+
)
102+
def test_connection__should_parse_host_uri__if_host_formatted_as_uri(
103+
app, config_extension
104+
):
105+
"""Make sure a simple connection pass ALIAS setting variable."""
106+
db = MongoEngine()
107+
app.config.update(config_extension)
108+
109+
# Verify no extension for Mongoengine yet created for app
110+
assert app.extensions == {}
111+
assert current_mongoengine_instance() is None
112+
113+
# Create db connection. Should return None.
114+
assert db.init_app(app) is None
115+
116+
# Verify db added to Flask extensions.
117+
assert current_mongoengine_instance() == db
118+
119+
connection = mongoengine.get_connection()
120+
mongo_engine_db = mongoengine.get_db()
121+
assert isinstance(mongo_engine_db, Database)
122+
assert isinstance(connection, MongoClient)
123+
assert mongo_engine_db.name == "flask_mongoengine_test_db"
124+
assert connection.HOST == "localhost"
125+
assert connection.PORT == 27017
126+
127+
128+
@pytest.mark.parametrize(
129+
("config_extension"),
130+
[
131+
{
132+
"MONGODB_SETTINGS": {
133+
"HOST": "mongomock://localhost:27017/flask_mongoengine_test_db"
134+
}
135+
},
136+
{
137+
"MONGODB_SETTINGS": {
138+
"ALIAS": "simple_conn",
139+
"HOST": "localhost",
140+
"PORT": 27017,
141+
"DB": "flask_mongoengine_test_db",
142+
"IS_MOCK": True,
143+
}
144+
},
145+
{"MONGODB_HOST": "mongomock://localhost:27017/flask_mongoengine_test_db"},
146+
],
147+
ids=("Dict format as URI", "Dict format as Param", "Config variable format as URI"),
148+
)
149+
def test_connection__should_parse_mongo_mock_uri__as_uri_and_as_settings(
150+
app, config_extension
151+
):
152+
"""Make sure a simple connection pass ALIAS setting variable."""
153+
db = MongoEngine()
154+
app.config.update(config_extension)
155+
156+
# Verify no extension for Mongoengine yet created for app
157+
assert app.extensions == {}
158+
assert current_mongoengine_instance() is None
159+
160+
# Create db connection. Should return None.
161+
162+
with pytest.raises(RuntimeError) as error:
163+
assert db.init_app(app) is None
164+
165+
assert str(error.value) == "You need mongomock installed to mock MongoEngine."
166+
167+
168+
@pytest.mark.parametrize(
169+
("config_extension"),
170+
[
171+
{
172+
"MONGODB_SETTINGS": {
173+
"HOST": "postgre://localhost:27017/flask_mongoengine_test_db"
174+
}
175+
},
176+
{"MONGODB_HOST": "mysql://localhost:27017/flask_mongoengine_test_db"},
177+
],
178+
ids=("Dict format as URI", "Config variable format as URI"),
179+
)
180+
def test_connection__should_raise__if_uri_not_properly_formatted(app, config_extension):
181+
"""Make sure a simple connection pass ALIAS setting variable."""
182+
db = MongoEngine()
183+
app.config.update(config_extension)
184+
185+
# Verify no extension for Mongoengine yet created for app
186+
assert app.extensions == {}
187+
assert current_mongoengine_instance() is None
188+
189+
# Create db connection. Should return None.
190+
191+
with pytest.raises(InvalidURI) as error:
192+
assert db.init_app(app) is None
193+
194+
assert (
195+
str(error.value)
196+
== "Invalid URI scheme: URI must begin with 'mongodb://' or 'mongodb+srv://'"
197+
)

0 commit comments

Comments
 (0)