github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/page_cursor_test.go (about)

     1  package graphql
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPageCursor_UnmarshalGQL(t *testing.T) {
    11  	for name, tc := range map[string]struct {
    12  		input    interface{}
    13  		err      bool
    14  		errmsg   string
    15  		expected PageCursor
    16  	}{
    17  		//given
    18  		"correct input": {
    19  			input:    "cursor",
    20  			err:      false,
    21  			expected: PageCursor("cursor"),
    22  		},
    23  		"error: input is nil": {
    24  			input:  nil,
    25  			err:    true,
    26  			errmsg: "Invalid data [reason=input should not be nil]",
    27  		},
    28  		"error: invalid input": {
    29  			input:  123,
    30  			err:    true,
    31  			errmsg: "unexpected input type: int, should be string",
    32  		},
    33  	} {
    34  		t.Run(name, func(t *testing.T) {
    35  			// WHEN
    36  			var pageCursor PageCursor
    37  			err := pageCursor.UnmarshalGQL(tc.input)
    38  
    39  			// THEN
    40  			if tc.err {
    41  				assert.Error(t, err)
    42  				assert.EqualError(t, err, tc.errmsg)
    43  				assert.Empty(t, pageCursor)
    44  			} else {
    45  				assert.NoError(t, err)
    46  				assert.Equal(t, tc.expected, pageCursor)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestPageCursor_MarshalGQL(t *testing.T) {
    53  	//given
    54  	fixCursor := PageCursor("cursor")
    55  	expectedCursor := `"cursor"`
    56  	buf := bytes.Buffer{}
    57  
    58  	// WHEN
    59  	fixCursor.MarshalGQL(&buf)
    60  
    61  	// THEN
    62  	assert.NotNil(t, buf)
    63  	assert.Equal(t, expectedCursor, buf.String())
    64  }