[#292] pruning: simplify table test driver

This commit is contained in:
Christian Schwarz 2020-08-29 17:26:31 +02:00
parent 639359f393
commit ad7a104ab4
2 changed files with 17 additions and 39 deletions

View File

@ -39,10 +39,7 @@ func TestKeepLastN(t *testing.T) {
rules: []KeepRule{ rules: []KeepRule{
KeepLastN{1}, KeepLastN{1},
}, },
expDestroyAlternatives: []map[string]bool{ expDestroy: map[string]bool{"1": true, "2": true, "3": true, "4": true},
{"1": true, "2": true, "3": true, "4": true},
{"1": true, "2": true, "3": true, "5": true},
},
}, },
"keepMany": { "keepMany": {
inputs: inputs["s1"], inputs: inputs["s1"],

View File

@ -3,6 +3,9 @@ package pruning
import ( import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type stubSnap struct { type stubSnap struct {
@ -20,8 +23,7 @@ func (s stubSnap) Date() time.Time { return s.date }
type testCase struct { type testCase struct {
inputs []Snapshot inputs []Snapshot
rules []KeepRule rules []KeepRule
expDestroy, effDestroy map[string]bool expDestroy map[string]bool
expDestroyAlternatives []map[string]bool
} }
type snapshotList []Snapshot type snapshotList []Snapshot
@ -44,41 +46,20 @@ func (l snapshotList) NameList() []string {
} }
func testTable(tcs map[string]testCase, t *testing.T) { func testTable(tcs map[string]testCase, t *testing.T) {
mapEqual := func(a, b map[string]bool) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if w, ok := b[k]; !ok || v != w {
return false
}
}
return true
}
for name := range tcs { for name := range tcs {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
tc := tcs[name] tc := tcs[name]
remove := PruneSnapshots(tc.inputs, tc.rules) destroyList := PruneSnapshots(tc.inputs, tc.rules)
tc.effDestroy = make(map[string]bool) destroySet := make(map[string]bool, len(destroyList))
for _, s := range remove { for _, s := range destroyList {
tc.effDestroy[s.Name()] = true destroySet[s.Name()] = true
} }
if tc.expDestroyAlternatives == nil { t.Logf("destroySet:\n%#v", destroySet)
if tc.expDestroy == nil { t.Logf("expected:\n%#v", tc.expDestroy)
panic("must specify either expDestroyAlternatives or expDestroy")
} require.Equal(t, len(tc.expDestroy), len(destroySet))
tc.expDestroyAlternatives = []map[string]bool{tc.expDestroy} for name := range destroySet {
} assert.True(t, tc.expDestroy[name], "%q", name)
var okAlt map[string]bool = nil
for _, alt := range tc.expDestroyAlternatives {
t.Logf("testing possible result: %v", alt)
if mapEqual(alt, tc.effDestroy) {
okAlt = alt
}
}
if okAlt == nil {
t.Errorf("no alternatives matched result: %v", tc.effDestroy)
} }
}) })
} }