github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/headers.go (about)

     1  package unittest
     2  
     3  import (
     4  	"github.com/stretchr/testify/mock"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	storerr "github.com/koko1123/flow-go-1/storage"
     8  	storage "github.com/koko1123/flow-go-1/storage/mock"
     9  )
    10  
    11  // HeadersFromMap creates a storage header mock that backed by a given map
    12  func HeadersFromMap(headerDB map[flow.Identifier]*flow.Header) *storage.Headers {
    13  	headers := &storage.Headers{}
    14  	headers.On("Store", mock.Anything).Return(
    15  		func(header *flow.Header) error {
    16  			headerDB[header.ID()] = header
    17  			return nil
    18  		},
    19  	)
    20  	headers.On("ByBlockID", mock.Anything).Return(
    21  		func(blockID flow.Identifier) *flow.Header {
    22  			return headerDB[blockID]
    23  		},
    24  		func(blockID flow.Identifier) error {
    25  			_, exists := headerDB[blockID]
    26  			if !exists {
    27  				return storerr.ErrNotFound
    28  			}
    29  			return nil
    30  		},
    31  	)
    32  
    33  	return headers
    34  }