refactored iteritems/itervalues to improve 2/3 compat #2003

This commit is contained in:
Bastien Gérard
2019-02-18 21:08:04 +01:00
parent db47604865
commit 6d353dae1e
16 changed files with 87 additions and 72 deletions

View File

@@ -5,6 +5,7 @@ from datetime import datetime
from nose.plugins.skip import SkipTest
from pymongo.errors import OperationFailure
import pymongo
from six import iteritems
from mongoengine import *
from mongoengine.connection import get_db
@@ -68,7 +69,7 @@ class IndexesTest(unittest.TestCase):
info = BlogPost.objects._collection.index_information()
# _id, '-date', 'tags', ('cat', 'date')
self.assertEqual(len(info), 4)
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
for expected in expected_specs:
self.assertIn(expected['fields'], info)
@@ -100,7 +101,7 @@ class IndexesTest(unittest.TestCase):
# the indices on -date and tags will both contain
# _cls as first element in the key
self.assertEqual(len(info), 4)
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
for expected in expected_specs:
self.assertIn(expected['fields'], info)
@@ -115,7 +116,7 @@ class IndexesTest(unittest.TestCase):
ExtendedBlogPost.ensure_indexes()
info = ExtendedBlogPost.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
for expected in expected_specs:
self.assertIn(expected['fields'], info)
@@ -225,7 +226,7 @@ class IndexesTest(unittest.TestCase):
# Indexes are lazy so use list() to perform query
list(Person.objects)
info = Person.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('rank.title', 1)], info)
def test_explicit_geo2d_index(self):
@@ -245,7 +246,7 @@ class IndexesTest(unittest.TestCase):
Place.ensure_indexes()
info = Place._get_collection().index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('location.point', '2d')], info)
def test_explicit_geo2d_index_embedded(self):
@@ -268,7 +269,7 @@ class IndexesTest(unittest.TestCase):
Place.ensure_indexes()
info = Place._get_collection().index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('current.location.point', '2d')], info)
def test_explicit_geosphere_index(self):
@@ -288,7 +289,7 @@ class IndexesTest(unittest.TestCase):
Place.ensure_indexes()
info = Place._get_collection().index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('location.point', '2dsphere')], info)
def test_explicit_geohaystack_index(self):
@@ -310,7 +311,7 @@ class IndexesTest(unittest.TestCase):
Place.ensure_indexes()
info = Place._get_collection().index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('location.point', 'geoHaystack')], info)
def test_create_geohaystack_index(self):
@@ -322,7 +323,7 @@ class IndexesTest(unittest.TestCase):
Place.create_index({'fields': (')location.point', 'name')}, bucketSize=10)
info = Place._get_collection().index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('location.point', 'geoHaystack'), ('name', 1)], info)
def test_dictionary_indexes(self):
@@ -355,7 +356,7 @@ class IndexesTest(unittest.TestCase):
info = [(value['key'],
value.get('unique', False),
value.get('sparse', False))
for key, value in info.iteritems()]
for key, value in iteritems(info)]
self.assertIn(([('addDate', -1)], True, True), info)
BlogPost.drop_collection()
@@ -576,7 +577,7 @@ class IndexesTest(unittest.TestCase):
else:
self.assertEqual(BlogPost.objects.hint([('ZZ', 1)]).count(), 10)
self.assertEqual(BlogPost.objects.hint(TAGS_INDEX_NAME ).count(), 10)
self.assertEqual(BlogPost.objects.hint(TAGS_INDEX_NAME).count(), 10)
with self.assertRaises(Exception):
BlogPost.objects.hint(('tags', 1)).next()
@@ -806,7 +807,7 @@ class IndexesTest(unittest.TestCase):
self.fail('Unbound local error at index + pk definition')
info = BlogPost.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
index_item = [('_id', 1), ('comments.comment_id', 1)]
self.assertIn(index_item, info)
@@ -854,7 +855,7 @@ class IndexesTest(unittest.TestCase):
}
info = MyDoc.objects._collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
info = [value['key'] for key, value in iteritems(info)]
self.assertIn([('provider_ids.foo', 1)], info)
self.assertIn([('provider_ids.bar', 1)], info)
@@ -936,7 +937,6 @@ class IndexesTest(unittest.TestCase):
# Drop the temporary database at the end
connection.drop_database('tempdatabase')
def test_index_dont_send_cls_option(self):
"""
Ensure that 'cls' option is not sent through ensureIndex. We shouldn't

View File

@@ -2,6 +2,8 @@
import unittest
import warnings
from six import iteritems
from mongoengine import (BooleanField, Document, EmbeddedDocument,
EmbeddedDocumentField, GenericReferenceField,
IntField, ReferenceField, StringField, connect)
@@ -485,7 +487,7 @@ class InheritanceTest(unittest.TestCase):
meta = {'abstract': True}
class Human(Mammal): pass
for k, v in defaults.iteritems():
for k, v in iteritems(defaults):
for cls in [Animal, Fish, Guppy]:
self.assertEqual(cls._meta[k], v)

View File

@@ -9,6 +9,7 @@ import weakref
from datetime import datetime
from bson import DBRef, ObjectId
from pymongo.errors import DuplicateKeyError
from six import iteritems
from tests import fixtures
from tests.fixtures import (PickleEmbedded, PickleTest, PickleSignalsTest,
@@ -1482,7 +1483,7 @@ class InstanceTest(MongoDBTestCase):
Message.drop_collection()
# All objects share the same id, but each in a different collection
user = User(id=1, name='user-name')#.save()
user = User(id=1, name='user-name') # .save()
message = Message(id=1, author=user).save()
message.author.name = 'tutu'
@@ -2000,7 +2001,6 @@ class InstanceTest(MongoDBTestCase):
child_record.delete()
self.assertEqual(Record.objects(name='parent').get().children, [])
def test_reverse_delete_rule_with_custom_id_field(self):
"""Ensure that a referenced document with custom primary key
is also deleted upon deletion.
@@ -3059,7 +3059,7 @@ class InstanceTest(MongoDBTestCase):
def expand(self):
self.flattened_parameter = {}
for parameter_name, parameter in self.parameters.iteritems():
for parameter_name, parameter in iteritems(self.parameters):
parameter.expand()
class NodesSystem(Document):
@@ -3067,7 +3067,7 @@ class InstanceTest(MongoDBTestCase):
nodes = MapField(ReferenceField(Node, dbref=False))
def save(self, *args, **kwargs):
for node_name, node in self.nodes.iteritems():
for node_name, node in iteritems(self.nodes):
node.expand()
node.save(*args, **kwargs)
super(NodesSystem, self).save(*args, **kwargs)
@@ -3381,7 +3381,6 @@ class InstanceTest(MongoDBTestCase):
class User(Document):
company = ReferenceField(Company)
# Ensure index creation exception aren't swallowed (#1688)
with self.assertRaises(DuplicateKeyError):
User.objects().select_related()