Merge pull request #696 from lexqt/fix_django_17_compat
Fix tests for django 1.7
This commit is contained in:
commit
c6e846e0ae
2
AUTHORS
2
AUTHORS
@ -142,7 +142,7 @@ that much better:
|
|||||||
* Pete Campton
|
* Pete Campton
|
||||||
* Martyn Smith
|
* Martyn Smith
|
||||||
* Marcelo Anton
|
* Marcelo Anton
|
||||||
* Aleksey Porfirov
|
* Aleksey Porfirov (https://github.com/lexqt)
|
||||||
* Nicolas Trippar
|
* Nicolas Trippar
|
||||||
* Manuel Hermann
|
* Manuel Hermann
|
||||||
* Gustavo Gawryszewski
|
* Gustavo Gawryszewski
|
||||||
|
@ -6,6 +6,7 @@ Changelog
|
|||||||
Changes in 0.9.X - DEV
|
Changes in 0.9.X - DEV
|
||||||
======================
|
======================
|
||||||
|
|
||||||
|
- Fixed tests for Django 1.7 #696
|
||||||
- Follow ReferenceFields in EmbeddedDocuments with select_related #690
|
- Follow ReferenceFields in EmbeddedDocuments with select_related #690
|
||||||
- Added preliminary support for text indexes #680
|
- Added preliminary support for text indexes #680
|
||||||
- Added `elemMatch` operator as well - `match` is too obscure #653
|
- Added `elemMatch` operator as well - `match` is too obscure #653
|
||||||
|
@ -1,39 +1,31 @@
|
|||||||
#coding: utf-8
|
#coding: utf-8
|
||||||
from nose.plugins.skip import SkipTest
|
|
||||||
|
|
||||||
from mongoengine.python_support import PY3
|
from unittest import TestCase
|
||||||
|
|
||||||
from mongoengine import connect
|
from mongoengine import connect
|
||||||
|
from mongoengine.connection import get_db
|
||||||
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):
|
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
|
||||||
"""
|
"""
|
||||||
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'):
|
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 dropCollections(self):
|
||||||
super(MongoTestCase, self)._post_teardown()
|
|
||||||
for collection in self.db.collection_names():
|
for collection in self.db.collection_names():
|
||||||
if collection == 'system.indexes':
|
if collection == 'system.indexes':
|
||||||
continue
|
continue
|
||||||
self.db.drop_collection(collection)
|
self.db.drop_collection(collection)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.dropCollections()
|
||||||
|
@ -2,11 +2,11 @@ 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
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.template import Context, Template
|
from django.template import Context, Template
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -19,6 +19,10 @@ settings.configure(
|
|||||||
AUTHENTICATION_BACKENDS = ('mongoengine.django.auth.MongoEngineBackend',)
|
AUTHENTICATION_BACKENDS = ('mongoengine.django.auth.MongoEngineBackend',)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# For Django >= 1.7
|
||||||
|
if hasattr(django, 'setup'):
|
||||||
|
django.setup()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from django.contrib.auth import authenticate, get_user_model
|
from django.contrib.auth import authenticate, get_user_model
|
||||||
from mongoengine.django.auth import User
|
from mongoengine.django.auth import User
|
||||||
@ -32,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)
|
||||||
|
|
||||||
@ -293,5 +298,11 @@ 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):
|
||||||
|
self.db.dummy_collection.insert({'collection': 'will be dropped'})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user