From a481a23c75f06139a42a5016b976489992895939 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 23 Nov 2024 09:02:28 +0000 Subject: [PATCH] test(view): add unit tests --- db/bolt/view_test.go | 143 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/db/bolt/view_test.go b/db/bolt/view_test.go index edb5dd67..180b9b88 100644 --- a/db/bolt/view_test.go +++ b/db/bolt/view_test.go @@ -1,10 +1,11 @@ package bolt import ( - "github.com/semaphoreui/semaphore/db" "sort" "testing" "time" + + "github.com/semaphoreui/semaphore/db" ) func TestGetViews(t *testing.T) { @@ -127,3 +128,143 @@ func TestSetViewPositions(t *testing.T) { t.Fatal() } } +func TestGetView(t *testing.T) { + store := CreateTestStore() + + proj1, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "Test1", + }) + + if err != nil { + t.Fatal(err.Error()) + } + + view, err := store.CreateView(db.View{ + ProjectID: proj1.ID, + Title: "Test", + Position: 1, + }) + + if err != nil { + t.Fatal(err.Error()) + } + + found, err := store.GetView(proj1.ID, view.ID) + + if err != nil { + t.Fatal(err.Error()) + } + + if found.ID != view.ID || found.Title != view.Title || found.Position != view.Position { + t.Fatal() + } +} + +func TestUpdateView(t *testing.T) { + store := CreateTestStore() + + proj1, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "Test1", + }) + + if err != nil { + t.Fatal(err.Error()) + } + + view, err := store.CreateView(db.View{ + ProjectID: proj1.ID, + Title: "Test", + Position: 1, + }) + + if err != nil { + t.Fatal(err.Error()) + } + + view.Title = "Updated Test" + err = store.UpdateView(view) + + if err != nil { + t.Fatal(err.Error()) + } + + updatedView, err := store.GetView(proj1.ID, view.ID) + + if err != nil { + t.Fatal(err.Error()) + } + + if updatedView.Title != "Updated Test" { + t.Fatal() + } +} + +func TestCreateView(t *testing.T) { + store := CreateTestStore() + + proj1, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "Test1", + }) + + if err != nil { + t.Fatal(err.Error()) + } + + view, err := store.CreateView(db.View{ + ProjectID: proj1.ID, + Title: "Test", + Position: 1, + }) + + if err != nil { + t.Fatal(err.Error()) + } + + found, err := store.GetView(proj1.ID, view.ID) + + if err != nil { + t.Fatal(err.Error()) + } + + if found.ID != view.ID || found.Title != view.Title || found.Position != view.Position { + t.Fatal() + } +} + +func TestDeleteView(t *testing.T) { + store := CreateTestStore() + + proj1, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "Test1", + }) + + if err != nil { + t.Fatal(err.Error()) + } + + view, err := store.CreateView(db.View{ + ProjectID: proj1.ID, + Title: "Test", + Position: 1, + }) + + if err != nil { + t.Fatal(err.Error()) + } + + err = store.DeleteView(proj1.ID, view.ID) + + if err != nil { + t.Fatal(err.Error()) + } + + _, err = store.GetView(proj1.ID, view.ID) + + if err == nil { + t.Fatal("Expected error, got nil") + } +}