#coding: utf-8

from mongoengine.python_support import PY3
from mongoengine import connect

try:
    from django.test import TestCase
    from django.conf import settings
except Exception as err:
    if PY3:
        from unittest import TestCase
        # Dummy value so no error
        class settings:
            MONGO_DATABASE_NAME = 'dummy'
    else:
        raise err


class MongoTestCase(TestCase):
    """
        TestCase class that clear the collection between the tests
    """
    
    def _pre_setup(self):
        from mongoengine.connection import connect, disconnect, get_connection
        for db_name, db_alias in settings.MONGO_DATABASES.items():
            connection = get_connection(db_alias)
            connection.drop_database(db_name)
            disconnect(db_alias)
            connect(db_name, port=settings.MONGO_PORT)
        super(MongoTestCase, self)._pre_setup()

    def _post_teardown(self):
        from mongoengine.connection import get_connection, disconnect
        for db_name, db_alias in settings.MONGO_DATABASES.items():
            connection = get_connection(db_alias)
            connection.drop_database(db_name)
            disconnect(db_alias)
        super(MongoTestCase, self)._post_teardown()


@@ -83,16 +83,87 @@
  connect
+%0Afrom mongoengine.connection import connect, disconnect, get_connection
 %0A%0Atry:%0A 
@@ -479,20 +479,16 @@
     %22%22%22%0A
-    
     Test
@@ -593,246 +593,44 @@
     
-from mongoengine.connection import connect, disconnect, get_connection%0A        for db_name, db_alias in settings.MONGO_DATABASES.items():%0A            connection = get_connection(db_alias)%0A            connection.drop_database(db_name)%0A    
+# Disconnect from current database %0A
     
@@ -640,32 +640,24 @@
  disconnect(
-db_alias
 )%0A          
@@ -658,20 +658,16 @@
     
- 
-   
 connect(
 db_n
@@ -662,23 +662,44 @@
 connect(
-db_name
+settings.MONGO_TEST_DATABASE
 , port=s
@@ -717,16 +717,19 @@
 GO_PORT)
+   
 %0A       
@@ -808,33 +808,16 @@
         
-from mongoengine.
 connecti
@@ -819,22 +819,17 @@
 nection 
-import
+=
  get_con
@@ -839,54 +839,44 @@
 tion
-, disconnect%0A        for db_name, db_alias in 
+()%0A        connection.drop_database(
 sett
@@ -886,24 +886,29 @@
 s.MONGO_
+TEST_
 DATABASE
 S.items(
@@ -903,23 +903,10 @@
 BASE
-S.items():%0A    
+)%0A
     
@@ -923,113 +923,15 @@
 tion
- = get_connection(db_alias)%0A            connection.drop_database(db_name)%0A            disconnect(db_alias
+.close(
 )%0A  

Update for MongoEngine 0.10