Add SetStatus with test 10/161510/13
authorKatarzyna Gorska <k.gorska@samsung.com>
Wed, 29 Nov 2017 19:44:59 +0000 (20:44 +0100)
committerKatarzyna Gorska <k.gorska@samsung.com>
Wed, 10 Jan 2018 08:12:46 +0000 (09:12 +0100)
SetStatus changes artifact's status in ArtifactDB.

Change-Id: I3d00f1a4dce861220afee367433edb981286c40c
Signed-off-by: Katarzyna Gorska <k.gorska@samsung.com>
artifacts/database/database.go
artifacts/database/database_test.go

index b8ba759..7049c9c 100644 (file)
@@ -179,3 +179,33 @@ func (aDB *ArtifactDB) Select(arg interface{}) (artifacts []ArtifactInfo, err er
        }
        return artifacts, nil
 }
+
+// getID fetches ID of an artifact with provided path.
+func (aDB *ArtifactDB) getID(path ArtifactPath) (int64, error) {
+       res, err := aDB.dbmap.SelectInt("select ID from artifacts where Path=?", path)
+       if err != nil {
+               return 0, err
+       }
+       return res, nil
+}
+
+// SetStatus changes artifact's status in ArtifactDB.
+func (aDB *ArtifactDB) SetStatus(change ArtifactStatusChange) error {
+       ai, err := aDB.SelectPath(change.Path)
+       if err != nil {
+               return err
+       }
+       ar := artifactInfoRecord{
+               ArtifactInfo: ai,
+       }
+
+       id, err := aDB.getID(ar.Path)
+       if err != nil {
+               return err
+       }
+       ar.ID = id
+
+       ar.Status = change.NewStatus
+       _, err = aDB.dbmap.Update(&ar)
+       return err
+}
index ca9df77..5f4dff7 100644 (file)
@@ -254,4 +254,32 @@ var _ = Describe("ArtifactDB", func() {
                        Entry("filter is empty", emptyFilter, artifact, aImageReady, aYamlFailed, aTestFailed),
                )
        })
+       Describe("SetStatus", func() {
+               BeforeEach(func() {
+                       for _, a := range testArtifacts {
+                               err := goldenUnicorn.InsertArtifactInfo(&a)
+                               Expect(err).ToNot(HaveOccurred())
+                       }
+               })
+               DescribeTable("artifact status change",
+                       func(change weles.ArtifactStatusChange, expectedErr error) {
+
+                               err := goldenUnicorn.SetStatus(change)
+                               if expectedErr == nil {
+                                       Expect(err).ToNot(HaveOccurred())
+
+                                       a, err := goldenUnicorn.SelectPath(change.Path)
+                                       Expect(err).ToNot(HaveOccurred())
+                                       Expect(a.Status).To(Equal(change.NewStatus))
+                               } else {
+                                       Expect(err).To(Equal(expectedErr))
+                                       a, err := goldenUnicorn.SelectPath(change.Path)
+                                       Expect(err).To(HaveOccurred())
+                                       Expect(a).To(Equal(weles.ArtifactInfo{}))
+                               }
+                       },
+                       Entry("change status of artifact not present in ArtifactDB", weles.ArtifactStatusChange{invalidPath, weles.AM_DOWNLOADING}, sql.ErrNoRows),
+                       Entry("change status of artifact present in ArtifactDB", weles.ArtifactStatusChange{artifact.Path, weles.AM_DOWNLOADING}, nil),
+               )
+       })
 })