code.vegaprotocol.io/vega@v0.79.0/datanode/entities/pagination_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package entities_test
    17  
    18  import (
    19  	"encoding/base64"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/libs/ptr"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestCursorPaginationFromProto_Combinations(t *testing.T) {
    30  	// returns default when pagination is nil
    31  	cursor, err := entities.CursorPaginationFromProto(nil)
    32  	assert.NoError(t, err)
    33  	assert.Equal(t, entities.DefaultCursorPagination(true), cursor)
    34  
    35  	// returns default but uses newest first variable when defined
    36  	pagination := &v2.Pagination{NewestFirst: ptr.From(false)}
    37  	cursor, err = entities.CursorPaginationFromProto(pagination)
    38  	assert.NoError(t, err)
    39  	assert.Equal(t, entities.DefaultCursorPagination(false), cursor)
    40  
    41  	// uses the first filter
    42  	pagination = &v2.Pagination{First: ptr.From[int32](100)}
    43  	cursor, err = entities.CursorPaginationFromProto(pagination)
    44  	assert.NoError(t, err)
    45  	assert.Equal(t, entities.CursorPagination{
    46  		NewestFirst: true,
    47  		Forward:     &entities.CursorOffset{Limit: ptr.From[int32](100)},
    48  	}, cursor)
    49  
    50  	// uses the last filter
    51  	pagination = &v2.Pagination{Last: ptr.From[int32](50)}
    52  	cursor, err = entities.CursorPaginationFromProto(pagination)
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, entities.CursorPagination{
    55  		NewestFirst: true,
    56  		Backward:    &entities.CursorOffset{Limit: ptr.From[int32](50)},
    57  	}, cursor)
    58  
    59  	encodedTestCursor := base64.StdEncoding.EncodeToString([]byte("abcd"))
    60  	testCursor := &entities.Cursor{}
    61  	testCursor.Decode(encodedTestCursor)
    62  	assert.NoError(t, err)
    63  
    64  	defaultPageSize := ptr.From[int32](entities.DefaultPageSize)
    65  
    66  	// uses after filter
    67  	pagination = &v2.Pagination{After: ptr.From(encodedTestCursor)}
    68  	cursor, err = entities.CursorPaginationFromProto(pagination)
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, entities.CursorPagination{
    71  		NewestFirst: true,
    72  		Forward: &entities.CursorOffset{
    73  			Limit:  defaultPageSize,
    74  			Cursor: testCursor,
    75  		},
    76  	}, cursor)
    77  
    78  	// uses before filter
    79  	pagination = &v2.Pagination{Before: ptr.From(encodedTestCursor)}
    80  	cursor, err = entities.CursorPaginationFromProto(pagination)
    81  	assert.NoError(t, err)
    82  	assert.Equal(t, entities.CursorPagination{
    83  		NewestFirst: true,
    84  		Backward: &entities.CursorOffset{
    85  			Limit:  defaultPageSize,
    86  			Cursor: testCursor,
    87  		},
    88  	}, cursor)
    89  
    90  	// uses the first and after filters in conjunction
    91  	pagination = &v2.Pagination{First: ptr.From[int32](200), After: ptr.From(encodedTestCursor)}
    92  	cursor, err = entities.CursorPaginationFromProto(pagination)
    93  	assert.NoError(t, err)
    94  	assert.Equal(t, entities.CursorPagination{
    95  		NewestFirst: true,
    96  		Forward: &entities.CursorOffset{
    97  			Limit:  ptr.From[int32](200),
    98  			Cursor: testCursor,
    99  		},
   100  	}, cursor)
   101  
   102  	// uses the last and before filters in conjunction
   103  	pagination = &v2.Pagination{Last: ptr.From[int32](200), Before: ptr.From(encodedTestCursor)}
   104  	cursor, err = entities.CursorPaginationFromProto(pagination)
   105  	assert.NoError(t, err)
   106  	assert.Equal(t, entities.CursorPagination{
   107  		NewestFirst: true,
   108  		Backward: &entities.CursorOffset{
   109  			Limit:  ptr.From[int32](200),
   110  			Cursor: testCursor,
   111  		},
   112  	}, cursor)
   113  
   114  	// using first and before ignores before
   115  	pagination = &v2.Pagination{First: ptr.From[int32](30), Before: ptr.From(encodedTestCursor)}
   116  	cursor, err = entities.CursorPaginationFromProto(pagination)
   117  	assert.NoError(t, err)
   118  	assert.Equal(t, entities.CursorPagination{
   119  		NewestFirst: true,
   120  		Forward: &entities.CursorOffset{
   121  			Limit: ptr.From[int32](30),
   122  		},
   123  	}, cursor)
   124  
   125  	// using last and after ignores after
   126  	pagination = &v2.Pagination{Last: ptr.From[int32](50), After: ptr.From(encodedTestCursor)}
   127  	cursor, err = entities.CursorPaginationFromProto(pagination)
   128  	assert.NoError(t, err)
   129  	assert.Equal(t, entities.CursorPagination{
   130  		NewestFirst: true,
   131  		Backward: &entities.CursorOffset{
   132  			Limit: ptr.From[int32](50),
   133  		},
   134  	}, cursor)
   135  }