Skip to content

Commit

Permalink
Improve mongomock tests to avoid regression
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-bagerard committed Dec 13, 2020
1 parent 2e284b9 commit 65b8cfc
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 117 deletions.
119 changes: 2 additions & 117 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import datetime
import unittest

from bson.tz_util import utc
import pymongo

from pymongo import MongoClient, ReadPreference
from pymongo.errors import InvalidName, OperationFailure
import pytest

try:
import unittest2 as unittest
except ImportError:
import unittest

import mongoengine.connection
from mongoengine import (
DateTimeField,
Expand All @@ -34,18 +29,6 @@ def get_tz_awareness(connection):
return connection.codec_options.tz_aware


try:
import mongomock

MONGOMOCK_INSTALLED = True
except ImportError:
MONGOMOCK_INSTALLED = False

require_mongomock = pytest.mark.skipif(
not MONGOMOCK_INSTALLED, reason="you need mongomock installed to run this testcase"
)


class ConnectionTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -196,7 +179,7 @@ def test_connect_passes_silently_connect_multiple_times_with_same_config(self):
def test_connect_with_invalid_db_name(self):
"""Ensure that connect() method fails fast if db name is invalid"""
with pytest.raises(InvalidName):
connect("mongomock://localhost")
connect("mongodb://localhost")

def test_connect_with_db_name_external(self):
"""Ensure that connect() works if db name is $external"""
Expand All @@ -220,104 +203,6 @@ def test_connect_with_invalid_db_name_type(self):
non_string_db_name = ["e. g. list instead of a string"]
connect(non_string_db_name)

@require_mongomock
def test_connect_in_mocking(self):
"""Ensure that the connect() method works properly in mocking."""
connect("mongoenginetest", host="mongomock://localhost")
conn = get_connection()
assert isinstance(conn, mongomock.MongoClient)

connect("mongoenginetest2", host="mongomock://localhost", alias="testdb2")
conn = get_connection("testdb2")
assert isinstance(conn, mongomock.MongoClient)

connect(
"mongoenginetest3",
host="mongodb://localhost",
is_mock=True,
alias="testdb3",
)
conn = get_connection("testdb3")
assert isinstance(conn, mongomock.MongoClient)

connect("mongoenginetest4", is_mock=True, alias="testdb4")
conn = get_connection("testdb4")
assert isinstance(conn, mongomock.MongoClient)

connect(
host="mongodb://localhost:27017/mongoenginetest5",
is_mock=True,
alias="testdb5",
)
conn = get_connection("testdb5")
assert isinstance(conn, mongomock.MongoClient)

connect(host="mongomock://localhost:27017/mongoenginetest6", alias="testdb6")
conn = get_connection("testdb6")
assert isinstance(conn, mongomock.MongoClient)

connect(
host="mongomock://localhost:27017/mongoenginetest7",
is_mock=True,
alias="testdb7",
)
conn = get_connection("testdb7")
assert isinstance(conn, mongomock.MongoClient)

@require_mongomock
def test_default_database_with_mocking(self):
"""Ensure that the default database is correctly set when using mongomock."""
disconnect_all()

class SomeDocument(Document):
pass

conn = connect(host="mongomock://localhost:27017/mongoenginetest")
some_document = SomeDocument()
# database won't exist until we save a document
some_document.save()
assert conn.get_default_database().name == "mongoenginetest"
assert conn.list_database_names()[0] == "mongoenginetest"

@require_mongomock
def test_connect_with_host_list(self):
"""Ensure that the connect() method works when host is a list
Uses mongomock to test w/o needing multiple mongod/mongos processes
"""
connect(host=["mongomock://localhost"])
conn = get_connection()
assert isinstance(conn, mongomock.MongoClient)

connect(host=["mongodb://localhost"], is_mock=True, alias="testdb2")
conn = get_connection("testdb2")
assert isinstance(conn, mongomock.MongoClient)

connect(host=["localhost"], is_mock=True, alias="testdb3")
conn = get_connection("testdb3")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["mongomock://localhost:27017", "mongomock://localhost:27018"],
alias="testdb4",
)
conn = get_connection("testdb4")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["mongodb://localhost:27017", "mongodb://localhost:27018"],
is_mock=True,
alias="testdb5",
)
conn = get_connection("testdb5")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["localhost:27017", "localhost:27018"], is_mock=True, alias="testdb6"
)
conn = get_connection("testdb6")
assert isinstance(conn, mongomock.MongoClient)

def test_disconnect_cleans_globals(self):
"""Ensure that the disconnect() method cleans the globals objects"""
connections = mongoengine.connection._connections
Expand Down
173 changes: 173 additions & 0 deletions tests/test_connection_mongomock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import unittest

