...

Source file src/github.com/gofrs/uuid/uuid_test.go

Documentation: github.com/gofrs/uuid

     1  // Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining
     4  // a copy of this software and associated documentation files (the
     5  // "Software"), to deal in the Software without restriction, including
     6  // without limitation the rights to use, copy, modify, merge, publish,
     7  // distribute, sublicense, and/or sell copies of the Software, and to
     8  // permit persons to whom the Software is furnished to do so, subject to
     9  // the following conditions:
    10  //
    11  // The above copyright notice and this permission notice shall be
    12  // included in all copies or substantial portions of the Software.
    13  //
    14  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    15  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    16  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    17  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    18  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    19  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    20  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    21  
    22  package uuid
    23  
    24  import (
    25  	"bytes"
    26  	"fmt"
    27  	"testing"
    28  	"time"
    29  )
    30  
    31  func TestUUID(t *testing.T) {
    32  	t.Run("IsNil", testUUIDIsNil)
    33  	t.Run("Bytes", testUUIDBytes)
    34  	t.Run("String", testUUIDString)
    35  	t.Run("Version", testUUIDVersion)
    36  	t.Run("Variant", testUUIDVariant)
    37  	t.Run("SetVersion", testUUIDSetVersion)
    38  	t.Run("SetVariant", testUUIDSetVariant)
    39  	t.Run("Format", testUUIDFormat)
    40  }
    41  
    42  func testUUIDIsNil(t *testing.T) {
    43  	u := UUID{}
    44  	got := u.IsNil()
    45  	want := true
    46  	if got != want {
    47  		t.Errorf("%v.IsNil() = %t, want %t", u, got, want)
    48  	}
    49  }
    50  
    51  func testUUIDBytes(t *testing.T) {
    52  	got := codecTestUUID.Bytes()
    53  	want := codecTestData
    54  	if !bytes.Equal(got, want) {
    55  		t.Errorf("%v.Bytes() = %x, want %x", codecTestUUID, got, want)
    56  	}
    57  }
    58  
    59  func testUUIDString(t *testing.T) {
    60  	got := NamespaceDNS.String()
    61  	want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
    62  	if got != want {
    63  		t.Errorf("%v.String() = %q, want %q", NamespaceDNS, got, want)
    64  	}
    65  }
    66  
    67  func testUUIDVersion(t *testing.T) {
    68  	u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    69  	if got, want := u.Version(), V1; got != want {
    70  		t.Errorf("%v.Version() == %d, want %d", u, got, want)
    71  	}
    72  }
    73  
    74  func testUUIDVariant(t *testing.T) {
    75  	tests := []struct {
    76  		u    UUID
    77  		want byte
    78  	}{
    79  		{
    80  			u:    UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    81  			want: VariantNCS,
    82  		},
    83  		{
    84  			u:    UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    85  			want: VariantRFC4122,
    86  		},
    87  		{
    88  			u:    UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    89  			want: VariantMicrosoft,
    90  		},
    91  		{
    92  			u:    UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
    93  			want: VariantFuture,
    94  		},
    95  	}
    96  	for _, tt := range tests {
    97  		if got := tt.u.Variant(); got != tt.want {
    98  			t.Errorf("%v.Variant() == %d, want %d", tt.u, got, tt.want)
    99  		}
   100  	}
   101  }
   102  
   103  func testUUIDSetVersion(t *testing.T) {
   104  	u := UUID{}
   105  	want := V4
   106  	u.SetVersion(want)
   107  	if got := u.Version(); got != want {
   108  		t.Errorf("%v.Version() == %d after SetVersion(%d)", u, got, want)
   109  	}
   110  }
   111  
   112  func testUUIDSetVariant(t *testing.T) {
   113  	variants := []byte{
   114  		VariantNCS,
   115  		VariantRFC4122,
   116  		VariantMicrosoft,
   117  		VariantFuture,
   118  	}
   119  	for _, want := range variants {
   120  		u := UUID{}
   121  		u.SetVariant(want)
   122  		if got := u.Variant(); got != want {
   123  			t.Errorf("%v.Variant() == %d after SetVariant(%d)", u, got, want)
   124  		}
   125  	}
   126  }
   127  
   128  func testUUIDFormat(t *testing.T) {
   129  	val := Must(FromString("12345678-90ab-cdef-1234-567890abcdef"))
   130  	tests := []struct {
   131  		u    UUID
   132  		f    string
   133  		want string
   134  	}{
   135  		{u: val, f: "%s", want: "12345678-90ab-cdef-1234-567890abcdef"},
   136  		{u: val, f: "%S", want: "12345678-90AB-CDEF-1234-567890ABCDEF"},
   137  		{u: val, f: "%q", want: `"12345678-90ab-cdef-1234-567890abcdef"`},
   138  		{u: val, f: "%x", want: "1234567890abcdef1234567890abcdef"},
   139  		{u: val, f: "%X", want: "1234567890ABCDEF1234567890ABCDEF"},
   140  		{u: val, f: "%v", want: "12345678-90ab-cdef-1234-567890abcdef"},
   141  		{u: val, f: "%+v", want: "12345678-90ab-cdef-1234-567890abcdef"},
   142  		{u: val, f: "%#v", want: "[16]uint8{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}"},
   143  		{u: val, f: "%T", want: "uuid.UUID"},
   144  		{u: val, f: "%t", want: "%!t(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   145  		{u: val, f: "%b", want: "%!b(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   146  		{u: val, f: "%c", want: "%!c(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   147  		{u: val, f: "%d", want: "%!d(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   148  		{u: val, f: "%e", want: "%!e(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   149  		{u: val, f: "%E", want: "%!E(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   150  		{u: val, f: "%f", want: "%!f(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   151  		{u: val, f: "%F", want: "%!F(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   152  		{u: val, f: "%g", want: "%!g(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   153  		{u: val, f: "%G", want: "%!G(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   154  		{u: val, f: "%o", want: "%!o(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   155  		{u: val, f: "%U", want: "%!U(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
   156  	}
   157  	for _, tt := range tests {
   158  		got := fmt.Sprintf(tt.f, tt.u)
   159  		if tt.want != got {
   160  			t.Errorf(`Format("%s") got %s, want %s`, tt.f, got, tt.want)
   161  		}
   162  	}
   163  }
   164  
   165  func TestMust(t *testing.T) {
   166  	sentinel := fmt.Errorf("uuid: sentinel error")
   167  	defer func() {
   168  		r := recover()
   169  		if r == nil {
   170  			t.Fatalf("did not panic, want %v", sentinel)
   171  		}
   172  		err, ok := r.(error)
   173  		if !ok {
   174  			t.Fatalf("panicked with %T, want error (%v)", r, sentinel)
   175  		}
   176  		if err != sentinel {
   177  			t.Fatalf("panicked with %v, want %v", err, sentinel)
   178  		}
   179  	}()
   180  	fn := func() (UUID, error) {
   181  		return Nil, sentinel
   182  	}
   183  	Must(fn())
   184  }
   185  
   186  func TestTimeFromTimestamp(t *testing.T) {
   187  	tests := []struct {
   188  		t    Timestamp
   189  		want time.Time
   190  	}{
   191  		// a zero timestamp represents October 15, 1582 at midnight UTC
   192  		{t: Timestamp(0), want: time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)},
   193  		// a one value is 100ns later
   194  		{t: Timestamp(1), want: time.Date(1582, 10, 15, 0, 0, 0, 100, time.UTC)},
   195  		// 10 million 100ns intervals later is one second
   196  		{t: Timestamp(10000000), want: time.Date(1582, 10, 15, 0, 0, 1, 0, time.UTC)},
   197  		{t: Timestamp(60 * 10000000), want: time.Date(1582, 10, 15, 0, 1, 0, 0, time.UTC)},
   198  		{t: Timestamp(60 * 60 * 10000000), want: time.Date(1582, 10, 15, 1, 0, 0, 0, time.UTC)},
   199  		{t: Timestamp(24 * 60 * 60 * 10000000), want: time.Date(1582, 10, 16, 0, 0, 0, 0, time.UTC)},
   200  		{t: Timestamp(365 * 24 * 60 * 60 * 10000000), want: time.Date(1583, 10, 15, 0, 0, 0, 0, time.UTC)},
   201  		// maximum timestamp value in a UUID is the year 5236
   202  		{t: Timestamp(uint64(1<<60 - 1)), want: time.Date(5236, 03, 31, 21, 21, 0, 684697500, time.UTC)},
   203  	}
   204  	for _, tt := range tests {
   205  		got, _ := tt.t.Time()
   206  		if !got.Equal(tt.want) {
   207  			t.Errorf("%v.Time() == %v, want %v", tt.t, got, tt.want)
   208  		}
   209  	}
   210  }
   211  
   212  func TestTimestampFromV1(t *testing.T) {
   213  	tests := []struct {
   214  		u       UUID
   215  		want    Timestamp
   216  		wanterr bool
   217  	}{
   218  		{u: Must(NewV4()), wanterr: true},
   219  		{u: Must(FromString("00000000-0000-1000-0000-000000000000")), want: 0},
   220  		{u: Must(FromString("424f137e-a2aa-11e8-98d0-529269fb1459")), want: 137538640775418750},
   221  		{u: Must(FromString("ffffffff-ffff-1fff-ffff-ffffffffffff")), want: Timestamp(1<<60 - 1)},
   222  	}
   223  	for _, tt := range tests {
   224  		got, goterr := TimestampFromV1(tt.u)
   225  		if tt.wanterr && goterr == nil {
   226  			t.Errorf("TimestampFromV1(%v) want error, got %v", tt.u, got)
   227  		} else if tt.want != got {
   228  			t.Errorf("TimestampFromV1(%v) got %v, want %v", tt.u, got, tt.want)
   229  		}
   230  	}
   231  }
   232  
   233  func TestTimestampFromV6(t *testing.T) {
   234  	tests := []struct {
   235  		u       UUID
   236  		want    Timestamp
   237  		wanterr bool
   238  	}{
   239  		{u: Must(NewV1()), wanterr: true},
   240  		{u: Must(FromString("00000000-0000-6000-0000-000000000000")), want: 0},
   241  		{u: Must(FromString("1ec06cff-e9b1-621c-8627-ba3fd7e551c9")), want: 138493178941215260},
   242  		{u: Must(FromString("ffffffff-ffff-6fff-ffff-ffffffffffff")), want: Timestamp(1<<60 - 1)},
   243  	}
   244  
   245  	for _, tt := range tests {
   246  		got, err := TimestampFromV6(tt.u)
   247  
   248  		switch {
   249  		case tt.wanterr && err == nil:
   250  			t.Errorf("TimestampFromV6(%v) want error, got %v", tt.u, got)
   251  
   252  		case tt.want != got:
   253  			t.Errorf("TimestampFromV6(%v) got %v, want %v", tt.u, got, tt.want)
   254  		}
   255  	}
   256  }
   257  

View as plain text