Fix MongoTestCase and add test for it

This commit is contained in:
Aleksey Porfirov 2014-07-05 21:13:25 +04:00
parent 0d81e7933e
commit c4b3196917
2 changed files with 26 additions and 15 deletions

View File

@ -1,38 +1,31 @@
#coding: utf-8 #coding: utf-8
from nose.plugins.skip import SkipTest
from mongoengine.python_support import PY3
from mongoengine import connect from mongoengine import connect
from mongoengine.connection import get_db
from mongoengine.python_support import PY3
try: try:
from django.test import TestCase from django.test import TestCase
from django.conf import settings
except Exception as err: except Exception as err:
if PY3: if PY3:
from unittest import TestCase from unittest import TestCase
# Dummy value so no error
class settings:
MONGO_DATABASE_NAME = 'dummy'
else: else:
raise err raise err
class MongoTestCase(TestCase): class MongoTestCase(TestCase):
def setUp(self):
if PY3:
raise SkipTest('django does not have Python 3 support')
""" """
TestCase class that clear the collection between the tests TestCase class that clear the collection between the tests
""" """
@property @property
def db_name(self): def db_name(self):
return 'test_%s' % settings.MONGO_DATABASE_NAME from django.conf import settings
return 'test_%s' % getattr(settings, 'MONGO_DATABASE_NAME', 'dummy')
def __init__(self, methodName='runtest'): def __init__(self, methodName='runtest'):
self.db = connect(self.db_name).get_db() connect(self.db_name)
self.db = get_db()
super(MongoTestCase, self).__init__(methodName) super(MongoTestCase, self).__init__(methodName)
def _post_teardown(self): def _post_teardown(self):
@ -41,3 +34,14 @@ class MongoTestCase(TestCase):
if collection == 'system.indexes': if collection == 'system.indexes':
continue continue
self.db.drop_collection(collection) self.db.drop_collection(collection)
# prevent standard db init
def _databases_names(self, *args, **kwargs):
return []
def _fixture_setup(self):
pass
def _fixture_teardown(self):
pass

View File

@ -2,9 +2,8 @@ import sys
sys.path[0:0] = [""] sys.path[0:0] = [""]
import unittest import unittest
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from mongoengine import * from mongoengine import *
from mongoengine.django.shortcuts import get_document_or_404 from mongoengine.django.shortcuts import get_document_or_404
import django import django
@ -37,6 +36,7 @@ except Exception:
DJ15 = False DJ15 = False
from django.contrib.sessions.tests import SessionTestsMixin from django.contrib.sessions.tests import SessionTestsMixin
from mongoengine.django.sessions import SessionStore, MongoSession from mongoengine.django.sessions import SessionStore, MongoSession
from mongoengine.django.tests import MongoTestCase
from datetime import tzinfo, timedelta from datetime import tzinfo, timedelta
ZERO = timedelta(0) ZERO = timedelta(0)
@ -298,5 +298,12 @@ class MongoAuthTest(unittest.TestCase):
db_user = User.objects.get(username='user') db_user = User.objects.get(username='user')
self.assertEqual(user.id, db_user.id) self.assertEqual(user.id, db_user.id)
class MongoTestCaseTest(MongoTestCase):
def test_mongo_test_case(self):
# test __init__ and teardown in MongoTestCase
pass
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()