From f6a12d9f803eed15507c101b1244b0e684e0fc23 Mon Sep 17 00:00:00 2001 From: Qixiang Wan Date: Dec 07 2017 09:49:44 +0000 Subject: Allow use enum type name while creating ArtifactBuild This allows creating/updating ArtifactBuild with ArtifactType.(type) and ArtifactBuildState.(state), it previously requires us to do that with ArtifactType.(type).value and ArtifactBuildState.(state).value. --- diff --git a/freshmaker/models.py b/freshmaker/models.py index af65806..5ff5305 100644 --- a/freshmaker/models.py +++ b/freshmaker/models.py @@ -346,6 +346,8 @@ class ArtifactBuild(FreshmakerBase): return field if field in [s.name.lower() for s in list(ArtifactBuildState)]: return ArtifactBuildState[field.upper()].value + if isinstance(field, ArtifactBuildState): + return field.value raise ValueError("%s: %s, not in %r" % (key, field, list(ArtifactBuildState))) @validates('type') @@ -354,6 +356,8 @@ class ArtifactBuild(FreshmakerBase): return field if field in [t.name.lower() for t in list(ArtifactType)]: return ArtifactType[field.upper()].value + if isinstance(field, ArtifactType): + return field.value raise ValueError("%s: %s, not in %r" % (key, field, list(ArtifactType))) def depending_artifact_builds(self): diff --git a/tests/test_brew_container_task_state_change_handler.py b/tests/test_brew_container_task_state_change_handler.py index 1ceb349..51310e3 100644 --- a/tests/test_brew_container_task_state_change_handler.py +++ b/tests/test_brew_container_task_state_change_handler.py @@ -74,14 +74,14 @@ class TestBrewContainerTaskStateChangeHandler(helpers.FreshmakerTestCase): e1 = models.Event.create(db.session, "test_msg_id", "RHSA-2018-001", events.TestingEvent) event = self.get_event_from_msg(get_fedmsg('brew_container_task_closed')) - base_build = models.ArtifactBuild.create(db.session, e1, 'test-product-docker', ArtifactType.IMAGE.value, event.task_id) + base_build = models.ArtifactBuild.create(db.session, e1, 'test-product-docker', ArtifactType.IMAGE, event.task_id) - build_0 = models.ArtifactBuild.create(db.session, e1, 'docker-up-0', ArtifactType.IMAGE.value, 0, - dep_on=base_build, state=ArtifactBuildState.PLANNED.value) - build_1 = models.ArtifactBuild.create(db.session, e1, 'docker-up-1', ArtifactType.IMAGE.value, 0, - dep_on=base_build, state=ArtifactBuildState.PLANNED.value) - build_2 = models.ArtifactBuild.create(db.session, e1, 'docker-up-2', ArtifactType.IMAGE.value, 0, - dep_on=base_build, state=ArtifactBuildState.PLANNED.value) + build_0 = models.ArtifactBuild.create(db.session, e1, 'docker-up-0', ArtifactType.IMAGE, 0, + dep_on=base_build, state=ArtifactBuildState.PLANNED) + build_1 = models.ArtifactBuild.create(db.session, e1, 'docker-up-1', ArtifactType.IMAGE, 0, + dep_on=base_build, state=ArtifactBuildState.PLANNED) + build_2 = models.ArtifactBuild.create(db.session, e1, 'docker-up-2', ArtifactType.IMAGE, 0, + dep_on=base_build, state=ArtifactBuildState.PLANNED) self.handler.handle(event) self.assertEqual(base_build.state, ArtifactBuildState.DONE.value) @@ -107,10 +107,10 @@ class TestBrewContainerTaskStateChangeHandler(helpers.FreshmakerTestCase): e1 = models.Event.create(db.session, "test_msg_id", "RHSA-2018-001", events.TestingEvent) event = self.get_event_from_msg(get_fedmsg('brew_container_task_failed')) - base_build = models.ArtifactBuild.create(db.session, e1, 'test-product-docker', ArtifactType.IMAGE.value, event.task_id) + base_build = models.ArtifactBuild.create(db.session, e1, 'test-product-docker', ArtifactType.IMAGE, event.task_id) - models.ArtifactBuild.create(db.session, e1, 'docker-up', ArtifactType.IMAGE.value, 0, - dep_on=base_build, state=ArtifactBuildState.PLANNED.value) + models.ArtifactBuild.create(db.session, e1, 'docker-up', ArtifactType.IMAGE, 0, + dep_on=base_build, state=ArtifactBuildState.PLANNED) self.handler.handle(event) self.assertEqual(base_build.state, ArtifactBuildState.FAILED.value) build_image.assert_not_called() diff --git a/tests/test_errata_advisory_state_changed.py b/tests/test_errata_advisory_state_changed.py index 24290f1..ef55d9e 100644 --- a/tests/test_errata_advisory_state_changed.py +++ b/tests/test_errata_advisory_state_changed.py @@ -366,12 +366,12 @@ class TestCheckImagesToRebuild(unittest.TestCase): self.ev = Event.create(db.session, 'msg-id', '123', 100) self.b1 = ArtifactBuild.create( db.session, self.ev, "parent", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, original_nvr="parent-1-25") self.b1.build_args = build_args self.b2 = ArtifactBuild.create( db.session, self.ev, "child", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, dep_on=self.b1, original_nvr="child-1-25") self.b2.build_args = build_args @@ -593,7 +593,7 @@ class TestPrepareYumRepo(unittest.TestCase): self.ev = Event.create(db.session, 'msg-id', '123', 100) ArtifactBuild.create( db.session, self.ev, "parent", "image", - state=ArtifactBuildState.PLANNED.value) + state=ArtifactBuildState.PLANNED) db.session.commit() def tearDown(self): diff --git a/tests/test_handler.py b/tests/test_handler.py index fed44d6..a8e1a5f 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -304,33 +304,33 @@ class TestBuildFirstBatch(TestCase): p1 = ArtifactBuild.create(db.session, self.db_event, "parent1-1-4", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, original_nvr="parent1-1-4") p1.build_args = build_args self.p1 = p1 b = ArtifactBuild.create(db.session, self.db_event, "parent1_child1", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, dep_on=p1, original_nvr="parent1_child1-1-4") b.build_args = build_args # Not in PLANNED state. b = ArtifactBuild.create(db.session, self.db_event, "parent3", "image", - state=ArtifactBuildState.BUILD.value, + state=ArtifactBuildState.BUILD, original_nvr="parent3-1-4") b.build_args = build_args # No build args b = ArtifactBuild.create(db.session, self.db_event, "parent4", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, original_nvr="parent4-1-4") db.session.commit() # No parent - base image b = ArtifactBuild.create(db.session, self.db_event, "parent5", "image", - state=ArtifactBuildState.PLANNED.value, + state=ArtifactBuildState.PLANNED, original_nvr="parent5-1-4") b.build_args = build_args b.build_args = b.build_args.replace("nvr", "")