Skip to content

Commit

Permalink
fix(be): set view positions for boltdb
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Oct 27, 2021
1 parent 1285f10 commit 339dfa5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
11 changes: 11 additions & 0 deletions db/bolt/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,16 @@ func (d *BoltDb) DeleteView(projectID int, viewID int) error {
}

func (d *BoltDb) SetViewPositions(projectID int, positions map[int]int) error {
for position, id := range positions {
view, err := d.GetView(projectID, id)
if err != nil {
return err
}
view.Position = position
err = d.UpdateView(view)
if err != nil {
return err
}
}
return nil
}
92 changes: 88 additions & 4 deletions db/bolt/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bolt

import (
"github.com/ansible-semaphore/semaphore/db"
"sort"
"testing"
"time"
)
Expand All @@ -16,7 +17,7 @@ func TestGetViews(t *testing.T) {

proj1, err := store.CreateProject(db.Project{
Created: time.Now(),
Name: "Test1",
Name: "Test1",
})

if err != nil {
Expand All @@ -25,8 +26,8 @@ func TestGetViews(t *testing.T) {

_, err = store.CreateView(db.View{
ProjectID: proj1.ID,
Title: "Test",
Position: 1,
Title: "Test",
Position: 1,
})

if err != nil {
Expand All @@ -52,4 +53,87 @@ func TestGetViews(t *testing.T) {
if view.ID != found[0].ID || view.Title != found[0].Title || view.Position != found[0].Position {
t.Fatal()
}
}
}

func TestSetViewPositions(t *testing.T) {
store := createStore()
err := store.Connect()

if err != nil {
t.Fatal(err.Error())
}

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{
v1.ID: 1,
v2.ID: 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 != 1 || found[1].Position != 2 {
t.Fatal()
}
}

0 comments on commit 339dfa5

Please sign in to comment.