Index: tracvote/__init__.py
===================================================================
--- tracvote/__init__.py	(revision 14232)
+++ tracvote/__init__.py	(working copy)
@@ -99,29 +99,24 @@
     the current version has to be retrieved separately.
     """
     realm = resource.realm
+    resource.version = 0
     if realm == 'ticket':
-        db = env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        for tkt_changes, in env.db_query("""
             SELECT SUM(c.change) FROM (
                 SELECT 1 as change
                   FROM ticket_change
                  WHERE ticket=%s
                  GROUP BY time) AS c
-            """, (resource.id,))
-        tkt_changes = cursor.fetchone()
-        resource.version = tkt_changes and tkt_changes[0] or 0
+            """, (resource.id,)):
+            resource.version = tkt_changes
     elif realm == 'wiki':
-        db = env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        for version in env.db_query("""
             SELECT version
               FROM wiki
              WHERE name=%s
              ORDER BY version DESC LIMIT 1
-            """, (resource.id,))
-        page_version = cursor.fetchone()
-        resource.version = page_version and int(page_version[0]) or 0
+            """, (resource.id,)):
+            resource.version = version
     return resource
 
 def resource_from_path(env, path):
@@ -200,63 +195,58 @@
             args.append(realm)
         if top:
             args.append(top)
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        for row in self.env.db_query("""
             SELECT realm,resource_id,SUM(vote) AS count
               FROM votes%s
              GROUP by realm,resource_id
              ORDER by count DESC,resource_id%s
-        """ % (realm and ' WHERE realm=%s' or '', top and ' LIMIT %s' or ''),
-            args)
-        rows = cursor.fetchall()
-        for row in rows:
+            """ % (' WHERE realm=%s' if realm else '',
+                   ' LIMIT %s' if top else ''),
+            args):
             yield row
 
     def get_vote_counts(self, resource):
         """Get negative, total and positive vote counts and return them in a
         tuple.
         """
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
-            SELECT sum(vote)
-              FROM votes
-             WHERE realm=%s
-               AND resource_id=%s
-        """, (resource.realm, resource.id))
-        total = cursor.fetchone()[0] or 0
-        cursor.execute("""
-            SELECT sum(vote)
-              FROM votes
-             WHERE vote < 0
-               AND realm=%s
-               AND resource_id=%s
-        """, (resource.realm, resource.id))
-        negative = cursor.fetchone()[0] or 0
-        cursor.execute("""
-            SELECT sum(vote)
-              FROM votes
-             WHERE vote > 0
-               AND realm=%s
-               AND resource_id=%s
-        """, (resource.realm, to_unicode(resource.id)))
-        positive = cursor.fetchone()[0] or 0
-        return (negative, total, positive)
+        with self.env.db_query as db:
+            total = negative = positive = 0
+            for sum_vote, in db("""
+                SELECT sum(vote)
+                  FROM votes
+                 WHERE realm=%s
+                   AND resource_id=%s
+                """, (resource.realm, resource.id)):
+                total = sum_vote
+            for sum_vote, in db("""
+                SELECT sum(vote)
+                  FROM votes
+                 WHERE vote < 0
+                   AND realm=%s
+                   AND resource_id=%s
+                """, (resource.realm, resource.id)):
+                negative = sum_vote
+            for sum_vote, in db("""
+                SELECT sum(vote)
+                  FROM votes
+                 WHERE vote > 0
+                   AND realm=%s
+                   AND resource_id=%s
+                """, (resource.realm, to_unicode(resource.id))):
+                positive = sum_vote
+            return (negative or 0, total or 0, positive or 0)
 
     def get_vote(self, req, resource):
         """Return the current users vote for a resource."""
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        for vote, in self.env.db_query("""
             SELECT vote
               FROM votes
              WHERE username=%s
                AND realm=%s
                AND resource_id=%s
