reverse version config
This commit is contained in:
1 parent
95aae06a41
commit
713db0aa4c
2 files changed
+46
-1
No files matched your search
@@ -62,11 +62,18 @@ func (s *VersionConfigStore) Delete(id string) {
|
||||
func (s VersionConfigStore) GetByConfigId(id string) []entity.VersionConfig {
|
||||
response, found := s.VersionByConfigId[id]
|
||||
if found {
|
||||
return response
|
||||
return s.reverse(response)
|
||||
}
|
||||
return []entity.VersionConfig{}
|
||||
}
|
||||
|
||||
func (s VersionConfigStore) reverse(cfg []entity.VersionConfig) []entity.VersionConfig {
|
||||
for i, j := 0, len(cfg)-1; i < j; i, j = i+1, j-1 {
|
||||
cfg[i], cfg[j] = cfg[j], cfg[i]
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func NewVersionConfigStore() *VersionConfigStore {
|
||||
return &VersionConfigStore{
|
||||
VersionByConfigId: make(map[string][]entity.VersionConfig),
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"isp-config-service/conf"
|
||||
"isp-config-service/entity"
|
||||
)
|
||||
|
||||
func TestVersionConfigStore(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
type example struct {
|
||||
cfg entity.VersionConfig
|
||||
removedId string
|
||||
}
|
||||
|
||||
config.UnsafeSet(&conf.Configuration{VersionConfigCount: 0})
|
||||
vcs := NewVersionConfigStore()
|
||||
for _, e := range []example{
|
||||
{cfg: entity.VersionConfig{Id: "1", ConfigId: "1"}, removedId: ""},
|
||||
{cfg: entity.VersionConfig{Id: "2", ConfigId: "1"}, removedId: ""},
|
||||
{cfg: entity.VersionConfig{Id: "3", ConfigId: "1"}, removedId: ""},
|
||||
{cfg: entity.VersionConfig{Id: "4", ConfigId: "1"}, removedId: ""},
|
||||
{cfg: entity.VersionConfig{Id: "5", ConfigId: "1"}, removedId: ""},
|
||||
{cfg: entity.VersionConfig{Id: "6", ConfigId: "1"}, removedId: "1"},
|
||||
{cfg: entity.VersionConfig{Id: "7", ConfigId: "1"}, removedId: "2"},
|
||||
} {
|
||||
removedId := vcs.Update(e.cfg)
|
||||
a.Equal(e.removedId, removedId)
|
||||
}
|
||||
for i, versionConfig := range vcs.GetByConfigId("1") {
|
||||
a.Equal(fmt.Sprintf("%d", 7-i), versionConfig.Id)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user