...

Source file src/github.com/99designs/gqlgen/graphql/uuid_test.go

Documentation: github.com/99designs/gqlgen/graphql

     1  package graphql
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/uuid"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestMarshalUUID(t *testing.T) {
    11  	t.Run("Null Values", func(t *testing.T) {
    12  		assert.Equal(t, "null", m2s(MarshalUUID(uuid.Nil)))
    13  	})
    14  
    15  	t.Run("Valid Values", func(t *testing.T) {
    16  
    17  		var values = []struct {
    18  			input    uuid.UUID
    19  			expected string
    20  		}{
    21  			{uuid.MustParse("fd5343a9-0372-11ee-9fb2-0242ac160014"), "\"fd5343a9-0372-11ee-9fb2-0242ac160014\""},
    22  		}
    23  		for _, v := range values {
    24  			assert.Equal(t, v.expected, m2s(MarshalUUID(v.input)))
    25  		}
    26  	})
    27  }
    28  
    29  func TestUnmarshalUUID(t *testing.T) {
    30  	t.Run("Invalid Non-String Values", func(t *testing.T) {
    31  		var values = []interface{}{123, 1.2345678901, 1.2e+20, 1.2e-20, true, false, nil}
    32  		for _, v := range values {
    33  			result, err := UnmarshalUUID(v)
    34  			assert.Equal(t, uuid.Nil, result)
    35  			assert.ErrorContains(t, err, "is not a uuid")
    36  		}
    37  	})
    38  
    39  	t.Run("Invalid String Values", func(t *testing.T) {
    40  		var values = []struct {
    41  			input    string
    42  			expected string
    43  		}{
    44  			{"X50e8400-e29b-41d4-a716-446655440000", "invalid UUID format"},
    45  			{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "invalid UUID format"},
    46  			{"F50e8400-e29b-41d4-a716-44665544000", "invalid UUID length: 35"},
    47  			{"aaa", "invalid UUID length: 3"},
    48  			{"", "invalid UUID length: 0"},
    49  		}
    50  		for _, v := range values {
    51  			result, err := UnmarshalUUID(v.input)
    52  			assert.Equal(t, uuid.Nil, result)
    53  			assert.ErrorContains(t, err, v.expected)
    54  		}
    55  	})
    56  }
    57  

View as plain text