chart_test.go 1.2 KB
package domain

import (
	"github.com/magiconair/properties/assert"
	"testing"
)

func TestReorder(t *testing.T) {
	//chartsAfter:
	//target := &Chart{Id: 3, Pid: 0, Sort: 3}
	inputs := []struct {
		charts     []*Chart
		targetId   int64
		sortIndex  int
		pid        int64
		appendFlag bool
		want       []int64
	}{
		{createChartsBefore(), 3, 1, 0, true, []int64{3, 1, 2}},
		{createChartsBefore(), 3, 2, 0, true, []int64{1, 3, 2}},
		{createChartsBefore(), 3, 3, 0, true, []int64{1, 2, 3}},
		{createChartsBefore(), 3, 4, 0, true, []int64{1, 2, 3}},
		{createChartsBefore(), 3, 1, 1, false, []int64{1, 2}},
	}
	for _, input := range inputs {
		assert.Equal(t, chartIds(Reorder(input.charts, &Chart{Id: input.targetId, Pid: input.pid, Sort: input.sortIndex}, input.appendFlag)), input.want)
	}
}
func chartIds(charts []*Chart) []int64 {
	var idList = make([]int64, 0)
	for _, c := range charts {
		if c.Id == 0 {
			continue
		}
		idList = append(idList, c.Id)
	}
	return idList
}

func createChartsBefore() []*Chart {
	return []*Chart{
		{Id: 1, Pid: 0, Sort: 1},
		{Id: 2, Pid: 0, Sort: 2},
		{Id: 3, Pid: 0, Sort: 3},
	}
}

func createChartsAfter() []*Chart {
	return []*Chart{
		{Id: 5, Pid: 1, Sort: 1},
		{Id: 6, Pid: 1, Sort: 2},
		{Id: 7, Pid: 1, Sort: 3},
	}
}