Fix test failing randomly because of concurrency.

This commit is contained in:
Gilb's 2016-03-15 22:28:31 +01:00
parent d651d0d472
commit 39eec59c90

View File

@ -11,6 +11,7 @@ from datetime import datetime
from mongoengine import * from mongoengine import *
from mongoengine.connection import get_db, get_connection from mongoengine.connection import get_db, get_connection
from mongoengine.context_managers import switch_db
__all__ = ("IndexesTest", ) __all__ = ("IndexesTest", )
@ -822,33 +823,29 @@ class IndexesTest(unittest.TestCase):
name = StringField(required=True) name = StringField(required=True)
term = StringField(required=True) term = StringField(required=True)
class Report(Document): class ReportEmbedded(Document):
key = EmbeddedDocumentField(CompoundKey, primary_key=True) key = EmbeddedDocumentField(CompoundKey, primary_key=True)
text = StringField() text = StringField()
Report.drop_collection()
my_key = CompoundKey(name="n", term="ok") my_key = CompoundKey(name="n", term="ok")
report = Report(text="OK", key=my_key).save() report = ReportEmbedded(text="OK", key=my_key).save()
self.assertEqual({'text': 'OK', '_id': {'term': 'ok', 'name': 'n'}}, self.assertEqual({'text': 'OK', '_id': {'term': 'ok', 'name': 'n'}},
report.to_mongo()) report.to_mongo())
self.assertEqual(report, Report.objects.get(pk=my_key)) self.assertEqual(report, ReportEmbedded.objects.get(pk=my_key))
def test_compound_key_dictfield(self): def test_compound_key_dictfield(self):
class Report(Document): class ReportDictField(Document):
key = DictField(primary_key=True) key = DictField(primary_key=True)
text = StringField() text = StringField()
Report.drop_collection()
my_key = {"name": "n", "term": "ok"} my_key = {"name": "n", "term": "ok"}
report = Report(text="OK", key=my_key).save() report = ReportDictField(text="OK", key=my_key).save()
self.assertEqual({'text': 'OK', '_id': {'term': 'ok', 'name': 'n'}}, self.assertEqual({'text': 'OK', '_id': {'term': 'ok', 'name': 'n'}},
report.to_mongo()) report.to_mongo())
self.assertEqual(report, Report.objects.get(pk=my_key)) self.assertEqual(report, ReportDictField.objects.get(pk=my_key))
def test_string_indexes(self): def test_string_indexes(self):
@ -909,26 +906,38 @@ class IndexesTest(unittest.TestCase):
Issue #812 Issue #812
""" """
# Use a new connection and database since dropping the database could
# cause concurrent tests to fail.
connection = connect(db='tempdatabase',
alias='test_indexes_after_database_drop')
class BlogPost(Document): class BlogPost(Document):
title = StringField() title = StringField()
slug = StringField(unique=True) slug = StringField(unique=True)
BlogPost.drop_collection() meta = {'db_alias': 'test_indexes_after_database_drop'}
# Create Post #1 try:
post1 = BlogPost(title='test1', slug='test') BlogPost.drop_collection()
post1.save()
# Drop the Database # Create Post #1
self.connection.drop_database(BlogPost._get_db().name) post1 = BlogPost(title='test1', slug='test')
post1.save()
# Re-create Post #1 # Drop the Database
post1 = BlogPost(title='test1', slug='test') connection.drop_database('tempdatabase')
post1.save()
# Re-create Post #1
post1 = BlogPost(title='test1', slug='test')
post1.save()
# Create Post #2
post2 = BlogPost(title='test2', slug='test')
self.assertRaises(NotUniqueError, post2.save)
finally:
# Drop the temporary database at the end
connection.drop_database('tempdatabase')
# Create Post #2
post2 = BlogPost(title='test2', slug='test')
self.assertRaises(NotUniqueError, post2.save)
def test_index_dont_send_cls_option(self): def test_index_dont_send_cls_option(self):
""" """