From c7237cb09d6d0b10f36c86a2550af3f46cf8c0da Mon Sep 17 00:00:00 2001 From: Anton Schirg Date: Mon, 27 Aug 2018 16:24:19 +0200 Subject: [PATCH] test keep last n pruning --- cmd/pruning/keep_last_n_test.go | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 cmd/pruning/keep_last_n_test.go diff --git a/cmd/pruning/keep_last_n_test.go b/cmd/pruning/keep_last_n_test.go new file mode 100644 index 0000000..21e19f8 --- /dev/null +++ b/cmd/pruning/keep_last_n_test.go @@ -0,0 +1,77 @@ +package pruning + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestKeepLastN(t *testing.T) { + + o := func(minutes int) time.Time { + return time.Unix(123, 0).Add(time.Duration(minutes) * time.Minute) + } + + inputs := map[string][]Snapshot{ + "s1": []Snapshot{ + stubSnap{name: "1", date: o(10)}, + stubSnap{name: "2", date: o(20)}, + stubSnap{name: "3", date: o(15)}, + stubSnap{name: "4", date: o(30)}, + stubSnap{name: "5", date: o(30)}, + }, + "s2": []Snapshot{}, + } + + tcs := map[string]testCase{ + "keep2": { + inputs: inputs["s1"], + rules: []KeepRule{ + KeepLastN{2}, + }, + exp: map[string]bool{ + "4": true, "5": true, + }, + }, + "keep1": { // Keep one of two with same time + inputs: inputs["s1"], + rules: []KeepRule{ + KeepLastN{1}, + }, + exp: map[string]bool{ + "4": true, //5 would be ok too + }, + }, + "keepMany": { + inputs: inputs["s1"], + rules: []KeepRule{ + KeepLastN{100}, + }, + exp: map[string]bool{ + "1": true, + "2": true, + "3": true, + "4": true, + "5": true, + }, + }, + "empty": { + inputs: inputs["s2"], + rules: []KeepRule{ + KeepLastN{100}, + }, + exp: map[string]bool{}, + }, + } + + testTable(tcs, t) + + t.Run("mustBePositive", func(t *testing.T) { + var err error + _, err = NewKeepLastN(0) + assert.Error(t, err) + _, err = NewKeepLastN(-5) + assert.Error(t, err) + }) + +}