diff test/db_test_base.py @ 5395:23b8e6067f7c

Python 3 preparation: update calls to dict methods. Tool-assisted patch. Changes of iterkeys / itervalues / iteritems to keys / values / items are fully automated, but may make things less efficient in Python 2. Automated tools want to add list() around many calls to keys / values / items, but I thought most such list() additions were unnecessary because it seemed the result of keys / values / items was just iterated over while the set of dict keys remained unchanged, rather than used in a way requiring an actual list, or used while the set of keys in the dict could change. It's quite possible I missed some cases where list() was really needed, or left in some unnecessary list() calls. In cases where list() was only needed because the resulting list was then sorted in-place, I changed the code to use calls to sorted().
author Joseph Myers <jsm@polyomino.org.uk>
date Tue, 24 Jul 2018 23:04:42 +0000
parents 0fd27da353eb
children 0cde8a595893
line wrap: on
line diff
--- a/test/db_test_base.py	Tue Jul 24 23:03:35 2018 +0000
+++ b/test/db_test_base.py	Tue Jul 24 23:04:42 2018 +0000
@@ -1230,8 +1230,7 @@
         self.assertEqual(nodeid, '1')
         self.assertEqual(journaltag, self.db.user.lookup('admin'))
         self.assertEqual(action, 'create')
-        keys = params.keys()
-        keys.sort()
+        keys = sorted(params.keys())
         self.assertEqual(keys, [])
 
         # journal entry for link
@@ -2279,11 +2278,11 @@
             shutil.rmtree('_test_export')
 
         # compare with snapshot of the database
-        for cn, items in orig.iteritems():
+        for cn, items in orig.items():
             klass = self.db.classes[cn]
             propdefs = klass.getprops(1)
             # ensure retired items are retired :)
-            l = items.keys(); l.sort()
+            l = sorted(items.keys())
             m = klass.list(); m.sort()
             ae(l, m, '%s id list wrong %r vs. %r'%(cn, l, m))
             for id, props in items.items():
@@ -2299,8 +2298,8 @@
                             raise
                         # don't get hung up on rounding errors
                         assert not l.__cmp__(value, int_seconds=1)
-        for jc, items in origj.iteritems():
-            for id, oj in items.iteritems():
+        for jc, items in origj.items():
+            for id, oj in items.items():
                 rj = self.db.getjournal(jc, id)
                 # Both mysql and postgresql have some minor issues with
                 # rounded seconds on export/import, so we compare only
@@ -2525,8 +2524,7 @@
         # force any post-init stuff to happen
         self.db.post_init()
         props = self.db.issue.getprops()
-        keys = props.keys()
-        keys.sort()
+        keys = sorted(props.keys())
         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
             'creator', 'deadline', 'feedback', 'files', 'fixer', 'foo', 'id', 'messages',
             'nosy', 'priority', 'spam', 'status', 'superseder', 'title'])
@@ -2539,8 +2537,7 @@
         del self.db.issue.properties['title']
         self.db.post_init()
         props = self.db.issue.getprops()
-        keys = props.keys()
-        keys.sort()
+        keys = sorted(props.keys())
         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
             'creator', 'deadline', 'feedback', 'files', 'foo', 'id', 'messages',
             'nosy', 'priority', 'spam', 'status', 'superseder'])
@@ -2554,8 +2551,7 @@
         del self.db.issue.properties['title']
         self.db.post_init()
         props = self.db.issue.getprops()
-        keys = props.keys()
-        keys.sort()
+        keys = sorted(props.keys())
         self.assertEqual(keys, ['activity', 'actor', 'assignedto', 'creation',
             'creator', 'deadline', 'feedback', 'files', 'fixer', 'foo', 'id',
             'messages', 'nosy', 'priority', 'spam', 'status', 'superseder'])
@@ -2716,8 +2712,7 @@
     def test_fileClassProps(self):
         self.open_database()
         a = self.module.FileClass(self.db, 'a')
-        l = a.getprops().keys()
-        l.sort()
+        l = sorted(a.getprops().keys())
         self.assert_(l, ['activity', 'actor', 'content', 'created',
             'creation', 'type'])
 
@@ -3005,7 +3000,7 @@
             for x in range(4):
                 assert(('user', nodeid) in self.db.cache)
                 n = self.db.user.getnode(nodeid)
-                for k, v in user_result[nodeid].iteritems():
+                for k, v in user_result[nodeid].items():
                     ae((k, n[k]), (k, v))
                 for k in 'creation', 'activity':
                     assert(n[k])
@@ -3022,7 +3017,7 @@
             result.append(id)
             assert(('issue', id) in self.db.cache)
             n = self.db.issue.getnode(id)
-            for k, v in issue_result[id].iteritems():
+            for k, v in issue_result[id].items():
                 ae((k, n[k]), (k, v))
             for k in 'creation', 'activity':
                 assert(n[k])
@@ -3030,7 +3025,7 @@
             for x in range(4):
                 assert(('user', nodeid) in self.db.cache)
                 n = self.db.user.getnode(nodeid)
-                for k, v in user_result[nodeid].iteritems():
+                for k, v in user_result[nodeid].items():
                     ae((k, n[k]), (k, v))
                 for k in 'creation', 'activity':
                     assert(n[k])

Roundup Issue Tracker: http://roundup-tracker.org/