featurebase/idk/idktest/idktest.go
CLoZengineer f9ddb5d5c1
fix: updating code to meet linting requirements (#2171)
* removes unused filesize function

* removes ioutil usage

* updates ioutil.ReadAll to io.ReadAll

* updates ioutil.TempFile to os.CreateTemp

* updates ioutil.TempDir to os.MkdirTemp

* updates ioutil.ReadAll to os.ReadAll

* update ioutil.WriteFile to os.WriteFile

* updates ioutil.Discard to io.Discard

* updates ioutil.ReadDir to os.ReadDir where applicable

* removes unused code in idk

* creates type to use for context value keys

* replaces assert.Nil with assert.NoError for error checks
2022-09-29 12:34:29 -04:00

76 lines
1.6 KiB
Go

package idktest
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/pkg/errors"
)
const (
ErrDeletingIndex = "deleting index"
ErrRunningIngest = "running ingester"
ErrGettingSchema = "getting schema"
ErrGettingQuery = "querying"
)
type ExtractResponse struct {
Results []Result
}
type Result struct {
Fields []erField
Columns []Column
}
type erField struct {
Name string
Type string
}
type Column struct {
ColumnID int `json:"column"`
Rows []interface{} `json:"rows"`
}
// doExtractQuery sends a query of result-type Extract to pilosa and
// unpacks the response.
// This was created as a helper function for TestLookupFieldWithExternalId.
func DoExtractQuery(pql, index string) (ExtractResponse, error) {
pilosaHost, ok := os.LookupEnv("IDK_TEST_PILOSA_HOST")
if !ok {
pilosaHost = "pilosa:10101"
}
var eResp ExtractResponse
body := strings.NewReader(pql)
url := fmt.Sprintf("http://%s/index/%s/query", pilosaHost, index)
req, err := http.NewRequest("POST", url, body)
if err != nil {
return eResp, errors.Errorf("creating request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return eResp, errors.Errorf("sending request: %v", err)
}
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
if err != nil {
return eResp, errors.Errorf("reading response: %v", err)
}
err = json.Unmarshal(s, &eResp)
if err != nil {
return eResp, errors.Errorf("unmarshaling response: %v", err)
}
return eResp, nil
}