change pruning testing function to use set compare on names

This commit is contained in:
Anton Schirg 2018-08-27 16:17:39 +02:00
parent c2b04d10c5
commit 4073c5dfb0

View File

@ -1,6 +1,7 @@
package pruning package pruning
import ( import (
"fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
"time" "time"
@ -18,13 +19,39 @@ func (s stubSnap) Replicated() bool { return s.replicated }
func (s stubSnap) Date() time.Time { return s.date } func (s stubSnap) Date() time.Time { return s.date }
func TestPruneSnapshots(t *testing.T) { type testCase struct {
type testCase struct {
inputs []Snapshot inputs []Snapshot
rules []KeepRule rules []KeepRule
exp, eff []Snapshot exp, eff map[string]bool
}
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 {
t.Run(name, func(t *testing.T) {
tc := tcs[name]
remove := PruneSnapshots(tc.inputs, tc.rules)
tc.eff = make(map[string]bool)
for _, s := range remove {
tc.eff[s.Name()] = true
}
assert.True(t, mapEqual(tc.exp, tc.eff), fmt.Sprintf("is %v but should be %v", tc.eff, tc.exp))
})
}
}
func TestPruneSnapshots(t *testing.T) {
inputs := map[string][]Snapshot{ inputs := map[string][]Snapshot{
"s1": []Snapshot{ "s1": []Snapshot{
@ -40,8 +67,8 @@ func TestPruneSnapshots(t *testing.T) {
rules: []KeepRule{ rules: []KeepRule{
MustKeepRegex("foo_"), MustKeepRegex("foo_"),
}, },
exp: []Snapshot{ exp: map[string]bool{
stubSnap{name: "bar_123"}, "bar_123": true,
}, },
}, },
"multipleRules": { "multipleRules": {
@ -50,7 +77,7 @@ func TestPruneSnapshots(t *testing.T) {
MustKeepRegex("foo_"), MustKeepRegex("foo_"),
MustKeepRegex("bar_"), MustKeepRegex("bar_"),
}, },
exp: []Snapshot{}, exp: map[string]bool{},
}, },
"onlyThoseRemovedByAllAreRemoved": { "onlyThoseRemovedByAllAreRemoved": {
inputs: inputs["s1"], inputs: inputs["s1"],
@ -58,31 +85,28 @@ func TestPruneSnapshots(t *testing.T) {
MustKeepRegex("notInS1"), // would remove all MustKeepRegex("notInS1"), // would remove all
MustKeepRegex("bar_"), // would remove all but bar_, i.e. foo_.* MustKeepRegex("bar_"), // would remove all but bar_, i.e. foo_.*
}, },
exp: []Snapshot{ exp: map[string]bool{
stubSnap{name: "foo_123"}, "foo_123": true,
stubSnap{name: "foo_456"}, "foo_456": true,
}, },
}, },
"noRulesKeepsAll": { "noRulesKeepsAll": {
inputs: inputs["s1"], inputs: inputs["s1"],
rules: []KeepRule{}, rules: []KeepRule{},
exp: inputs["s1"], exp: map[string]bool{
"foo_123": true,
"foo_456": true,
"bar_123": true,
},
}, },
"noSnaps": { "noSnaps": {
inputs: []Snapshot{}, inputs: []Snapshot{},
rules: []KeepRule{ rules: []KeepRule{
MustKeepRegex("foo_"), MustKeepRegex("foo_"),
}, },
exp: []Snapshot{}, exp: map[string]bool{},
}, },
} }
for name := range tcs { testTable(tcs, t)
t.Run(name, func(t *testing.T) {
tc := tcs[name]
tc.eff = PruneSnapshots(tc.inputs, tc.rules)
assert.Equal(t, tc.exp, tc.eff)
})
}
} }