Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tukaelu committed Oct 21, 2023
1 parent 60a8be6 commit e50560d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/subcommand/cmd_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package subcommand

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/mackerelio/mackerel-client-go"
"github.com/stretchr/testify/assert"
)

func TestListExternalMonitorMetricNames(t *testing.T) {
sv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resJson := `
{
"monitors": [
{
"id": "1XXXXXXXXXX",
"type": "host"
},
{
"id": "2XXXXXXXXXX",
"type": "external",
"service": "test",
"url": "https://example.com"
},
{
"id": "3XXXXXXXXXX",
"type": "external",
"service": "test",
"url": "https://example.com"
}
]
}
`
w.Write([]byte(resJson))

Check failure on line 36 in cmd/subcommand/cmd_service_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
}))
defer sv.Close()

client, _ := mackerel.NewClientWithOptions("dummy", sv.URL, false)

c := &serviceCommand{
baseCommand: baseCommand{
client: client,
},
name: "test",
}

metricNames, _ := c.listExternalMonitorMetricNames()

assert.Equal(t, 2, len(metricNames))
assert.Equal(t, "__externalhttp.responsetime.2XXXXXXXXXX", metricNames[0])
assert.Equal(t, "__externalhttp.responsetime.3XXXXXXXXXX", metricNames[1])
}
29 changes: 29 additions & 0 deletions internal/retriever/retriever_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package retriever

import (
"testing"

"github.com/mackerelio/mackerel-client-go"
"github.com/stretchr/testify/assert"
)

func TestRetrieve(t *testing.T) {
fn := func() ([]mackerel.MetricValue, error) {
var metrics []mackerel.MetricValue
metrics = append(metrics, mackerel.MetricValue{Time: 1672498800, Value: 1})
metrics = append(metrics, mackerel.MetricValue{Time: 1672498860, Value: 2})
metrics = append(metrics, mackerel.MetricValue{Time: 1672498920, Value: 3})
return metrics, nil
}
ret, _ := Retrieve(nil, "foo.bar", fn)

assert.Equal(t, "foo.bar", ret[0].Name)
assert.Equal(t, int64(1672498800), ret[0].Time)
assert.Equal(t, 1, ret[0].Value)
assert.Equal(t, "foo.bar", ret[1].Name)
assert.Equal(t, int64(1672498860), ret[1].Time)
assert.Equal(t, 2, ret[1].Value)
assert.Equal(t, "foo.bar", ret[2].Name)
assert.Equal(t, int64(1672498920), ret[2].Time)
assert.Equal(t, 3, ret[2].Value)
}

0 comments on commit e50560d

Please sign in to comment.