Skip to content

Commit

Permalink
Add test for the raw handleSummary() data parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Jan 12, 2021
1 parent 616fced commit 0838813
Showing 1 changed file with 155 additions and 6 deletions.
161 changes: 155 additions & 6 deletions js/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
)

func TestTextSummary(t *testing.T) {
t.Parallel()
var (
checksOut = " █ child\n\n" +
" ✓ check1\n" +
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestTextSummary(t *testing.T) {
t, "/script.js",
fmt.Sprintf(`
exports.options = {summaryTrendStats: %s};
exports.default = function() {};
exports.default = function() {/* we don't run this, metrics are mocked */};
`, string(trendStats)),
lib.RuntimeOptions{
CompatibilityMode: null.NewString("base", true),
Expand Down Expand Up @@ -162,9 +163,7 @@ func createTestMetrics(t *testing.T) (map[string]*stats.Metric, *lib.Group) {
return metrics, rootG
}

func TestSummarizeMetricsJSON(t *testing.T) {
metrics, rootG := createTestMetrics(t)
expected := `{
const expectedOldJSONExportResult = `{
"root_group": {
"name": "",
"path": "",
Expand Down Expand Up @@ -237,11 +236,13 @@ func TestSummarizeMetricsJSON(t *testing.T) {
}
`

func TestOldJSONExport(t *testing.T) {
t.Parallel()
runner, err := getSimpleRunner(
t, "/script.js",
`
exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]};
exports.default = function() {};
exports.default = function() {/* we don't run this, metrics are mocked */};
`,
lib.RuntimeOptions{
CompatibilityMode: null.NewString("base", true),
Expand All @@ -251,6 +252,7 @@ func TestSummarizeMetricsJSON(t *testing.T) {

require.NoError(t, err)

metrics, rootG := createTestMetrics(t)
result, err := runner.HandleSummary(context.Background(), &lib.Summary{
Metrics: metrics,
RootGroup: rootG,
Expand All @@ -263,5 +265,152 @@ func TestSummarizeMetricsJSON(t *testing.T) {
require.NotNil(t, result["result.json"])
jsonExport, err := ioutil.ReadAll(result["result.json"])
require.NoError(t, err)
assert.JSONEq(t, expected, string(jsonExport))
assert.JSONEq(t, expectedOldJSONExportResult, string(jsonExport))
}

const expectedHandleSummaryRawData = `
{
"root_group": {
"groups": [
{
"name": "child",
"path": "::child",
"id": "f41cbb53a398ec1c9fb3d33e20c9b040",
"groups": [],
"checks": [
{
"id": "6289a7a06253a1c3f6137dfb25695563",
"passes": 30,
"fails": 0,
"name": "check1",
"path": "::child::check1"
},
{
"fails": 5,
"name": "check3",
"path": "::child::check3",
"id": "c7553eca92d3e034b5808332296d304a",
"passes": 10
},
{
"name": "check2",
"path": "::child::check2",
"id": "06f5922794bef0d4584ba76a49893e1f",
"passes": 5,
"fails": 10
}
]
}
],
"checks": [],
"name": "",
"path": "",
"id": "d41d8cd98f00b204e9800998ecf8427e"
},
"options": {
"summaryTrendStats": [
"avg",
"min",
"med",
"max",
"p(90)",
"p(95)",
"p(99)",
"count"
],
"summaryTimeUnit": ""
},
"metrics": {
"checks": {
"contains": "default",
"values": {
"passes": 45,
"fails": 15,
"rate": 0.75
},
"type": "rate"
},
"my_trend": {
"thresholds": {
"my_trend<1000": {
"ok": false
}
},
"type": "trend",
"contains": "time",
"values": {
"max": 20,
"p(90)": 19,
"p(95)": 19.5,
"p(99)": 19.9,
"count": 3,
"avg": 15,
"min": 10,
"med": 15
}
},
"vus": {
"contains": "default",
"values": {
"value": 1,
"min": 1,
"max": 1
},
"type": "gauge"
},
"http_reqs": {
"type": "counter",
"contains": "default",
"values": {
"count": 3,
"rate": 3
},
"thresholds": {
"rate<100": {
"ok": true
}
}
}
}
}`

func TestRawHandleSummaryData(t *testing.T) {
t.Parallel()
runner, err := getSimpleRunner(
t, "/script.js",
`
exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]};
exports.default = function() { /* we don't run this, metrics are mocked */ };
exports.handleSummary = function(data) {
return {'rawdata.json': JSON.stringify(data)};
};
`,
lib.RuntimeOptions{
CompatibilityMode: null.NewString("base", true),
// we still want to check this
SummaryExport: null.StringFrom("old-export.json"),
},
)

require.NoError(t, err)

metrics, rootG := createTestMetrics(t)
result, err := runner.HandleSummary(context.Background(), &lib.Summary{
Metrics: metrics,
RootGroup: rootG,
TestRunDuration: time.Second,
})
require.NoError(t, err)

require.Len(t, result, 2)
require.Nil(t, result["stdout"])

require.NotNil(t, result["old-export.json"])
oldExport, err := ioutil.ReadAll(result["old-export.json"])
require.NoError(t, err)
assert.JSONEq(t, expectedOldJSONExportResult, string(oldExport))
require.NotNil(t, result["rawdata.json"])
newRawData, err := ioutil.ReadAll(result["rawdata.json"])
require.NoError(t, err)
assert.JSONEq(t, expectedHandleSummaryRawData, string(newRawData))
}

0 comments on commit 0838813

Please sign in to comment.