Add tests for latest map.

This commit is contained in:
Tom Wilkie
2016-01-24 10:06:03 -08:00
parent f071067289
commit f2df789503
2 changed files with 124 additions and 3 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"reflect"
"sort"
"time"
@@ -25,7 +24,11 @@ type LatestEntry struct {
}
func (e LatestEntry) String() string {
return fmt.Sprintf("\"%s\" (%d)", e.Value, e.Timestamp)
return fmt.Sprintf("\"%s\" (%d)", e.Value, e.Timestamp.String())
}
func (e LatestEntry) Equal(e2 LatestEntry) bool {
return e.Timestamp.Equal(e2.Timestamp) && e.Value == e2.Value
}
// EmptyLatestMap is an empty LatestMap. Start with this.
@@ -120,7 +123,7 @@ func (m LatestMap) DeepEqual(i interface{}) bool {
if otherValue, ok := n.Map.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
equal = equal && val.(LatestEntry).Equal(otherValue.(LatestEntry))
}
})
return equal

View File

@@ -0,0 +1,118 @@
package report
import (
"testing"
"time"
"github.com/weaveworks/scope/test"
"github.com/weaveworks/scope/test/reflect"
)
func TestLatestMapAdd(t *testing.T) {
now := time.Now()
want := EmptyLatestMap.
Set("foo", now, "Bar")
have := EmptyLatestMap.
Set("foo", now.Add(-1), "Baz").
Set("foo", now, "Bar")
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
if v, ok := have.Lookup("foo"); !ok || v != "Bar" {
t.Errorf("v != Bar")
}
if v, ok := have.Lookup("bar"); ok || v != "" {
t.Errorf("v != nil")
}
have.ForEach(func(k, v string) {
if k != "foo" || v != "Bar" {
t.Errorf("v != Bar")
}
})
}
func TestLatestMapDelete(t *testing.T) {
now := time.Now()
want := EmptyLatestMap
have := EmptyLatestMap.
Set("foo", now, "Baz").
Delete("foo")
if !reflect.DeepEqual(want, have) {
t.Errorf(test.Diff(want, have))
}
}
func TestLatestMapMerge(t *testing.T) {
now := time.Now()
then := now.Add(-1)
for name, c := range map[string]struct {
a, b, want LatestMap
}{
"Empty a": {
a: EmptyLatestMap,
b: EmptyLatestMap.
Set("foo", now, "bar"),
want: EmptyLatestMap.
Set("foo", now, "bar"),
},
"Empty b": {
a: EmptyLatestMap.
Set("foo", now, "bar"),
b: EmptyLatestMap,
want: EmptyLatestMap.
Set("foo", now, "bar"),
},
"Host merge": {
a: EmptyLatestMap.
Set("foo", now, "bar"),
b: EmptyLatestMap.
Set("baz", now, "bop"),
want: EmptyLatestMap.
Set("foo", now, "bar").
Set("baz", now, "bop"),
},
"Edge merge": {
a: EmptyLatestMap.
Set("foo", now, "bar"),
b: EmptyLatestMap.
Set("foo", then, "baz"),
want: EmptyLatestMap.
Set("foo", now, "bar"),
},
} {
if have := c.a.Merge(c.b); !reflect.DeepEqual(c.want, have) {
t.Errorf("%s:\n%s", name, test.Diff(c.want, have))
}
}
}
func TestLatestMapEncoding(t *testing.T) {
now := time.Now()
want := EmptyLatestMap.
Set("foo", now, "bar").
Set("bar", now, "baz")
{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyLatestMap
have.GobDecode(gobs)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
{
json, err := want.MarshalJSON()
if err != nil {
t.Fatal(err)
}
have := EmptyLatestMap
have.UnmarshalJSON(json)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}