[copr] master: Refactor checking for unfinished actions into a separate function (we will use it on multiple places) (d94d26a)

bkabrda at fedoraproject.org bkabrda at fedoraproject.org
Wed Mar 27 09:40:35 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit d94d26ab592eba0e7eee3120aadd7b20a5f37cbc
Author: Bohuslav Kabrda <bkabrda at redhat.com>
Date:   Wed Mar 27 10:20:04 2013 +0100

    Refactor checking for unfinished actions into a separate function (we will use it on multiple places)


>---------------------------------------------------------------

 coprs_frontend/coprs/logic/coprs_logic.py          |   19 ++++++++++++++-----
 coprs_frontend/tests/coprs_test_case.py            |    2 ++
 .../tests/test_logic/test_coprs_logic.py           |   14 ++++++++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/coprs_frontend/coprs/logic/coprs_logic.py b/coprs_frontend/coprs/logic/coprs_logic.py
index abc8f0b..29aa6b0 100644
--- a/coprs_frontend/coprs/logic/coprs_logic.py
+++ b/coprs_frontend/coprs/logic/coprs_logic.py
@@ -9,6 +9,7 @@ from coprs import whoosheers
 
 class CoprsLogic(object):
     """Used for manipulating Coprs. All methods accept user object as a first argument, as this may be needed in future."""
+
     @classmethod
     def get(cls, user, username, coprname, **kwargs):
         with_builds = kwargs.get('with_builds', False)
@@ -91,9 +92,7 @@ class CoprsLogic(object):
 
     @classmethod
     def update(cls, user, copr, check_for_duplicates = True):
-        action = cls.unfinished_actions(user, copr).first()
-        if action:
-            raise exceptions.ActionInProgressException('Action in progress on this copr.', action)
+        cls.raise_if_unfinished_action(user, copr)
 
         existing = cls.exists_for_user(copr.owner, copr.name).first()
         if existing:
@@ -125,13 +124,23 @@ class CoprsLogic(object):
                           update({models.Copr.build_count: models.Copr.build_count + 1})
 
     @classmethod
-    def unfinished_actions(cls, user, copr):
+    def unfinished_actions_for(cls, user, copr):
         actions = models.Action.query.filter(models.Action.object_type=='copr').\
                                       filter(models.Action.object_id==copr.id).\
-                                      filter(models.Action.backend_result==0)
+                                      filter(models.Action.backend_result==helpers.BackendResultEnum('waiting'))
 
         return actions
 
+    @classmethod
+    def raise_if_unfinished_action(cls, user, copr):
+        """This method raises ActionInProgressException if given copr has an unfinished
+        action. Returns None otherwise.
+        """
+        unfinished_actions = cls.unfinished_actions_for(user, copr).all()
+        if unfinished_actions:
+            raise exceptions.ActionInProgressException('Action in progress on this copr.', unfinished_actions[0])
+
+
 class CoprPermissionsLogic(object):
     @classmethod
     def get(cls, user, copr, searched_user):
diff --git a/coprs_frontend/tests/coprs_test_case.py b/coprs_frontend/tests/coprs_test_case.py
index dcb0cb9..4f6043f 100644
--- a/coprs_frontend/tests/coprs_test_case.py
+++ b/coprs_frontend/tests/coprs_test_case.py
@@ -100,6 +100,8 @@ class CoprsTestCase(object):
 
     @pytest.fixture
     def f_actions(self):
+        # if using actions, we need to flush coprs into db, so that we can get their ids
+        self.f_db()
         self.a1 = models.Action(action_type=helpers.ActionTypeEnum('rename'),
                                 object_type='copr',
                                 object_id=self.c1.id,
diff --git a/coprs_frontend/tests/test_logic/test_coprs_logic.py b/coprs_frontend/tests/test_logic/test_coprs_logic.py
new file mode 100644
index 0000000..c3f365b
--- /dev/null
+++ b/coprs_frontend/tests/test_logic/test_coprs_logic.py
@@ -0,0 +1,14 @@
+import pytest
+
+from coprs.exceptions import ActionInProgressException
+from coprs.helpers import BackendResultEnum
+from coprs.logic.coprs_logic import CoprsLogic
+
+from tests.coprs_test_case import CoprsTestCase
+
+class TestCoprsLogic(CoprsTestCase):
+    def test_update_raises_if_copr_has_unfinished_actions(self, f_users, f_coprs, f_actions, f_db):
+        self.c1.name = 'foo'
+        with pytest.raises(ActionInProgressException):
+            CoprsLogic.update(self.u1, self.c1)
+        self.db.session.rollback()



More information about the copr-devel mailing list