#262 Flask-Sqlalchemy 3.x
Closed 2 years ago by frantisekz. Opened 2 years ago by frantisekz.

file modified
+4 -3
@@ -19,7 +19,7 @@ 

  from alembic import command as al_command

  from alembic import script

  from alembic.migration import MigrationContext

- from sqlalchemy.engine import reflection

+ from sqlalchemy import inspect

  

  

  def get_alembic_config():
@@ -48,7 +48,7 @@ 

  

      # check whether the table 'milestone' exists

      # if it does not, we assume that the database is empty

-     insp = reflection.Inspector.from_engine(db.engine)

+     insp = inspect(db.engine)

      table_names = insp.get_table_names()

      if 'milestone' not in table_names:

          print(" - Creating tables")
@@ -361,7 +361,8 @@ 

          setup_logging()

  

      # run the requested command

-     args.func(args)

+     with app.app_context():

+         args.func(args)

  

  

  if __name__ == '__main__':

file modified
+1 -2
@@ -7,8 +7,7 @@ 

  bodhi-client >= 5.7.5

  email_validator

  Flask-Admin

- # FIXME: https://pagure.io/fedora-qa/blockerbugs/issue/260

- Flask-SQLAlchemy ~= 2.5

+ Flask-SQLAlchemy

  Flask-WTF

  iso8601

  Jinja2

file modified
+3
@@ -21,6 +21,8 @@ 

  class TestRestAPI(object):

      @classmethod

      def setup_class(cls):

+         cls.context = app.app_context()

+         cls.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -81,6 +83,7 @@ 

      def teardown_class(cls):

          db.session.rollback()

          db.drop_all()

+         cls.context.pop()

  

      # === /api/v0/milestones ===

  

@@ -110,6 +110,8 @@ 

  class TestEmptyDB(object):

      @classmethod

      def setup_class(cls):

+         cls.context = app.app_context()

+         cls.context.push()

          cls.client = app.test_client()

          db.session.rollback()

          db.drop_all()
@@ -119,6 +121,7 @@ 

      def teardown_class(cls):

          db.session.rollback()

          db.drop_all()

+         cls.context.pop()

  

      def test_index_raises(self):

          "An empty db means no milestone to display, should raise"
@@ -130,6 +133,8 @@ 

  class TestNotSyncedMilestone(object):

      @classmethod

      def setup_class(cls):

+         cls.context = app.app_context()

+         cls.context.push()

          cls.client = app.test_client()

          db.session.rollback()

          db.drop_all()
@@ -142,6 +147,7 @@ 

      def teardown_class(cls):

          db.session.rollback()

          db.drop_all()

+         cls.context.pop()

  

      def test_not_synced_index(self):

          rv = self.client.get('/', follow_redirects=True)
@@ -169,6 +175,8 @@ 

  class TestSyncedMilestone(object):

      @classmethod

      def setup_class(cls):

+         cls.context = app.app_context()

+         cls.context.push()

          cls.client = app.test_client()

          db.session.rollback()

          db.drop_all()
@@ -186,6 +194,7 @@ 

      def teardown_class(cls):

          db.session.rollback()

          db.drop_all()

+         cls.context.pop()

  

      def test_display_index(self):

          rv = self.client.get('/', follow_redirects=True)
@@ -223,6 +232,8 @@ 

  class TestGetFunctions(object):

      @classmethod

      def setup_method(self):

+         self.context = app.app_context()

+         self.context.push()

          self.client = app.test_client()

          db.session.rollback()

          db.drop_all()
@@ -278,6 +289,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def test_get_milestone_updates(self):

          updates = main.get_milestone_updates(self.milestone)

@@ -1,5 +1,5 @@ 

  import mock

- from blockerbugs import db

+ from blockerbugs import app, db

  from testing.test_controllers import add_release, add_milestone, add_bug

  from blockerbugs.util import pagure_interface

  from blockerbugs.util import discussion_sync
@@ -16,6 +16,8 @@ 

  

  class TestInactiveDiscussions(object):

      def setup_method(self):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -41,6 +43,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def test_closing_commenting(self, monkeypatch):

          stub_get_issue_call = mock.MagicMock(return_value=stub_issue_open)

