From d2dc3ef68bfc5ccd3eab38e64047fb66201d1764 Mon Sep 17 00:00:00 2001 From: Katarzyna Gorska Date: Wed, 29 Nov 2017 20:44:59 +0100 Subject: [PATCH] Add SetStatus with test SetStatus changes artifact's status in ArtifactDB. Change-Id: I3d00f1a4dce861220afee367433edb981286c40c Signed-off-by: Katarzyna Gorska --- artifacts/database/database.go | 28 ++++++++++++++++++++++++++ artifacts/database/database_test.go | 40 +++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/artifacts/database/database.go b/artifacts/database/database.go index d32256c..fd80ce9 100644 --- a/artifacts/database/database.go +++ b/artifacts/database/database.go @@ -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 +} diff --git a/artifacts/database/database_test.go b/artifacts/database/database_test.go index df8d7f3..9227b07 100644 --- a/artifacts/database/database_test.go +++ b/artifacts/database/database_test.go @@ -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), + ) + }) }) -- 2.7.4