github.com/blend/go-sdk@v1.20220411.3/stats/mock_event_collector_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 stats
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestMockEventCollectorDefaultTags(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	assertTags := func(actualTags []string) {
    20  		assert.Len(actualTags, 3)
    21  		assert.Equal("k1:v1", actualTags[0])
    22  		assert.Equal("k2:v2", actualTags[1])
    23  		assert.Equal("k3:v3", actualTags[2])
    24  	}
    25  
    26  	collector := NewMockEventCollector()
    27  	collector.AddDefaultTags(Tag("k1", "v1"))
    28  	collector.AddDefaultTags(Tag("k2", "v2"))
    29  
    30  	tags := collector.DefaultTags()
    31  	assert.Len(tags, 2)
    32  	assert.Equal("k1:v1", tags[0])
    33  	assert.Equal("k2:v2", tags[1])
    34  
    35  	event := collector.CreateEvent("event", "text", "k3:v3")
    36  	assertTags(event.Tags)
    37  
    38  	go func() { _ = collector.SendEvent(event) }()
    39  	receivedEvent := <-collector.Events
    40  	assertTags(receivedEvent.Tags)
    41  }
    42  
    43  func TestMockEventCollectorCreateEvent(t *testing.T) {
    44  	assert := assert.New(t)
    45  
    46  	collector := NewMockEventCollector()
    47  	event := collector.CreateEvent("event", "text", "k:v")
    48  	assert.Equal("event", event.Title)
    49  	assert.Equal("text", event.Text)
    50  	assert.Len(event.Tags, 1)
    51  	assert.Equal("k:v", event.Tags[0])
    52  }
    53  
    54  func TestMockEventCollectorSendEvent(t *testing.T) {
    55  	assert := assert.New(t)
    56  
    57  	collector := NewMockEventCollector()
    58  	event := collector.CreateEvent("event", "text", "k:v")
    59  
    60  	go func() { _ = collector.SendEvent(event) }()
    61  	receivedEvent := <-collector.Events
    62  	assert.Equal("event", receivedEvent.Title)
    63  	assert.Equal("text", receivedEvent.Text)
    64  }