github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/request/get_block_test.go (about)

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestGetBlock_InvalidParse(t *testing.T) {
    11  	var getBlock GetBlock
    12  
    13  	tooLong := make([]string, MaxBlockRequestHeightRange+1)
    14  	for i := range tooLong {
    15  		tooLong[i] = fmt.Sprintf("%d", i)
    16  	}
    17  
    18  	tests := []struct {
    19  		heights []string
    20  		start   string
    21  		end     string
    22  		outErr  string
    23  	}{
    24  		{nil, "", "", "must provide either heights or start and end height range"},
    25  		{[]string{"1"}, "1", "2", "can only provide either heights or start and end height range"},
    26  		{[]string{"final"}, "1", "2", "can only provide either heights or start and end height range"},
    27  		{[]string{"final", "1"}, "", "", "can not provide 'final' or 'sealed' values with other height values"},
    28  		{[]string{"final", "sealed"}, "", "", "can not provide 'final' or 'sealed' values with other height values"},
    29  		{[]string{"", ""}, "", "", "must provide either heights or start and end height range"},
    30  		{[]string{"-1"}, "", "", "invalid height format"},
    31  		{[]string{""}, "-1", "-2", "invalid height format"},
    32  		{[]string{""}, "foo", "10", "invalid height format"},
    33  		{[]string{""}, "10", "1", "start height must be less than or equal to end height"},
    34  		{[]string{""}, "1", "1000", "height range 999 exceeds maximum allowed of 50"},
    35  		{[]string{""}, "1", "", "must provide either heights or start and end height range"},
    36  		{tooLong, "", "", "at most 50 heights can be requested at a time"},
    37  	}
    38  
    39  	for i, test := range tests {
    40  		err := getBlock.Parse(test.heights, test.start, test.end)
    41  		assert.EqualError(t, err, test.outErr, fmt.Sprintf("test #%d -> %v", i, test))
    42  	}
    43  }
    44  
    45  func TestGetBlock_ValidParse(t *testing.T) {
    46  	tests := []struct {
    47  		heights []string
    48  		start   string
    49  		end     string
    50  	}{
    51  		{[]string{"final"}, "", ""},
    52  		{[]string{""}, "1", "5"},
    53  		{[]string{"1", "2", "3"}, "", ""},
    54  		{nil, "5", "final"},
    55  		{[]string{"sealed"}, "", ""},
    56  	}
    57  
    58  	var getBlock GetBlock
    59  	for i, test := range tests {
    60  		err := getBlock.Parse(test.heights, test.start, test.end)
    61  		assert.NoError(t, err, fmt.Sprintf("test #%d -> %v", i, test))
    62  	}
    63  
    64  	err := getBlock.Parse([]string{"1", "2"}, "", "")
    65  	assert.NoError(t, err)
    66  	assert.Equal(t, []uint64{1, 2}, getBlock.Heights)
    67  	assert.Equal(t, getBlock.EndHeight, EmptyHeight)
    68  	assert.Equal(t, getBlock.StartHeight, EmptyHeight)
    69  
    70  	err = getBlock.Parse([]string{}, "10", "20")
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, []uint64{}, getBlock.Heights)
    73  	assert.Equal(t, getBlock.EndHeight, uint64(20))
    74  	assert.Equal(t, getBlock.StartHeight, uint64(10))
    75  
    76  	err = getBlock.Parse([]string{}, "final", "sealed")
    77  	assert.NoError(t, err)
    78  	assert.Equal(t, getBlock.EndHeight, SealedHeight)
    79  	assert.Equal(t, getBlock.StartHeight, FinalHeight)
    80  }