Add SetStatus with test 10/161510/11
authorKatarzyna Gorska <k.gorska@samsung.com>
Wed, 29 Nov 2017 19:44:59 +0000 (20:44 +0100)
committerKatarzyna Gorska <k.gorska@samsung.com>
Fri, 15 Dec 2017 11:16:00 +0000 (12:16 +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 d32256c..fd80ce9 100644 (file)
@@ -190,3 +190,31 @@ func newArtifactInfoRecord(ai *ArtifactInfo) artifactInfoRecord {
 func artifactRecordToInfo(ar artifactInfoRecord) ArtifactInfo {
        return ar.ArtifactInfo
 }
+
+// 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 := newArtifactInfoRecord(&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 df8d7f3..9227b07 100644 (file)
@@ -120,6 +120,14 @@ var _ = Describe("ArtifactDB", func() {
                return n
        }
 
+       //      statusInDB := func(status weles.ArtifactStatus) int64 {
+       //              n, err := goldenUnicorn.dbmap.SelectInt(`SELECT COUNT(*)
+       //              FROM artifacts
+       //              WHERE Status = ?`, status)
+       //              Expect(err).ToNot(HaveOccurred())
+       //              return n
+       //      }
+
        BeforeEach(func() {
                var err error
                tmpDir, err = ioutil.TempDir("", "weles-")
@@ -224,14 +232,14 @@ var _ = Describe("ArtifactDB", func() {
                )
        })
 
-       Describe("list", func() {
+       Describe("List", func() {
                BeforeEach(func() {
                        for _, a := range testArtifacts {
                                err := goldenUnicorn.InsertArtifactInfo(&a)
                                Expect(err).ToNot(HaveOccurred())
                        }
                })
-               DescribeTable("ArtifactDB.List()",
+               DescribeTable("list artifacts matching filter",
                        func(filter weles.ArtifactFilter, expected []weles.ArtifactInfo) {
                                results, err := goldenUnicorn.Filter(filter)
                                Expect(err).ToNot(HaveOccurred())
@@ -255,4 +263,32 @@ var _ = Describe("ArtifactDB", func() {
                )
 
        })
+       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),
+               )
+       })
 })