package query

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/yalp/jsonpath"
)

func TestResponse(t *testing.T) {
	body := "{\"took\":4, \"hits\":{\"total\":10, \"hits\":[{\"_source\":{\"key\":\"value\"}}]}}"

	res := NewResponse(ioutil.NopCloser(bytes.NewReader([]byte(body))))

	assert := assert.New(t)

	assert.Equal(int64(10), res.TotalHits())

	var j interface{}
	json.Unmarshal([]byte(res.Result()[0]), &j)

	if str, err := jsonpath.Read(j, "$.key"); err == nil {
		assert.Equal(str, "value")
	}
}
