From 3558207025a2e5601d6f81bd1ec1855efa0e2d60 Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Apr 27 2018 10:51:51 +0000
Subject: Fake compose ids and koji task ids are negative.


---

diff --git a/freshmaker/handlers/errata/errata_advisory_rpms_signed.py b/freshmaker/handlers/errata/errata_advisory_rpms_signed.py
index 0693046..68f8925 100644
--- a/freshmaker/handlers/errata/errata_advisory_rpms_signed.py
+++ b/freshmaker/handlers/errata/errata_advisory_rpms_signed.py
@@ -58,7 +58,7 @@ class ErrataAdvisoryRPMsSignedHandler(ContainerBuildHandler):
     name = 'ErrataAdvisoryRPMsSignedHandler'
 
     # Used to generate incremental compose id in dry run mode.
-    _FAKE_COMPOSE_ID = 1
+    _FAKE_COMPOSE_ID = 0
 
     def can_handle(self, event):
         return isinstance(event, ErrataAdvisoryRPMsSignedEvent)
@@ -78,7 +78,9 @@ class ErrataAdvisoryRPMsSignedHandler(ContainerBuildHandler):
         # increasing and unique even between Freshmaker restarts.
         if self.dry_run:
             ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID = \
-                Compose.get_highest_compose_id(db.session) + 1
+                Compose.get_lowest_compose_id(db.session) - 1
+            if ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID >= 0:
+                ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID = -1
 
         self.event = event
 
diff --git a/freshmaker/kojiservice.py b/freshmaker/kojiservice.py
index c0b2a8d..e6f2cc7 100644
--- a/freshmaker/kojiservice.py
+++ b/freshmaker/kojiservice.py
@@ -51,7 +51,7 @@ class KojiService(object):
     """
 
     # Used to generate incremental task id in dry run mode.
-    _FAKE_TASK_ID = 1
+    _FAKE_TASK_ID = 0
 
     def __init__(self, profile=None, dry_run=False):
         self._config = koji.read_config(profile or 'koji')
@@ -62,7 +62,9 @@ class KojiService(object):
         # increasing and unique even between Freshmaker restarts.
         if self.dry_run:
             KojiService._FAKE_TASK_ID = \
-                ArtifactBuild.get_highest_build_id(db.session) + 1
+                ArtifactBuild.get_lowest_build_id(db.session) - 1
+            if KojiService._FAKE_TASK_ID >= 0:
+                KojiService._FAKE_TASK_ID = -1
 
     @property
     def config(self):
diff --git a/freshmaker/models.py b/freshmaker/models.py
index b9544ff..5e64e19 100644
--- a/freshmaker/models.py
+++ b/freshmaker/models.py
@@ -454,14 +454,14 @@ class ArtifactBuild(FreshmakerBase):
         raise ValueError("%s: %s, not in %r" % (key, field, list(ArtifactType)))
 
     @classmethod
-    def get_highest_build_id(cls, session):
+    def get_lowest_build_id(cls, session):
         """
-        Returns the highest build_id. If there is no build so far,
+        Returns the lowest build_id. If there is no build so far,
         returns 0.
         """
         build = (session.query(ArtifactBuild)
                  .filter(cls.build_id != None)  # noqa
-                 .order_by(ArtifactBuild.build_id.desc())
+                 .order_by(ArtifactBuild.build_id.asc())
                  .first())
         if not build:
             return 0
@@ -579,13 +579,13 @@ class Compose(FreshmakerBase):
                 self.odcs_compose_id)['state_name']
 
     @classmethod
-    def get_highest_compose_id(cls, session):
+    def get_lowest_compose_id(cls, session):
         """
-        Returns the highest odcs_compose_id. If there is no compose,
+        Returns the lowest odcs_compose_id. If there is no compose,
         returns 0.
         """
         compose = session.query(Compose).order_by(
-            Compose.odcs_compose_id.desc()).first()
+            Compose.odcs_compose_id.asc()).first()
         if not compose:
             return 0
         return compose.odcs_compose_id
diff --git a/tests/test_errata_advisory_rpms_signed_handler.py b/tests/test_errata_advisory_rpms_signed_handler.py
index 90d24c1..d6d7790 100644
--- a/tests/test_errata_advisory_rpms_signed_handler.py
+++ b/tests/test_errata_advisory_rpms_signed_handler.py
@@ -273,7 +273,7 @@ class TestErrataAdvisoryRPMsSignedHandler(helpers.ModelsTestCase):
         handler = ErrataAdvisoryRPMsSignedHandler()
         handler.handle(self.rhba_event)
 
-        self.assertEqual(ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID, 5)
+        self.assertEqual(ErrataAdvisoryRPMsSignedHandler._FAKE_COMPOSE_ID, -1)
 
     @patch.object(freshmaker.conf, 'handler_build_whitelist', new={
         'ErrataAdvisoryRPMsSignedHandler': {
diff --git a/tests/test_models.py b/tests/test_models.py
index cc0b186..799721d 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -254,7 +254,7 @@ class TestArtifactBuildComposesRel(helpers.ModelsTestCase):
     def setUp(self):
         super(TestArtifactBuildComposesRel, self). setUp()
 
-        self.compose_1 = Compose(odcs_compose_id=1)
+        self.compose_1 = Compose(odcs_compose_id=-1)
         self.compose_2 = Compose(odcs_compose_id=2)
         self.compose_3 = Compose(odcs_compose_id=3)
         self.compose_4 = Compose(odcs_compose_id=4)
@@ -273,7 +273,7 @@ class TestArtifactBuildComposesRel(helpers.ModelsTestCase):
         self.build_1.build_id = 3
         self.build_2 = ArtifactBuild.create(
             db.session, self.event, 'build-2', ArtifactType.IMAGE)
-        self.build_2.build_id = 2
+        self.build_2.build_id = -2
         self.build_3 = ArtifactBuild.create(
             db.session, self.event, 'build-3', ArtifactType.IMAGE)
         self.build_3.build_id = None
@@ -295,13 +295,13 @@ class TestArtifactBuildComposesRel(helpers.ModelsTestCase):
 
         db.session.commit()
 
-    def test_get_highest_compose_id(self):
-        compose_id = Compose.get_highest_compose_id(db.session)
-        self.assertEqual(compose_id, 4)
+    def test_get_lowest_compose_id(self):
+        compose_id = Compose.get_lowest_compose_id(db.session)
+        self.assertEqual(compose_id, -1)
 
-    def test_get_highest_build_id(self):
-        build_id = ArtifactBuild.get_highest_build_id(db.session)
-        self.assertEqual(build_id, 3)
+    def test_get_lowest_build_id(self):
+        build_id = ArtifactBuild.get_lowest_build_id(db.session)
+        self.assertEqual(build_id, -2)
 
     def test_build_composes(self):
         self.assertEqual(3, len(self.build_1.composes))

