Semaphore/db/bolt/view_test.go

271 lines
4.2 KiB
Go
Raw Permalink Normal View History

package bolt
import (
2021-10-27 13:58:45 +02:00
"sort"
"testing"
"time"
2024-11-23 10:02:28 +01:00
"github.com/semaphoreui/semaphore/db"
)
func TestGetViews(t *testing.T) {
store := CreateTestStore()
proj1, err := store.CreateProject(db.Project{
Created: time.Now(),
2021-10-27 13:58:45 +02:00
Name: "Test1",
})
if err != nil {
t.Fatal(err.Error())
}
_, err = store.CreateView(db.View{
ProjectID: proj1.ID,
2021-10-27 13:58:45 +02:00
Title: "Test",
Position: 1,
})
if err != nil {
t.Fatal(err.Error())
}
found, err := store.GetViews(proj1.ID)
if err != nil {
t.Fatal(err.Error())
}
if len(found) != 1 {
t.Fatal()
}
view, err := store.GetView(proj1.ID, found[0].ID)
if err != nil {
t.Fatal(err.Error())
}
if view.ID != found[0].ID || view.Title != found[0].Title || view.Position != found[0].Position {
t.Fatal()
}
2021-10-27 13:58:45 +02:00
}
func TestSetViewPositions(t *testing.T) {
store := CreateTestStore()
2021-10-27 13:58:45 +02:00
proj1, err := store.CreateProject(db.Project{
Created: time.Now(),
Name: "Test1",
})
if err != nil {
t.Fatal(err.Error())
}
v1, err := store.CreateView(db.View{
ProjectID: proj1.ID,
Title: "Test",
Position: 4,
})
if err != nil {
t.Fatal(err.Error())
}
v2, err := store.CreateView(db.View{
ProjectID: proj1.ID,
Title: "Test",
Position: 2,
})
if err != nil {
t.Fatal(err.Error())
}
found, err := store.GetViews(proj1.ID)
if err != nil {
t.Fatal(err.Error())
}
if len(found) != 2 {
t.Fatal()
}
sort.Slice(found, func(i, j int) bool {
return found[i].Position < found[j].Position
})
if found[0].Position != v2.Position || found[1].Position != v1.Position {
t.Fatal()
}
err = store.SetViewPositions(proj1.ID, map[int]int{
2021-10-27 14:15:36 +02:00
v1.ID: 3,
v2.ID: 6,
2021-10-27 13:58:45 +02:00
})
if err != nil {
t.Fatal(err.Error())
}
found, err = store.GetViews(proj1.ID)
if err != nil {
t.Fatal(err.Error())
}
if len(found) != 2 {
t.Fatal()
}
sort.Slice(found, func(i, j int) bool {
return found[i].Position < found[j].Position
})
2021-10-27 14:15:36 +02:00
if found[0].Position != 3 || found[1].Position != 6 {
2021-10-27 13:58:45 +02:00
t.Fatal()
}
}
2024-11-23 10:02:28 +01:00
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")
}
}