go-micro.dev/v5@v5.12.0/events/store_test.go (about)

     1  package events
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/uuid"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestStore(t *testing.T) {
    11  	store := NewStore()
    12  
    13  	testData := []Event{
    14  		{ID: uuid.New().String(), Topic: "foo"},
    15  		{ID: uuid.New().String(), Topic: "foo"},
    16  		{ID: uuid.New().String(), Topic: "bar"},
    17  	}
    18  
    19  	// write the records to the store
    20  	t.Run("Write", func(t *testing.T) {
    21  		for _, event := range testData {
    22  			err := store.Write(&event)
    23  			assert.Nilf(t, err, "Writing an event should not return an error")
    24  		}
    25  	})
    26  
    27  	// should not be able to read events from a blank topic
    28  	t.Run("ReadMissingTopic", func(t *testing.T) {
    29  		evs, err := store.Read("")
    30  		assert.Equal(t, err, ErrMissingTopic, "Reading a blank topic should return an error")
    31  		assert.Nil(t, evs, "No events should be returned")
    32  	})
    33  
    34  	// should only get the events from the topic requested
    35  	t.Run("ReadTopic", func(t *testing.T) {
    36  		evs, err := store.Read("foo")
    37  		assert.Nilf(t, err, "No error should be returned")
    38  		assert.Len(t, evs, 2, "Only the events for this topic should be returned")
    39  	})
    40  
    41  	// limits should be honoured
    42  	t.Run("ReadTopicLimit", func(t *testing.T) {
    43  		evs, err := store.Read("foo", ReadLimit(1))
    44  		assert.Nilf(t, err, "No error should be returned")
    45  		assert.Len(t, evs, 1, "The result should include no more than the read limit")
    46  	})
    47  }