formatter_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package dashdiffs
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestDiff(t *testing.T) {
  8. // Sample json docs for tests only
  9. const (
  10. leftJSON = `{
  11. "key": "value",
  12. "object": {
  13. "key": "value",
  14. "anotherObject": {
  15. "same": "this field is the same in rightJSON",
  16. "change": "this field should change in rightJSON",
  17. "delete": "this field doesn't appear in rightJSON"
  18. }
  19. },
  20. "array": [
  21. "same",
  22. "change",
  23. "delete"
  24. ],
  25. "embeddedArray": {
  26. "array": [
  27. "same",
  28. "change",
  29. "delete"
  30. ]
  31. }
  32. }`
  33. rightJSON = `{
  34. "key": "differentValue",
  35. "object": {
  36. "key": "value",
  37. "newKey": "value",
  38. "anotherObject": {
  39. "same": "this field is the same in rightJSON",
  40. "change": "this field should change in rightJSON",
  41. "add": "this field is added"
  42. }
  43. },
  44. "array": [
  45. "same",
  46. "changed!",
  47. "add"
  48. ],
  49. "embeddedArray": {
  50. "array": [
  51. "same",
  52. "changed!",
  53. "add"
  54. ]
  55. }
  56. }`
  57. )
  58. Convey("Testing dashboard diffs", t, func() {
  59. // Compute the diff between the two JSON objects
  60. baseData, err := simplejson.NewJson([]byte(leftJSON))
  61. So(err, ShouldBeNil)
  62. newData, err := simplejson.NewJson([]byte(rightJSON))
  63. So(err, ShouldBeNil)
  64. left, jsonDiff, err := getDiff(baseData, newData)
  65. So(err, ShouldBeNil)
  66. Convey("The JSONFormatter should produce the expected JSON tokens", func() {
  67. f := NewJSONFormatter(left)
  68. _, err := f.Format(jsonDiff)
  69. So(err, ShouldBeNil)
  70. // Total up the change types. If the number of different change
  71. // types is correct, it means that the diff is producing correct
  72. // output to the template rendered.
  73. changeCounts := make(map[ChangeType]int)
  74. for _, line := range f.Lines {
  75. changeCounts[line.Change]++
  76. }
  77. // The expectedChangeCounts here were determined by manually
  78. // looking at the JSON
  79. expectedChangeCounts := map[ChangeType]int{
  80. ChangeNil: 12,
  81. ChangeAdded: 2,
  82. ChangeDeleted: 1,
  83. ChangeOld: 5,
  84. ChangeNew: 5,
  85. ChangeUnchanged: 5,
  86. }
  87. So(changeCounts, ShouldResemble, expectedChangeCounts)
  88. })
  89. Convey("The BasicFormatter should produce the expected BasicBlocks", func() {
  90. f := NewBasicFormatter(left)
  91. _, err := f.Format(jsonDiff)
  92. So(err, ShouldBeNil)
  93. bd := &BasicDiff{}
  94. blocks := bd.Basic(f.jsonDiff.Lines)
  95. changeCounts := make(map[ChangeType]int)
  96. for _, block := range blocks {
  97. for _, change := range block.Changes {
  98. changeCounts[change.Change]++
  99. }
  100. for _, summary := range block.Summaries {
  101. changeCounts[summary.Change]++
  102. }
  103. changeCounts[block.Change]++
  104. }
  105. expectedChangeCounts := map[ChangeType]int{
  106. ChangeNil: 3,
  107. ChangeAdded: 2,
  108. ChangeDeleted: 1,
  109. ChangeOld: 3,
  110. }
  111. So(changeCounts, ShouldResemble, expectedChangeCounts)
  112. })
  113. })
  114. }