Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
b54b43a4
Commit
b54b43a4
authored
Jun 22, 2017
by
Ben Tranter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for diff formatters
parent
bc6a57ce
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
0 deletions
+131
-0
pkg/components/dashdiffs/formatter_test.go
+131
-0
No files found.
pkg/components/dashdiffs/formatter_test.go
0 → 100644
View file @
b54b43a4
package
dashdiffs
import
(
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestDiff
(
t
*
testing
.
T
)
{
// Sample json docs for tests only
const
(
leftJSON
=
`{
"key": "value",
"object": {
"key": "value",
"anotherObject": {
"same": "this field is the same in rightJSON",
"change": "this field should change in rightJSON",
"delete": "this field doesn't appear in rightJSON"
}
},
"array": [
"same",
"change",
"delete"
],
"embeddedArray": {
"array": [
"same",
"change",
"delete"
]
}
}`
rightJSON
=
`{
"key": "differentValue",
"object": {
"key": "value",
"newKey": "value",
"anotherObject": {
"same": "this field is the same in rightJSON",
"change": "this field should change in rightJSON",
"add": "this field is added"
}
},
"array": [
"same",
"changed!",
"add"
],
"embeddedArray": {
"array": [
"same",
"changed!",
"add"
]
}
}`
)
Convey
(
"Testing dashboard diffs"
,
t
,
func
()
{
// Compute the diff between the two JSON objects
baseData
,
err
:=
simplejson
.
NewJson
([]
byte
(
leftJSON
))
So
(
err
,
ShouldBeNil
)
newData
,
err
:=
simplejson
.
NewJson
([]
byte
(
rightJSON
))
So
(
err
,
ShouldBeNil
)
left
,
jsonDiff
,
err
:=
getDiff
(
baseData
,
newData
)
So
(
err
,
ShouldBeNil
)
Convey
(
"The JSONFormatter should produce the expected JSON tokens"
,
func
()
{
f
:=
NewJSONFormatter
(
left
)
_
,
err
:=
f
.
Format
(
jsonDiff
)
So
(
err
,
ShouldBeNil
)
// Total up the change types. If the number of different change
// types is correct, it means that the diff is producing correct
// output to the template rendered.
changeCounts
:=
make
(
map
[
ChangeType
]
int
)
for
_
,
line
:=
range
f
.
Lines
{
changeCounts
[
line
.
Change
]
++
}
// The expectedChangeCounts here were determined by manually
// looking at the JSON
expectedChangeCounts
:=
map
[
ChangeType
]
int
{
ChangeNil
:
12
,
ChangeAdded
:
2
,
ChangeDeleted
:
1
,
ChangeOld
:
5
,
ChangeNew
:
5
,
ChangeUnchanged
:
5
,
}
So
(
changeCounts
,
ShouldResemble
,
expectedChangeCounts
)
})
Convey
(
"The BasicFormatter should produce the expected BasicBlocks"
,
func
()
{
f
:=
NewBasicFormatter
(
left
)
_
,
err
:=
f
.
Format
(
jsonDiff
)
So
(
err
,
ShouldBeNil
)
bd
:=
&
BasicDiff
{}
blocks
:=
bd
.
Basic
(
f
.
jsonDiff
.
Lines
)
changeCounts
:=
make
(
map
[
ChangeType
]
int
)
for
_
,
block
:=
range
blocks
{
for
_
,
change
:=
range
block
.
Changes
{
changeCounts
[
change
.
Change
]
++
}
for
_
,
summary
:=
range
block
.
Summaries
{
changeCounts
[
summary
.
Change
]
++
}
changeCounts
[
block
.
Change
]
++
}
expectedChangeCounts
:=
map
[
ChangeType
]
int
{
ChangeNil
:
3
,
ChangeAdded
:
2
,
ChangeDeleted
:
1
,
ChangeOld
:
3
,
}
So
(
changeCounts
,
ShouldResemble
,
expectedChangeCounts
)
})
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment