From 0f4b80c721423a0f3881b13e41e81600112b4cea Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Dec 07 2017 11:58:49 +0000
Subject: Return only INITIALIZED, BUILDING or COMPLETE events in Event.get_unreleased. Fix tests broken by previous commits.


---

diff --git a/freshmaker/models.py b/freshmaker/models.py
index 5ff5305..d5edc94 100644
--- a/freshmaker/models.py
+++ b/freshmaker/models.py
@@ -202,8 +202,28 @@ class Event(FreshmakerBase):
                                  released=released, manual=event.manual)
 
     @classmethod
-    def get_unreleased(cls, session):
-        return session.query(cls).filter_by(released=False).all()
+    def get_unreleased(cls, session, states=None):
+        """
+        Returns list of all unreleased events in given states. If no states
+        are provided, returns only events in INTIALIZED, BUILDING or COMPLETE
+        state.
+        :param session: db.session
+        :param list states: List of states to filter events for. If None,
+            INITIALIZED, BUILDING and COMPLETE is used.
+        :rtype: list of models.Event.
+        :return: List of unreleased events of `states` state.
+        """
+        if not states:
+            states = [EventState.INITIALIZED.value,
+                      EventState.BUILDING.value,
+                      EventState.COMPLETE.value]
+        else:
+            states = [
+                state.value if isinstance(state, EventState) else state for
+                state in states
+            ]
+        return session.query(cls).filter(cls.released == False,
+                                         cls.state.in_(states)).all()
 
     @property
     def event_type(self):
diff --git a/tests/test_errata_advisory_rpms_signed_handler.py b/tests/test_errata_advisory_rpms_signed_handler.py
index b361300..13f49fd 100644
--- a/tests/test_errata_advisory_rpms_signed_handler.py
+++ b/tests/test_errata_advisory_rpms_signed_handler.py
@@ -189,7 +189,7 @@ class TestErrataAdvisoryRPMsSignedHandler(unittest.TestCase):
         build_first_batch.assert_not_called()
 
         db_event = Event.get(db.session, event.msg_id)
-        self.assertEqual(EventState.COMPLETE.value, db_event.state)
+        self.assertEqual(EventState.BUILDING.value, db_event.state)
 
     @patch('freshmaker.handlers.errata.ErrataAdvisoryRPMsSignedHandler.'
            'allow_build', return_value=True)
@@ -209,4 +209,4 @@ class TestErrataAdvisoryRPMsSignedHandler(unittest.TestCase):
         build_first_batch.assert_called_once()
 
         db_event = Event.get(db.session, event.msg_id)
-        self.assertEqual(EventState.COMPLETE.value, db_event.state)
+        self.assertEqual(EventState.BUILDING.value, db_event.state)
diff --git a/tests/test_errata_advisory_state_changed.py b/tests/test_errata_advisory_state_changed.py
index ef55d9e..101c881 100644
--- a/tests/test_errata_advisory_state_changed.py
+++ b/tests/test_errata_advisory_state_changed.py
@@ -745,6 +745,7 @@ class TestFindEventsToInclude(unittest.TestCase):
         ev = Event.get_or_create(
             db.session, "msg2", "old_event_foo", ErrataAdvisoryRPMsSignedEvent,
             released=False)
+        ev.state = EventState.COMPLETE
         ArtifactBuild.create(db.session, ev, "foo", "image", 0)
 
         ev = Event.get_or_create(
@@ -915,7 +916,7 @@ class TestErrataAdvisoryStateChangedHandler(unittest.TestCase):
         db.session.commit()
 
         event = ErrataAdvisoryStateChangedEvent(
-            'msg-id-123', '123456', 'SHIPPED_LIVE')
+            'msg-id-123', 123456, 'SHIPPED_LIVE')
         handler = ErrataAdvisoryStateChangedHandler()
         msgs = handler.handle(event)
 
diff --git a/tests/test_models.py b/tests/test_models.py
index ef5c244..76657f9 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -23,7 +23,7 @@
 import unittest
 
 from freshmaker import db, events
-from freshmaker.models import Event, ArtifactBuild
+from freshmaker.models import Event, ArtifactBuild, EventState
 from freshmaker.types import ArtifactBuildState
 
 
@@ -141,3 +141,30 @@ class TestModels(unittest.TestCase):
             for build in [build2, build3, build4]:
                 self.assertEqual(build4.state, ArtifactBuildState.BUILD.value)
                 self.assertEqual(build4.state_reason, None)
+
+
+    def test_get_unreleased(self):
+        event1 = Event.create(db.session, "test_msg_id1", "test", events.TestingEvent)
+        event1.state = EventState.COMPLETE
+        event1.released = False
+
+        event2 = Event.create(db.session, "test_msg_id2", "test", events.TestingEvent)
+        event2.state = EventState.COMPLETE
+        event2.released = True
+
+        event3 = Event.create(db.session, "test_msg_id3", "test", events.TestingEvent)
+        event3.state = EventState.SKIPPED
+        event3.released = False
+
+        event4 = Event.create(db.session, "test_msg_id4", "test", events.TestingEvent)
+        event4.state = EventState.SKIPPED
+        event4.released = True
+        db.session.commit()
+
+        # No state means only COMPLETE should be returned
+        ret = Event.get_unreleased(db.session)
+        self.assertEqual(ret, [event1])
+
+        # No state means only COMPLETE should be returned
+        ret = Event.get_unreleased(db.session, states=[EventState.SKIPPED])
+        self.assertEqual(ret, [event3])