import pytest

import mongoengine.connection
from mongoengine import (
Document,
StringField,
connect,
disconnect_all,
)
from mongoengine.connection import get_connection


try:
import mongomock

MONGOMOCK_INSTALLED = True
except ImportError:
MONGOMOCK_INSTALLED = False

require_mongomock = pytest.mark.skipif(
not MONGOMOCK_INSTALLED, reason="you need mongomock installed to run this testcase"
)


class MongoMockConnectionTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
disconnect_all()

@classmethod
def tearDownClass(cls):
disconnect_all()

def tearDown(self):
mongoengine.connection._connection_settings = {}
mongoengine.connection._connections = {}
mongoengine.connection._dbs = {}

@require_mongomock
def test_connect_in_mocking(self):
"""Ensure that the connect() method works properly in mocking."""
connect("mongoenginetest", host="mongomock://localhost")
conn = get_connection()
assert isinstance(conn, mongomock.MongoClient)

connect("mongoenginetest2", host="mongomock://localhost", alias="testdb2")
conn = get_connection("testdb2")
assert isinstance(conn, mongomock.MongoClient)

connect(
"mongoenginetest3",
host="mongodb://localhost",
is_mock=True,
alias="testdb3",
)
conn = get_connection("testdb3")
assert isinstance(conn, mongomock.MongoClient)

connect("mongoenginetest4", is_mock=True, alias="testdb4")
conn = get_connection("testdb4")
assert isinstance(conn, mongomock.MongoClient)

connect(
host="mongodb://localhost:27017/mongoenginetest5",
is_mock=True,
alias="testdb5",
)
conn = get_connection("testdb5")
assert isinstance(conn, mongomock.MongoClient)

connect(host="mongomock://localhost:27017/mongoenginetest6", alias="testdb6")
conn = get_connection("testdb6")
assert isinstance(conn, mongomock.MongoClient)

connect(
host="mongomock://localhost:27017/mongoenginetest7",
is_mock=True,
alias="testdb7",
)
conn = get_connection("testdb7")
assert isinstance(conn, mongomock.MongoClient)

@require_mongomock
def test_default_database_with_mocking(self):
"""Ensure that the default database is correctly set when using mongomock."""
disconnect_all()

class SomeDocument(Document):
pass

conn = connect(host="mongomock://localhost:27017/mongoenginetest")
some_document = SomeDocument()
# database won't exist until we save a document
some_document.save()
assert SomeDocument.objects.count() == 1
assert conn.get_default_database().name == "mongoenginetest"
assert conn.list_database_names()[0] == "mongoenginetest"

@require_mongomock
def test_basic_queries_against_mongomock(self):
disconnect_all()

connect(host="mongomock://localhost:27017/mongoenginetest")

class Person(Document):
name = StringField()

Person.drop_collection()
assert Person.objects.count() == 0

bob = Person(name="Bob").save()
john = Person(name="John").save()
assert Person.objects.count() == 2

qs = Person.objects(name="Bob")
assert qs.count() == 1
assert qs.first() == bob
assert list(qs.as_pymongo()) == [{"_id": bob.id, "name": "Bob"}]

pipeline = [{"$project": {"name": {"$toUpper": "$name"}}}]
data = Person.objects.order_by("name").aggregate(pipeline)
assert list(data) == [
{"_id": bob.id, "name": "BOB"},
{"_id": john.id, "name": "JOHN"},
]

Person.drop_collection()
assert Person.objects.count() == 0

@require_mongomock
def test_connect_with_host_list(self):
"""Ensure that the connect() method works when host is a list
Uses mongomock to test w/o needing multiple mongod/mongos processes
"""
connect(host=["mongomock://localhost"])
conn = get_connection()
assert isinstance(conn, mongomock.MongoClient)

connect(host=["mongodb://localhost"], is_mock=True, alias="testdb2")
conn = get_connection("testdb2")
assert isinstance(conn, mongomock.MongoClient)

connect(host=["localhost"], is_mock=True, alias="testdb3")
conn = get_connection("testdb3")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["mongomock://localhost:27017", "mongomock://localhost:27018"],
alias="testdb4",
)
conn = get_connection("testdb4")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["mongodb://localhost:27017", "mongodb://localhost:27018"],
is_mock=True,
alias="testdb5",
)
conn = get_connection("testdb5")
assert isinstance(conn, mongomock.MongoClient)

connect(
host=["localhost:27017", "localhost:27018"], is_mock=True, alias="testdb6"
)
conn = get_connection("testdb6")
assert isinstance(conn, mongomock.MongoClient)


if __name__ == "__main__":
unittest.main()

0 comments on commit 65b8cfc

Please sign in to comment.