table_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package report_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/weaveworks/common/test"
  6. "github.com/weaveworks/scope/report"
  7. )
  8. func TestMulticolumnTables(t *testing.T) {
  9. want := []report.Row{
  10. {
  11. ID: "row1",
  12. Entries: map[string]string{
  13. "col1": "r1c1",
  14. "col2": "r1c2",
  15. "col3": "r1c3",
  16. },
  17. },
  18. {
  19. ID: "row2",
  20. Entries: map[string]string{
  21. "col1": "r2c1",
  22. "col3": "r2c3",
  23. },
  24. },
  25. }
  26. nmd := report.MakeNode("foo1")
  27. nmd = nmd.AddPrefixMulticolumnTable("foo_", want)
  28. template := report.TableTemplate{
  29. Type: report.MulticolumnTableType,
  30. Prefix: "foo_",
  31. }
  32. have, truncationCount := nmd.ExtractTable(template)
  33. if truncationCount != 0 {
  34. t.Error("Table shouldn't had been truncated")
  35. }
  36. if !reflect.DeepEqual(want, have) {
  37. t.Error(test.Diff(want, have))
  38. }
  39. }
  40. func TestPrefixPropertyLists(t *testing.T) {
  41. want := []report.Row{
  42. {
  43. ID: "label_foo1",
  44. Entries: map[string]string{
  45. "label": "foo1",
  46. "value": "bar1",
  47. },
  48. },
  49. {
  50. ID: "label_foo3",
  51. Entries: map[string]string{
  52. "label": "foo3",
  53. "value": "bar3",
  54. },
  55. },
  56. }
  57. nmd := report.MakeNode("foo1")
  58. nmd = nmd.AddPrefixPropertyList("foo_", map[string]string{
  59. "foo3": "bar3",
  60. "foo1": "bar1",
  61. })
  62. nmd = nmd.AddPrefixPropertyList("zzz_", map[string]string{
  63. "foo2": "bar2",
  64. })
  65. template := report.TableTemplate{
  66. Type: report.PropertyListType,
  67. Prefix: "foo_",
  68. }
  69. have, truncationCount := nmd.ExtractTable(template)
  70. if truncationCount != 0 {
  71. t.Error("Table shouldn't had been truncated")
  72. }
  73. if !reflect.DeepEqual(want, have) {
  74. t.Error(test.Diff(want, have))
  75. }
  76. }
  77. func TestFixedPropertyLists(t *testing.T) {
  78. want := []report.Row{
  79. {
  80. ID: "label_foo1",
  81. Entries: map[string]string{
  82. "label": "foo1",
  83. "value": "bar1",
  84. },
  85. },
  86. {
  87. ID: "label_foo2",
  88. Entries: map[string]string{
  89. "label": "foo2",
  90. "value": "bar2",
  91. },
  92. },
  93. }
  94. nmd := report.MakeNodeWith("foo1", map[string]string{
  95. "foo2key": "bar2",
  96. "foo1key": "bar1",
  97. })
  98. template := report.TableTemplate{
  99. Type: report.PropertyListType,
  100. FixedRows: map[string]string{
  101. "foo2key": "foo2",
  102. "foo1key": "foo1",
  103. },
  104. }
  105. have, truncationCount := nmd.ExtractTable(template)
  106. if truncationCount != 0 {
  107. t.Error("Table shouldn't had been truncated")
  108. }
  109. if !reflect.DeepEqual(want, have) {
  110. t.Error(test.Diff(want, have))
  111. }
  112. }
  113. func TestTables(t *testing.T) {
  114. want := []report.Table{
  115. {
  116. ID: "AAA",
  117. Label: "Aaa",
  118. Type: report.PropertyListType,
  119. Columns: nil,
  120. Rows: []report.Row{
  121. {
  122. ID: "label_foo1",
  123. Entries: map[string]string{
  124. "label": "foo1",
  125. "value": "bar1",
  126. },
  127. },
  128. {
  129. ID: "label_foo3",
  130. Entries: map[string]string{
  131. "label": "foo3",
  132. "value": "bar3",
  133. },
  134. },
  135. },
  136. },
  137. {
  138. ID: "BBB",
  139. Label: "Bbb",
  140. Type: report.MulticolumnTableType,
  141. Columns: []report.Column{{ID: "col1", Label: "Column 1"}},
  142. Rows: []report.Row{
  143. {
  144. ID: "row1",
  145. Entries: map[string]string{
  146. "col1": "r1c1",
  147. },
  148. },
  149. {
  150. ID: "row2",
  151. Entries: map[string]string{
  152. "col3": "r2c3",
  153. },
  154. },
  155. },
  156. },
  157. {
  158. ID: "CCC",
  159. Label: "Ccc",
  160. Type: report.PropertyListType,
  161. Columns: nil,
  162. Rows: []report.Row{
  163. {
  164. ID: "label_foo3",
  165. Entries: map[string]string{
  166. "label": "foo3",
  167. "value": "bar3",
  168. },
  169. },
  170. },
  171. },
  172. }
  173. nmd := report.MakeNodeWith("foo1", map[string]string{
  174. "foo3key": "bar3",
  175. "foo1key": "bar1",
  176. })
  177. nmd = nmd.AddPrefixMulticolumnTable("bbb_", []report.Row{
  178. {ID: "row1", Entries: map[string]string{"col1": "r1c1"}},
  179. {ID: "row2", Entries: map[string]string{"col3": "r2c3"}},
  180. })
  181. nmd = nmd.AddPrefixPropertyList("aaa_", map[string]string{
  182. "foo3": "bar3",
  183. "foo1": "bar1",
  184. })
  185. aaaTemplate := report.TableTemplate{
  186. ID: "AAA",
  187. Label: "Aaa",
  188. Prefix: "aaa_",
  189. Type: report.PropertyListType,
  190. }
  191. bbbTemplate := report.TableTemplate{
  192. ID: "BBB",
  193. Label: "Bbb",
  194. Prefix: "bbb_",
  195. Type: report.MulticolumnTableType,
  196. Columns: []report.Column{{ID: "col1", Label: "Column 1"}},
  197. }
  198. cccTemplate := report.TableTemplate{
  199. ID: "CCC",
  200. Label: "Ccc",
  201. Prefix: "ccc_",
  202. Type: report.PropertyListType,
  203. FixedRows: map[string]string{"foo3key": "foo3"},
  204. }
  205. templates := report.TableTemplates{
  206. aaaTemplate.ID: aaaTemplate,
  207. bbbTemplate.ID: bbbTemplate,
  208. cccTemplate.ID: cccTemplate,
  209. }
  210. have := templates.Tables(nmd)
  211. if !reflect.DeepEqual(want, have) {
  212. t.Error(test.Diff(want, have))
  213. }
  214. }