Skip to content

Instantly share code, notes, and snippets.

@shen-xianpeng
Forked from snormore/mongotestcase.py
Created June 21, 2014 03:15
Show Gist options
  • Save shen-xianpeng/373f8d461b6e28d26d2f to your computer and use it in GitHub Desktop.
Save shen-xianpeng/373f8d461b6e28d26d2f to your computer and use it in GitHub Desktop.
#coding: utf-8
from nose.plugins.skip import SkipTest
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
"""
mongodb_name = 'test_%s' % settings.MONGO_DATABASE_NAME
def _pre_setup(self):
if PY3:
raise SkipTest('django does not have Python 3 support')
from mongoengine.connection import connect, disconnect
disconnect()
connect(self.mongodb_name, port=settings.MONGO_PORT)
super(MongoTestCase, self)._pre_setup()
def _post_teardown(self):
from mongoengine.connection import get_connection, disconnect
connection = get_connection()
connection.drop_database(self.mongodb_name)
disconnect()
super(MongoTestCase, self)._post_teardown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment