#366 Ensure that Event.id is always set after `Event.get_or_create_from_event()`.
Merged 2 months ago by jkaluza. Opened 2 months ago by jkaluza.
jkaluza/freshmaker bob  into  master

file modified
+5 -2

@@ -201,8 +201,11 @@ 

          instance = cls.get(session, message_id)

          if instance:

              return instance

-         return cls.create(session, message_id, search_key, event_type,

-                           released=released, manual=manual, dry_run=dry_run)

+         instance = cls.create(

+             session, message_id, search_key, event_type,

+             released=released, manual=manual, dry_run=dry_run)

+         session.commit()

+         return instance

  

      @classmethod

      def get_or_create_from_event(cls, session, event, released=True):

file modified
+8

@@ -31,6 +31,14 @@ 

  

  class TestModels(helpers.ModelsTestCase):

  

+     def test_get_or_create_from_event(self):

+         event = events.TestingEvent('msg-1')

+         # First call creates new event, second call returns the same one.

+         for i in range(2):

+             db_event = Event.get_or_create_from_event(db.session, event)

+             self.assertEqual(db_event.id, 1)

+             self.assertEqual(db_event.message_id, 'msg-1')

+ 

      def test_creating_event_and_builds(self):

          event = Event.create(db.session, "test_msg_id", "RHSA-2017-284", events.TestingEvent)

          build = ArtifactBuild.create(db.session, event, "ed", "module", 1234)

Methods calling get_or_create_from_event() expect the Event.id to always
exist in the returned Event, but the Event.id is only set after the
db.session.commit().

Therefore in this commit, the session.commit() is called in the end
of get_or_create_from_event() to ensure the id is always set.

This fixes issues when @fail_event_on_handler_exception decorator
actually did not move the Event to failed state, because the Event.id
was None.

Commit 29de7eb fixes this pull-request

Pull-Request has been merged by jkaluza

2 months ago

Pull-Request has been merged by jkaluza

2 months ago