test(view): add unit tests

This commit is contained in:
Denis Gukov 2024-11-23 09:02:28 +00:00
parent 50b2064fc0
commit a481a23c75

View File

@ -1,10 +1,11 @@
package bolt package bolt
import ( import (
"github.com/semaphoreui/semaphore/db"
"sort" "sort"
"testing" "testing"
"time" "time"
"github.com/semaphoreui/semaphore/db"
) )
func TestGetViews(t *testing.T) { func TestGetViews(t *testing.T) {
@ -127,3 +128,143 @@ func TestSetViewPositions(t *testing.T) {
t.Fatal() 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")
}
}