Merge pull request #696 from lexqt/fix_django_17_compat

Fix tests for django 1.7
This commit is contained in:
Yohan Graterol 2014-07-06 12:43:12 -05:00
commit c6e846e0ae
4 changed files with 30 additions and 26 deletions

View File

@ -142,7 +142,7 @@ that much better:
* Pete Campton
* Martyn Smith
* Marcelo Anton
* Aleksey Porfirov
* Aleksey Porfirov (https://github.com/lexqt)
* Nicolas Trippar
* Manuel Hermann
* Gustavo Gawryszewski

View File

@ -6,6 +6,7 @@ Changelog
Changes in 0.9.X - DEV
======================
- Fixed tests for Django 1.7 #696
- Follow ReferenceFields in EmbeddedDocuments with select_related #690
- Added preliminary support for text indexes #680
- Added `elemMatch` operator as well - `match` is too obscure #653

View File

@ -1,39 +1,31 @@
#coding: utf-8
from nose.plugins.skip import SkipTest
from mongoengine.python_support import PY3
from unittest import TestCase
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
from mongoengine.connection import get_db
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
"""
db_name = 'test_%s' % settings.MONGO_DATABASE_NAME
@property
def db_name(self):
from django.conf import settings
return 'test_%s' % getattr(settings, 'MONGO_DATABASE_NAME', 'dummy')
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)
def _post_teardown(self):
super(MongoTestCase, self)._post_teardown()
def dropCollections(self):
for collection in self.db.collection_names():
if collection == 'system.indexes':
continue
self.db.drop_collection(collection)
def tearDown(self):
self.dropCollections()

View File

@ -2,11 +2,11 @@ import sys
sys.path[0:0] = [""]
import unittest
from nose.plugins.skip import SkipTest
from mongoengine import *
from mongoengine.django.shortcuts import get_document_or_404
import django
from django.http import Http404
from django.template import Context, Template
from django.conf import settings
@ -19,6 +19,10 @@ settings.configure(
AUTHENTICATION_BACKENDS = ('mongoengine.django.auth.MongoEngineBackend',)
)
# For Django >= 1.7
if hasattr(django, 'setup'):
django.setup()
try:
from django.contrib.auth import authenticate, get_user_model
from mongoengine.django.auth import User
@ -32,6 +36,7 @@ except Exception:
DJ15 = False
from django.contrib.sessions.tests import SessionTestsMixin
from mongoengine.django.sessions import SessionStore, MongoSession
from mongoengine.django.tests import MongoTestCase
from datetime import tzinfo, timedelta
ZERO = timedelta(0)
@ -293,5 +298,11 @@ class MongoAuthTest(unittest.TestCase):
db_user = User.objects.get(username='user')
self.assertEqual(user.id, db_user.id)
class MongoTestCaseTest(MongoTestCase):
def test_mongo_test_case(self):
self.db.dummy_collection.insert({'collection': 'will be dropped'})
if __name__ == '__main__':
unittest.main()