github.com/blend/go-sdk@v1.20220411.3/db/query_start_event_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package db
     9  
    10  import (
    11  	"bytes"
    12  	"context"
    13  	"encoding/json"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  	"github.com/blend/go-sdk/logger"
    18  )
    19  
    20  func Test_NewQueryStartEvent(t *testing.T) {
    21  	t.Parallel()
    22  	its := assert.New(t)
    23  
    24  	qe := NewQueryStartEvent("query-body",
    25  		OptQueryStartEventBody("event-body"),
    26  		OptQueryStartEventDatabase("event-database"),
    27  		OptQueryStartEventEngine("event-engine"),
    28  		OptQueryStartEventUsername("event-username"),
    29  		OptQueryStartEventLabel("event-query-label"),
    30  	)
    31  
    32  	its.Equal("event-body", qe.Body)
    33  	its.Equal("event-database", qe.Database)
    34  	its.Equal("event-engine", qe.Engine)
    35  	its.Equal("event-username", qe.Username)
    36  	its.Equal("event-query-label", qe.Label)
    37  
    38  	buf := new(bytes.Buffer)
    39  	noColor := logger.TextOutputFormatter{
    40  		NoColor: true,
    41  	}
    42  
    43  	qe.WriteText(noColor, buf)
    44  	its.Equal("[event-engine event-username@event-database] [event-query-label] event-body", buf.String())
    45  
    46  	contents, err := json.Marshal(qe)
    47  	its.Nil(err)
    48  	its.Contains(string(contents), "event-engine")
    49  }
    50  
    51  func Test_QueryStartEventListener(t *testing.T) {
    52  	assert := assert.New(t)
    53  
    54  	qe := NewQueryStartEvent("select 1")
    55  
    56  	var didCall bool
    57  	ml := NewQueryStartEventListener(func(ctx context.Context, ae QueryStartEvent) {
    58  		didCall = true
    59  	})
    60  	ml(context.Background(), qe)
    61  	assert.True(didCall)
    62  }