github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/events/store/store_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/events/store/store_test.go
    14  
    15  package store
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/tickoalcantara12/micro/v3/service/events"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestStore(t *testing.T) {
    26  	store := NewStore()
    27  
    28  	testData := []events.Event{
    29  		{ID: uuid.New().String(), Topic: "foo"},
    30  		{ID: uuid.New().String(), Topic: "foo"},
    31  		{ID: uuid.New().String(), Topic: "bar"},
    32  	}
    33  
    34  	// write the records to the store
    35  	t.Run("Write", func(t *testing.T) {
    36  		for _, event := range testData {
    37  			err := store.Write(&event)
    38  			assert.Nilf(t, err, "Writing an event should not return an error")
    39  		}
    40  	})
    41  
    42  	// should not be able to read events from a blank topic
    43  	t.Run("ReadMissingTopic", func(t *testing.T) {
    44  		evs, err := store.Read("")
    45  		assert.Equal(t, err, events.ErrMissingTopic, "Reading a blank topic should return an error")
    46  		assert.Nil(t, evs, "No events should be returned")
    47  	})
    48  
    49  	// should only get the events from the topic requested
    50  	t.Run("ReadTopic", func(t *testing.T) {
    51  		evs, err := store.Read("foo")
    52  		assert.Nilf(t, err, "No error should be returned")
    53  		assert.Len(t, evs, 2, "Only the events for this topic should be returned")
    54  	})
    55  
    56  	// limits should be honoured
    57  	t.Run("ReadTopicLimit", func(t *testing.T) {
    58  		evs, err := store.Read("foo", events.ReadLimit(1))
    59  		assert.Nilf(t, err, "No error should be returned")
    60  		assert.Len(t, evs, 1, "The result should include no more than the read limit")
    61  	})
    62  }