-        """, (get_reporter_id(req), resource.realm, to_unicode(resource.id)))
-        row = cursor.fetchone()
-        return row and row[0]
+            """, (get_reporter_id(req), resource.realm,
+                  to_unicode(resource.id))):
+            return vote
 
     def set_vote(self, req, resource, vote):
         """Vote for a resource."""
@@ -265,99 +255,89 @@
                      resource.realm, to_unicode(resource.id)))
         if not resource.version:
             args.pop(1)
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
-            UPDATE votes
-               SET changetime=%%s%s,vote=%%s
-             WHERE username=%%s
-               AND realm=%%s
-               AND resource_id=%%s
-        """ % (resource.version and ',version=%s' or ''), args)
-        if self.get_vote(req, resource) is None:
+        with self.env.db_transaction as db:
+            cursor = db.cursor()
             cursor.execute("""
-                INSERT INTO votes
-                    (realm,resource_id,version,username,vote,time,changetime)
-                VALUES (%s,%s,%s,%s,%s,%s,%s)
-            """, (resource.realm, to_unicode(resource.id), resource.version,
-                  get_reporter_id(req), vote, now_ts, now_ts))
-        db.commit()
+                UPDATE votes
+                   SET changetime=%%s%s,vote=%%s
+                 WHERE username=%%s
+                   AND realm=%%s
+                   AND resource_id=%%s
+            """ % (',version=%s' if resource.version else ''), args)
+            if self.get_vote(req, resource) is None:
+                cursor.execute("""
+                    INSERT INTO votes
+                      (realm,resource_id,version,username,vote,time,changetime)
+                    VALUES (%s,%s,%s,%s,%s,%s,%s)
+                """, (resource.realm, to_unicode(resource.id), resource.version,
+                      get_reporter_id(req), vote, now_ts, now_ts))
 
     def reparent_votes(self, resource, old_id):
         """Update resource reference of votes on a renamed resource."""
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        self.env.db_transaction("""
             UPDATE votes
                SET resource_id=%s
              WHERE realm=%s
                AND resource_id=%s
-        """, (to_unicode(resource.id), resource.realm, to_unicode(old_id)))
-        db.commit()
+            """, (to_unicode(resource.id), resource.realm, to_unicode(old_id)))
 
     def delete_votes(self, resource):
         """Delete votes for a resource."""
         args = list((resource.realm, to_unicode(resource.id)))
         if resource.version:
             args.append(resource.version)
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        self.env.db_transaction("""
             DELETE
               FROM votes
              WHERE realm=%%s
                AND resource_id=%%s%s
-        """ % (resource.version and ' AND version=%s' or ''), args)
-        db.commit()
+            """ % (' AND version=%s' if resource.version else ''), args)
 
     def get_votes(self, req, resource=None, top=0):
         """Return most recent votes, optionally only for one resource."""
