Skip to content

Commit

Permalink
feat: Add testing for writing out query to log file
Browse files Browse the repository at this point in the history
  • Loading branch information
devanbenz committed Dec 30, 2024
1 parent f0c2c28 commit b9e9eb7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions query/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package query_test
import (
"errors"
"fmt"
"github.com/stretchr/testify/require"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -595,6 +597,34 @@ func TestQueryExecutor_InvalidSource(t *testing.T) {
}
}

func TestQueryExecutor_WriteQueryToLog(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
require.NoError(t, err, "parse query")

f, err := os.CreateTemp("", "query-test.log")
require.NoError(t, err, "create temp file")

defer os.Remove(f.Name())

e := NewQueryExecutor()
e.WithLogWriter(e.Logger, f.Name())

e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
require.Equal(t, uint64(1), ctx.QueryID, "query ID")
return nil
},
}

discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
err = f.Close()
require.NoError(t, err, "close temp file")

dat, err := os.ReadFile(f.Name())
cont := strings.Contains(string(dat), "SELECT count(value) FROM cpu")
require.True(t, cont, "expected query output")
}

func discardOutput(results <-chan *query.Result) {
for range results {
// Read all results and discard.
Expand Down

0 comments on commit b9e9eb7

Please sign in to comment.