diff --git a/quest_test.go b/quest_test.go index 08f8f62..4391e06 100644 --- a/quest_test.go +++ b/quest_test.go @@ -88,6 +88,26 @@ func TestSmokeQueryTwoStreams(t *testing.T) { DeleteStream(t, NewGlob.Client, stream2) } +func TestSmokeRunQueries(t *testing.T) { + RunFlog(t, NewGlob.Stream) + // test count + QueryLogStreamCount(t, NewGlob.Client, NewGlob.Stream, 50) + // test yeild all values + AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s", NewGlob.Stream) + AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s OFFSET 25 LIMIT 25", NewGlob.Stream) + // test fetch single column + for _, item := range flogStreamFields() { + AssertQueryOK(t, NewGlob.Client, "SELECT %s FROM %s", item, NewGlob.Stream) + } + // test basic filter + AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s WHERE method = 'POST'", NewGlob.Stream) + // test group by + AssertQueryOK(t, NewGlob.Client, "SELECT method, COUNT(*) FROM %s GROUP BY method", NewGlob.Stream) + AssertQueryOK(t, NewGlob.Client, `SELECT DATE_TRUNC('minute', p_timestamp) as minute, COUNT(*) FROM %s GROUP BY minute`, NewGlob.Stream) + + DeleteStream(t, NewGlob.Client, NewGlob.Stream) +} + func TestSmokeSetAlert(t *testing.T) { req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/alert", strings.NewReader(AlertBody)) response, err := NewGlob.Client.Do(req) diff --git a/test_utils.go b/test_utils.go index 49598eb..cee6fb2 100644 --- a/test_utils.go +++ b/test_utils.go @@ -33,6 +33,23 @@ const ( sleepDuration = 2 * time.Second ) +func flogStreamFields() []string { + return []string{ + "p_timestamp", + "p_tags", + "p_metadata", + "host", + "'user-identifier'", + "datetime", + "method", + "request", + "protocol", + "status", + "bytes", + "referer", + } +} + func readAsString(body io.Reader) string { r, _ := io.ReadAll(body) return string(r) @@ -123,6 +140,31 @@ func QueryTwoLogStreamCount(t *testing.T, client HTTPClient, stream1 string, str require.Equalf(t, expected, body, "Query count incorrect; Expected %s, Actual %s", expected, body) } +func AssertQueryOK(t *testing.T, client HTTPClient, query string, args ...any) { + // Query last 10 minutes of data only + endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano) + startTime := time.Now().Add(-10 * time.Minute).Format(time.RFC3339Nano) + + var finalQuery string + if len(args) == 0 { + finalQuery = query + } else { + finalQuery = fmt.Sprintf(query, args...) + } + + queryJSON, _ := json.Marshal(map[string]interface{}{ + "query": finalQuery, + "startTime": startTime, + "endTime": endTime, + }) + + req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON)) + response, err := client.Do(req) + require.NoErrorf(t, err, "Request failed: %s", err) + body := readAsString(response.Body) + require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body) +} + func AssertStreamSchema(t *testing.T, client HTTPClient, stream string, schema string) { req, _ := client.NewRequest("GET", "logstream/"+stream+"/schema", nil) response, err := client.Do(req)