-        args = resource and [resource.realm, to_unicode(resource.id)] or []
+        args = [resource.realm, to_unicode(resource.id)] if resource else []
         if top:
             args.append(top)
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
+        for row in self.env.db_query("""
             SELECT realm,resource_id,vote,username,changetime
               FROM votes
              WHERE changetime is not NULL%s
              ORDER BY changetime DESC%s
-        """ % (resource and ' AND realm=%s AND resource_id=%s' or '',
-               top and ' LIMIT %s' or ''), args)
-        rows = cursor.fetchall()
-        for row in rows:
+            """ % (' AND realm=%s AND resource_id=%s' if resource else '',
+                   ' LIMIT %s' if top else ''), args):
             yield row
 
     def get_total_vote_count(self, realm):
         """Return the total vote count for a realm, like 'ticket'."""
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute('SELECT sum(vote) FROM votes WHERE resource LIKE %s',
-                       (realm + '%',))
-        total = cursor.fetchone()[0] or 0
-        cursor.execute("""
-            SELECT sum(vote)
-              FROM votes
-             WHERE vote < 0
-               AND resource LIKE %s
-        """, (realm + '%',))
-        negative = cursor.fetchone()[0] or 0
-        cursor.execute("""
-            SELECT sum(vote)
-              FROM votes
-             WHERE vote > 0
-               AND resource=%s
-        """, (realm + '%',))
-        positive = cursor.fetchone()[0] or 0
-        return (negative, total, positive)
+        with self.env.db_query as db:
+            total = negative = positive = 0
+            for sum_vote, in db(
+                    'SELECT sum(vote) FROM votes WHERE resource LIKE %s',
+                    (realm + '%',)):
+                total = sum_vote
+            for sum_vote, in db("""
+                SELECT sum(vote)
+                  FROM votes
+                 WHERE vote < 0
+                   AND resource LIKE %s
+                """, (realm + '%',)):
+                negative = sum_vote
+            for sum_vote, in db("""
+                SELECT sum(vote)
+                  FROM votes
+                 WHERE vote > 0
+                   AND resource=%s
+                """, (realm + '%',)):
+                positive = sum_vote
+            return (negative, total, positive)
 
     def get_realm_votes(self, realm):
         """Return a dictionary of vote count for a realm."""
-        db = self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute('SELECT resource FROM votes WHERE resource LIKE %s',
-                       (realm + '%',))
-        resources = set([i[0] for i in cursor.fetchall()])
+        resources = set()
+        for i, in self.env.db_query(
+                'SELECT resource FROM votes WHERE resource LIKE %s',
+                (realm + '%',)):
+            resources.add(i)
         votes = {}
         for resource in resources:
             votes[resource] = self.get_vote_counts(resource)
@@ -400,7 +380,7 @@
         match = self.path_match.match(req.path_info)
         vote, path = match.groups()
         resource = resource_from_path(self.env, path)
-        vote = vote == 'up' and +1 or -1
+        vote = +1 if vote == 'up' else -1
         old_vote = self.get_vote(req, resource)
 
         # Protect against CSRF attacks: Validate the token like done in Trac
@@ -511,7 +491,7 @@
             top = 5
         else:
             args, kw = parse_args(content)
-            compact = 'compact' in args and True
+            compact = 'compact' in args
             top = as_int(kw.get('top'), 5, min=0)
 
         if name == 'LastVoted':
@@ -523,12 +503,13 @@
                          % (format_author(i[3]),
                             format_datetime(to_datetime(i[4]))))
                 lst(tag.li(tag.a(
-                    get_resource_description(env, resource, compact and
-                                             'compact' or 'default'),
+                    get_resource_description(
+                                env, resource, 
+                                'compact' if compact else 'default'),
                     href=get_resource_url(env, resource, formatter.href),
-                    title=(compact and '%+i %s' % (i[2], voted) or None)),
-                    (not compact and Markup(' %s %s' % (tag.b('%+i' % i[2]),
-                                                        voted)) or '')))
+                    title=('%+i %s' % (i[2], voted) if compact else None)),
+                    (Markup(' %s %s' % (tag.b('%+i' % i[2]),
+                                        voted)) if not compact else '')))
             return lst
 
         elif name == 'TopVoted':
@@ -539,11 +520,12 @@
                     break
                 resource = Resource(i[0], i[1])
                 lst(tag.li(tag.a(
-                    get_resource_description(env, resource, compact and
-                                             'compact' or 'default'),
+                    get_resource_description(
+                                env, resource,
+                                'compact' if compact else 'default'),
                     href=get_resource_url(env, resource, formatter.href),
-                    title=(compact and '%+i' % i[2] or None)),
-                    (not compact and ' (%+i)' % i[2] or '')))
+                    title=('%+i' % i[2] if compact else None)),
+                    (' (%+i)' % i[2] if not compact else '')))
             return lst
 
         elif name == 'VoteList':
@@ -551,11 +533,10 @@
             resource = resource_from_path(env, req.path_info)
             for i in self.get_votes(req, resource, top=top):
                 vote = ('at %s' % format_datetime(to_datetime(i[4])))
-                lst(tag.li(
-                    compact and format_author(i[3]) or
+                lst(tag.li(format_author(i[3]) if compact else
                     Markup(u'%s by %s %s' % (tag.b('%+i' % i[2]),
                                              tag(format_author(i[3])), vote)),
-                    title=(compact and '%+i %s' % (i[2], vote) or None)))
+                    title=('%+i %s' % (i[2], vote) if compact else None)))
             return lst
 
     ### IEnvironmentSetupParticipant methods
@@ -563,84 +544,85 @@
     def environment_created(self):
         pass
 
-    def environment_needs_upgrade(self, db):
-        if self.get_schema_version(db) < self.schema_version:
+    def environment_needs_upgrade(self):
+        schema_ver = self.get_schema_version()
+        if schema_ver < self.schema_version:
             return True
-        elif self.get_schema_version(db) > self.schema_version:
+        elif schema_ver > self.schema_version:
             raise TracError(
                 "A newer version of VotePlugin has been installed before, "
                 "but downgrading is unsupported.")
         return False
 
-    def upgrade_environment(self, db):
+    def upgrade_environment(self):
         """Each schema version should have its own upgrade module, named
         upgrades/dbN.py, where 'N' is the version number (int).
         """
         db_mgr = DatabaseManager(self.env)
-        schema_ver = self.get_schema_version(db)
+        schema_ver = self.get_schema_version()
 
-        cursor = db.cursor()
-        # Is this a new installation?
-        if not schema_ver:
-            # Perform a single-step install: Create plugin schema and
-            # insert default data into the database.
-            connector = db_mgr._get_connector()[0]
-            for table in self.schema:
-                for stmt in connector.to_sql(table):
-                    self.env.log.debug(stmt)
-                    cursor.execute(stmt)
-            for table, cols, vals in self.db_data:
-                cursor.executemany("INSERT INTO %s (%s) VALUES (%s)"
-                                   % (table, ','.join(cols),
-                                      ','.join(['%s' for c in cols])), vals)
-        elif schema_ver < self.schema_version:
-            # Perform incremental upgrades.
-            for i in range(schema_ver + 1, self.schema_version + 1):
-                name  = 'db%i' % i
-                try:
-                    upgrades = __import__('tracvote.upgrades', globals(),
-                                          locals(), [name])
-                    script = getattr(upgrades, name)
-                except AttributeError:
-                    raise TracError("No upgrade module for version "
-                                    "%(num)i (%(version)s.py)",
-                                    num=i, version=name)
-                script.do_upgrade(self.env, i, cursor)
-        else:
-            # Obsolete call handled gracefully.
-            return
-        cursor.execute("""
-            UPDATE system
-               SET value=%s
-             WHERE name='vote_version'
-            """, (self.schema_version,))
-        self.log.info("Upgraded VotePlugin db schema from version %d to %d"
-                      % (schema_ver, self.schema_version))
-        db.commit()
+        with self.env.db_transaction as db:
+            cursor = db.cursor()
+            # Is this a new installation?
+            if not schema_ver:
+                # Perform a single-step install: Create plugin schema and
+                # insert default data into the database.
+                connector = db_mgr._get_connector()[0]
+                for table in self.schema:
+                    for stmt in connector.to_sql(table):
+                        self.env.log.debug(stmt)
+                        cursor.execute(stmt)
+                for table, cols, vals in self.db_data:
+                    cursor.executemany("INSERT INTO %s (%s) VALUES (%s)"
+                                       % (table, ','.join(cols),
+                                          ','.join(['%s' for c in cols])), vals)
+            elif schema_ver < self.schema_version:
+                # Perform incremental upgrades.
+                for i in range(schema_ver + 1, self.schema_version + 1):
+                    name  = 'db%i' % i
+                    try:
+                        upgrades = __import__('tracvote.upgrades', globals(),
+                                              locals(), [name])
+                        script = getattr(upgrades, name)
+                    except AttributeError:
+                        raise TracError("No upgrade module for version "
+                                        "%(num)i (%(version)s.py)",
+                                        num=i, version=name)
+                    script.do_upgrade(self.env, i, cursor)
+            else:
+                # Obsolete call handled gracefully.
+                return
+            cursor.execute("""
+                UPDATE system
+                   SET value=%s
+                 WHERE name='vote_version'
+                """, (self.schema_version,))
+            self.log.info("Upgraded VotePlugin db schema from version %d to %d"
+                          % (schema_ver, self.schema_version))
 
     ### Internal methods
 
-    def get_schema_version(self, db=None):
+    def get_schema_version(self):
         """Return the current schema version for this plugin."""
-        db = db and db or self.env.get_db_cnx()
-        cursor = db.cursor()
-        cursor.execute("""
-            SELECT value
-              FROM system
-             WHERE name='vote_version'
-        """)
-        row = cursor.fetchone()
-        schema_ver = row and int(row[0]) or 0
-        if schema_ver > 1:
-            # The expected outcome for any recent installation.
-            return schema_ver
-        # Care for pre-tracvote-0.2 installations.
-        dburi = self.config.get('trac', 'database')
-        tables = self._get_tables(dburi, cursor)
-        if 'votes' in tables:
-            return 1
-        # This is a new installation.
-        return 0
+        schema_ver = 0
+        with self.env.db_query as db:
+            for value, in db("""
+                SELECT value
+                  FROM system
+                 WHERE name='vote_version'
+                """):
+                schema_ver = int(value)
+            if schema_ver > 1:
+                # The expected outcome for any recent installation.
+                return schema_ver
+            # Care for pre-tracvote-0.2 installations.
+            dburi = self.config.get('trac', 'database')
+            cursor = db.cursor()
+            tables = self._get_tables(dburi, cursor)
+            if 'votes' in tables:
+                return 1
+            # This is a new installation.
+            return 0
 
     def render_voter(self, req):
         path = req.path_info.strip('/')
@@ -676,14 +658,14 @@
         """Return a tuple of (body_text, title_text) describing the votes on a
         resource.
         """
-        negative, total, positive = resource and \
-                                    self.get_vote_counts(resource) or (0,0,0)
+        negative, total, positive = self.get_vote_counts(resource) if resource \
+            else (0,0,0)
         count_detail = ['%+i' % i for i in (positive, negative) if i]
         if count_detail:
             count_detail = ' (%s)' % ', '.join(count_detail)
         else:
             count_detail = ''
-        return ('%+i' % total, 'Vote count%s' % count_detail)
+        return ('%+i' % total, 'VOTE2 count%s' % count_detail)
 
     def _get_tables(self, dburi, cursor):
         """Code from TracMigratePlugin by Jun Omae (see tracmigrate.admin)."""
Index: tracvote/tests/main.py
===================================================================
--- tracvote/tests/main.py	(revision 14232)
+++ tracvote/tests/main.py	(working copy)
@@ -32,11 +32,9 @@
         self.env = EnvironmentStub(enable=['trac.*'])
         self.env.path = tempfile.mkdtemp()
         self.db_mgr = DatabaseManager(self.env)
-        self.db = self.env.get_db_cnx()
         self.votes = VoteSystem(self.env)
 
     def tearDown(self):
-        self.db.close()
         # Really close db connections.
         self.env.shutdown()
         shutil.rmtree(self.env.path)
@@ -44,38 +42,41 @@
     # Helpers
 
     def _schema_init(self, schema=None):
-        cursor = self.db.cursor()
-        cursor.execute("DROP TABLE IF EXISTS votes")
-        if schema:
-            connector = self.db_mgr._get_connector()[0]
-            for table in schema:
-                for stmt in connector.to_sql(table):
-                    cursor.execute(stmt)
+        with self.env.db_transaction as db:
+            cursor = db.cursor()
+            cursor.execute("DROP TABLE IF EXISTS votes")
+            if schema:
+                connector = self.db_mgr._get_connector()[0]
+                for table in schema:
+                    for stmt in connector.to_sql(table):
+                        cursor.execute(stmt)
 
     def _verify_curr_schema(self):
-        self.assertFalse(self.votes.environment_needs_upgrade(self.db))
-        cursor = self.db.cursor()
-        cursor.execute('SELECT * FROM votes')
-        cols = get_column_names(cursor)
-        self.assertTrue('resource' not in cols)
-        self.assertEquals(['realm', 'resource_id', 'version', 'username',
-                           'vote', 'time', 'changetime'], cols)
-        cursor.execute("""
-            SELECT value
-              FROM system
-             WHERE name='vote_version'
-        """)
-        schema_ver = int(cursor.fetchone()[0])
-        self.assertEquals(self.votes.schema_version, schema_ver)
+        self.assertFalse(self.votes.environment_needs_upgrade())
+        with self.env.db_query as db:
+            cursor = db.cursor()
+            cursor.execute('SELECT * FROM votes')
+            cols = get_column_names(cursor)
+            self.assertTrue('resource' not in cols)
+            self.assertEquals(['realm', 'resource_id', 'version', 'username',
+                               'vote', 'time', 'changetime'], cols)
+            cursor.execute("""
+                SELECT value
+                  FROM system
+                 WHERE name='vote_version'
+            """)
+            schema_ver = int(cursor.fetchone()[0])
+            self.assertEquals(self.votes.schema_version, schema_ver)
 
     def _verify_schema_unregistered(self):
-        cursor = self.db.cursor()
-        cursor.execute("""
-            SELECT value
-              FROM system
-             WHERE name='vote_version'
-        """)
-        self.assertFalse(cursor.fetchone())
+        with self.env.db_query as db:
+            cursor = db.cursor()
+            cursor.execute("""
+                SELECT value
+                  FROM system
+                 WHERE name='vote_version'
+            """)
+            self.assertFalse(cursor.fetchone())
 
     # Tests
 
@@ -84,10 +85,10 @@
         #   Revert these changes for clean install testing.
         self._schema_init()
 
-        self.assertEquals(0, self.votes.get_schema_version(self.db))
-        self.assertTrue(self.votes.environment_needs_upgrade(self.db))
+        self.assertEquals(0, self.votes.get_schema_version())
+        self.assertTrue(self.votes.environment_needs_upgrade())
 
-        self.votes.upgrade_environment(self.db)
+        self.votes.upgrade_environment()
         self._verify_curr_schema()
 
     def test_upgrade_v1_to_current(self):
@@ -102,37 +103,38 @@
         self._schema_init(schema)
 
         # Populate tables with test data.
-        cursor = self.db.cursor()
-        cursor.executemany("""
-            INSERT INTO votes
-                   (resource,username,vote)
-            VALUES (%s,%s,%s)
-        """, (('ticket/1','user',-1), ('ticket/2','user',1),
-              ('wiki/DeletedPage','user',-1), ('wiki/ExistingPage','user',1)))
-        # Resources must exist for successful data migration.
-        t = Ticket(self.env, db=self.db)
-        t['summary'] = 'test ticket'
-        t.insert()
-        w = WikiPage(self.env, 'ExistingPage')
-        w.text = 'content'
-        w.save('author', 'comment', '::1')
-        self._verify_schema_unregistered()
-        self.assertEquals(1, self.votes.get_schema_version(self.db))
-        self.assertTrue(self.votes.environment_needs_upgrade(self.db))
+        with self.env.db_query as db:
+            cursor = db.cursor()
+            cursor.executemany("""
+                INSERT INTO votes
+                       (resource,username,vote)
+                VALUES (%s,%s,%s)
+            """, (('ticket/1','user',-1), ('ticket/2','user',1),
+                  ('wiki/DeletedPage','user',-1), ('wiki/ExistingPage','user',1)))
+            # Resources must exist for successful data migration.
+            t = Ticket(self.env)
+            t['summary'] = 'test ticket'
+            t.insert()
+            w = WikiPage(self.env, 'ExistingPage')
+            w.text = 'content'
+            w.save('author', 'comment', '::1')
+            self._verify_schema_unregistered()
+            self.assertEquals(1, self.votes.get_schema_version())
+            self.assertTrue(self.votes.environment_needs_upgrade())
 
-        # Data migration and registration of unversioned schema.
-        self.votes.upgrade_environment(self.db)
-        self._verify_curr_schema()
+            # Data migration and registration of unversioned schema.
+            self.votes.upgrade_environment()
+            self._verify_curr_schema()
 
-        cursor.execute('SELECT * FROM votes')
-        votes = cursor.fetchall()
-        t_votes = [id for realm,id,ver,u,v,t,c in votes if realm == 'ticket']
-        w_votes = [id for realm,id,ver,u,v,t,c in votes if realm == 'wiki']
-        self.assertTrue('1' in t_votes)
-        if resource_check:
-            self.assertFalse('2' in t_votes)
-            self.assertFalse('DeletedPage' in w_votes)
-        self.assertTrue('ExistingPage' in w_votes)
+            cursor.execute('SELECT * FROM votes')
+            votes = cursor.fetchall()
+            t_votes = [id for realm,id,ver,u,v,t,c in votes if realm == 'ticket']
+            w_votes = [id for realm,id,ver,u,v,t,c in votes if realm == 'wiki']
+            self.assertTrue('1' in t_votes)
+            if resource_check:
+                self.assertFalse('2' in t_votes)
+                self.assertFalse('DeletedPage' in w_votes)
+            self.assertTrue('ExistingPage' in w_votes)
 
 
 class VoteSystemTestCase(unittest.TestCase):
@@ -140,19 +142,24 @@
     def setUp(self):
         self.env = EnvironmentStub(default_data=True,
                                    enable=['trac.*', 'tracvote.*'])
+        # OK, this is a quick hack... I didn't have time to investigate
+        # why I got ''Cannot find implementation(s) of the `IPermissionPolicy`
+        # interface named `ReadonlyWikiPolicy`.''
+        self.env.config.set('trac', 'permission_policies',
+                            ', '.join(
+                ['DefaultPermissionPolicy',
+                 'LegacyAttachmentPolicy']))
         self.env.path = tempfile.mkdtemp()
         self.perm = PermissionSystem(self.env)
         self.req = Mock()
 
-        self.db = self.env.get_db_cnx()
         self.votes = VoteSystem(self.env)
         # Current tracvotes schema is setup with enabled component anyway.
         #   Revert these changes for getting default permissions inserted.
         self._revert_schema_init()
-        self.votes.upgrade_environment(self.db)
+        self.votes.upgrade_environment()
 
     def tearDown(self):
-        self.db.close()
         # Really close db connections.
         self.env.shutdown()
         shutil.rmtree(self.env.path)
@@ -160,11 +167,12 @@
     # Helpers
 
     def _revert_schema_init(self):
-        cursor = self.db.cursor()
-        cursor.execute("DROP TABLE IF EXISTS votes")
-        cursor.execute("DELETE FROM system WHERE name='vote_version'")
-        cursor.execute("DELETE FROM permission WHERE action %s"
-                       % self.db.like(), ('VOTE_%',))
+        with self.env.db_transaction as db:
+            cursor = db.cursor()
+            cursor.execute("DROP TABLE IF EXISTS votes")
+            cursor.execute("DELETE FROM system WHERE name='vote_version'")
+            cursor.execute("DELETE FROM permission WHERE action %s"
+                           % db.like(), ('VOTE_%',))
 
     # Tests
 
