From d0e022050e7ffd500e1ec2621d9cb8cd742d8baa Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 20 Oct 2016 11:58:12 +0000 Subject: [PATCH] Fix and extend tests --- render/detailed/tables_test.go | 5 +++++ report/table.go | 6 +++--- report/table_test.go | 33 ++++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/render/detailed/tables_test.go b/render/detailed/tables_test.go index ebdf3b2ee..17c4c6b3d 100644 --- a/render/detailed/tables_test.go +++ b/render/detailed/tables_test.go @@ -48,6 +48,11 @@ func TestNodeTables(t *testing.T) { }, }, }, + { + ID: docker.ImageTableID, + Label: "Image", + Rows: []report.MetadataRow{}, + }, }, }, { diff --git a/report/table.go b/report/table.go index 4d84467bd..c1e014d8e 100644 --- a/report/table.go +++ b/report/table.go @@ -34,8 +34,8 @@ func (node Node) AddPrefixTable(prefix string, labels map[string]string) Node { return node } -// extractTable returns the key-value pairs to build a table from this node -func (node Node) extractTable(template TableTemplate) (rows map[string]string, truncationCount int) { +// ExtractTable returns the key-value pairs to build a table from this node +func (node Node) ExtractTable(template TableTemplate) (rows map[string]string, truncationCount int) { rows = map[string]string{} truncationCount = 0 node.Latest.ForEach(func(key string, _ time.Time, value string) { @@ -141,7 +141,7 @@ type TableTemplates map[string]TableTemplate func (t TableTemplates) Tables(node Node) []Table { var result []Table for _, template := range t { - rows, truncationCount := node.extractTable(template) + rows, truncationCount := node.ExtractTable(template) table := Table{ ID: template.ID, Label: template.Label, diff --git a/report/table_test.go b/report/table_test.go index cf276ba75..e7dedf000 100644 --- a/report/table_test.go +++ b/report/table_test.go @@ -9,15 +9,15 @@ import ( "github.com/weaveworks/scope/test" ) -func TestTables(t *testing.T) { +func TestPrefixTables(t *testing.T) { want := map[string]string{ "foo1": "bar1", "foo2": "bar2", } nmd := report.MakeNode("foo1") - nmd = nmd.AddTable("foo_", want) - have, truncationCount := nmd.ExtractTable("foo_") + nmd = nmd.AddPrefixTable("foo_", want) + have, truncationCount := nmd.ExtractTable(report.TableTemplate{Prefix: "foo_"}) if truncationCount != 0 { t.Error("Table shouldn't had been truncated") @@ -28,6 +28,29 @@ func TestTables(t *testing.T) { } } +func TestFixedTables(t *testing.T) { + want := map[string]string{ + "foo1": "bar1", + "foo2": "bar2", + } + nmd := report.MakeNodeWith("foo1", map[string]string{ + "foo1key": "bar1", + "foo2key": "bar2", + }) + + template := report.TableTemplate{FixedRows: map[string]string{ + "foo1key": "foo1", + "foo2key": "foo2", + }, + } + + have, _ := nmd.ExtractTable(template) + + if !reflect.DeepEqual(want, have) { + t.Error(test.Diff(want, have)) + } +} + func TestTruncation(t *testing.T) { wantTruncationCount := 1 want := map[string]string{} @@ -39,8 +62,8 @@ func TestTruncation(t *testing.T) { nmd := report.MakeNode("foo1") - nmd = nmd.AddTable("foo_", want) - _, truncationCount := nmd.ExtractTable("foo_") + nmd = nmd.AddPrefixTable("foo_", want) + _, truncationCount := nmd.ExtractTable(report.TableTemplate{Prefix: "foo_"}) if truncationCount != wantTruncationCount { t.Error(