github.com/blend/go-sdk@v1.20220411.3/uuid/uuid_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package uuid
     9  
    10  import (
    11  	"encoding/json"
    12  	"fmt"
    13  	"testing"
    14  
    15  	"gopkg.in/yaml.v3"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  )
    19  
    20  func TestV4(t *testing.T) {
    21  	m := make(map[string]bool)
    22  	for x := 1; x < 32; x++ {
    23  		uuid := V4()
    24  		s := uuid.ToFullString()
    25  		if m[s] {
    26  			t.Errorf("NewRandom returned duplicated UUID %s\n", s)
    27  		}
    28  		m[s] = true
    29  		if v := uuid.Version(); v != 4 {
    30  			t.Errorf("Random UUID of version %v\n", v)
    31  		}
    32  	}
    33  }
    34  
    35  func makeTestUUIDv4(versionNumber byte, variant byte) UUID {
    36  	return []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, versionNumber, 0x0, variant, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
    37  }
    38  
    39  func TestIsUUIDv4(t *testing.T) {
    40  	assert := assert.New(t)
    41  
    42  	valid := makeTestUUIDv4(0x40, 0x80)
    43  	versionInvalid := makeTestUUIDv4(0xF0, 0x80)
    44  	variantInvalid := makeTestUUIDv4(0x40, 0xF0)
    45  	lengthInvalid := UUID([]byte{})
    46  
    47  	assert.True(valid.IsV4())
    48  	assert.False(variantInvalid.IsV4())
    49  	assert.False(versionInvalid.IsV4())
    50  	assert.False(lengthInvalid.IsV4())
    51  }
    52  
    53  func TestParseUUIDv4Valid(t *testing.T) {
    54  	assert := assert.New(t)
    55  
    56  	validShort := V4().ToShortString()
    57  	validParsedShort, err := Parse(validShort)
    58  	assert.Nil(err)
    59  	assert.True(validParsedShort.IsV4())
    60  	assert.Equal(validShort, validParsedShort.ToShortString())
    61  
    62  	validFull := V4().ToFullString()
    63  	validParsedFull, err := Parse(validFull)
    64  	assert.Nil(err)
    65  	assert.True(validParsedFull.IsV4())
    66  	assert.Equal(validFull, validParsedFull.ToFullString())
    67  
    68  	validBracedShort := fmt.Sprintf("{%s}", validShort)
    69  	validParsedBracedShort, err := Parse(validBracedShort)
    70  	assert.Nil(err)
    71  	assert.True(validParsedBracedShort.IsV4())
    72  	assert.Equal(validShort, validParsedBracedShort.ToShortString())
    73  
    74  	validBracedFull := fmt.Sprintf("{%s}", validFull)
    75  	validParsedBracedFull, err := Parse(validBracedFull)
    76  	assert.Nil(err)
    77  	assert.True(validParsedBracedFull.IsV4())
    78  	assert.Equal(validFull, validParsedBracedFull.ToFullString())
    79  }
    80  
    81  func TestParseUUIDv4Invalid(t *testing.T) {
    82  	assert := assert.New(t)
    83  
    84  	/*
    85  		_, err := Parse("")
    86  		assert.NotNil(err, "should handle empty strings")
    87  	*/
    88  
    89  	_, err := Parse("fcae3946f75d+3258678bb5e6795a6d3")
    90  	assert.NotNil(err, "should handle invalid characters")
    91  
    92  	_, err = Parse("4f2e28b7b8f94b9eba1d90c4452")
    93  	assert.NotNil(err, "should handle invalid length uuids")
    94  }
    95  
    96  type marshalTest struct {
    97  	ID UUID `json:"id" yaml:"id"`
    98  }
    99  
   100  func TestJSONMarshalers(t *testing.T) {
   101  	assert := assert.New(t)
   102  
   103  	id := V4()
   104  	rawJSON := []byte(fmt.Sprintf(`{"id":"%s"}`, id.ToFullString()))
   105  
   106  	var testVal marshalTest
   107  	assert.Nil(json.Unmarshal(rawJSON, &testVal))
   108  	assert.Equal(id.String(), testVal.ID.String())
   109  
   110  	newJSON, err := json.Marshal(testVal)
   111  	assert.Nil(err)
   112  
   113  	var verify marshalTest
   114  	assert.Nil(json.Unmarshal(newJSON, &verify))
   115  	assert.Equal(id.String(), verify.ID.String())
   116  }
   117  
   118  func TestJSONMarshalFormat(t *testing.T) {
   119  	assert := assert.New(t)
   120  
   121  	id := V4()
   122  
   123  	marshaled, err := id.MarshalJSON()
   124  	assert.Nil(err)
   125  	assert.Equal(fmt.Sprintf("%q", id.ToFullString()), string(marshaled))
   126  }
   127  
   128  func TestYAMLMarshalers(t *testing.T) {
   129  	assert := assert.New(t)
   130  
   131  	id := V4()
   132  	rawYAML := []byte(fmt.Sprintf(`id: "%s"`, id.ToFullString()))
   133  
   134  	var testVal marshalTest
   135  	assert.Nil(yaml.Unmarshal(rawYAML, &testVal))
   136  	assert.Equal(id.String(), testVal.ID.String())
   137  
   138  	newYAML, err := yaml.Marshal(testVal)
   139  	assert.Nil(err)
   140  
   141  	var verify marshalTest
   142  	assert.Nil(yaml.Unmarshal(newYAML, &verify))
   143  	assert.Equal(id.String(), verify.ID.String())
   144  }