file modified
+4 -1
@@ -4,7 +4,7 @@ 

  

  import pytest

  

- from blockerbugs import db, updatetype

+ from blockerbugs import app, db, updatetype

  from blockerbugs.models.update import Update

  from blockerbugs.models.release import Release

  from blockerbugs.models.bug import Bug
@@ -12,6 +12,8 @@ 

  

  class TestInit(object):

      def setup_method(self, method):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -52,6 +54,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      @pytest.mark.parametrize('blocker', ['proposed_blocker', 'accepted_blocker', 'accepted_0day',

                                           'accepted_prevrel', None])

file modified
+4 -1
@@ -2,7 +2,7 @@ 

  

  from datetime import datetime

  

- from blockerbugs import db

+ from blockerbugs import app, db

  from blockerbugs.models.update import Update

  from blockerbugs.models.release import Release

  from blockerbugs.models.milestone import Milestone
@@ -12,6 +12,8 @@ 

  

  class TestTestData(object):

      def setup_method(self, method):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -20,6 +22,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def test_remove_empty(self):

          '''Removing a non-existent release shouldn't crash'''

file modified
+4 -1
@@ -3,7 +3,7 @@ 

  import datetime

  import copy

  

- from blockerbugs import db

+ from blockerbugs import app, db

  from blockerbugs.models.update import Update

  from blockerbugs.models.release import Release

  from blockerbugs.models.bug import Bug
@@ -11,6 +11,8 @@ 

  

  class TestUpdateModel(object):

      def setup_method(self, method):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -55,6 +57,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def create_update(self):

          self.update = Update(updateid=self.updateinfo['updateid'],

file modified
+4 -1
@@ -4,7 +4,7 @@ 

  from blockerbugs.models.release import Release

  from blockerbugs.models.bug import Bug

  from blockerbugs.models.update import Update

- from blockerbugs import db

+ from blockerbugs import app, db

  

  

  def generate_release(number=99):
@@ -33,6 +33,8 @@ 

  

  class TestfuncBugModel(object):

      def setup_method(self, method):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -43,6 +45,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def test_add_release(self):

          ref_number = 99

file modified
+4 -1
@@ -5,7 +5,7 @@ 

  from blockerbugs.models.milestone import Milestone

  from blockerbugs.models.release import Release

  from blockerbugs.models.bug import Bug

- from blockerbugs import db

+ from blockerbugs import app, db

  from blockerbugs.util.bug_sync import BugSync

  

  templatebug = Munch({
@@ -57,6 +57,8 @@ 

  class TestfuncBugsync(object):

      @classmethod

      def setup_class(cls):

+         cls.context = app.app_context()

+         cls.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -77,6 +79,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         cls.context.pop()

  

      def setup_method(self, method):

          self.basicbug = copy.deepcopy(templatebug)

@@ -6,7 +6,7 @@ 

  from mock import patch, Mock

  from fedora.client import ServerError

  

- from blockerbugs import db

+ from blockerbugs import app, db

  from blockerbugs.models.milestone import Milestone

  from blockerbugs.models.release import Release

  from blockerbugs.models.update import Update
@@ -110,6 +110,8 @@ 

  

  class TestfuncUpdateSync(object):

      def setup_method(self, method):

+         self.context = app.app_context()

+         self.context.push()

          db.session.rollback()

          db.drop_all()

          db.create_all()
@@ -131,6 +133,7 @@ 

          db.session.rollback()

          db.session.close()

          db.drop_all()

+         self.context.pop()

  

      def test_search_updates_two_bugs_one_update(self):

          bug1 = add_bug(2000, 'testbug1', self.test_milestone99alpha)

Build succeeded.

My attempt is in #263, please have a look. I wanted to end with xunit-style classes, because they caused me a lot of headaches (before I found out they're incompatible with pytest fixtures), so it looks like heavy changes, but they are basically the same in each file. Also instead of manually controlling the context, I used the recommended fixture. But the overall principle remains the same.

Pull-Request has been closed by frantisekz

2 years ago