uuid_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. )
  10. func TestUUIDNil(t *testing.T) {
  11. var uuid UUID
  12. want, got := "00000000-0000-0000-0000-000000000000", uuid.String()
  13. if want != got {
  14. t.Fatalf("TestNil: expected %q got %q", want, got)
  15. }
  16. }
  17. var testsUUID = []struct {
  18. input string
  19. variant int
  20. version int
  21. }{
  22. {"b4f00409-cef8-4822-802c-deb20704c365", VariantIETF, 4},
  23. {"f81d4fae-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  24. {"00000000-7dec-11d0-a765-00a0c91e6bf6", VariantIETF, 1},
  25. {"3051a8d7-aea7-1801-e0bf-bc539dd60cf3", VariantFuture, 1},
  26. {"3051a8d7-aea7-2801-e0bf-bc539dd60cf3", VariantFuture, 2},
  27. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 3},
  28. {"3051a8d7-aea7-4801-e0bf-bc539dd60cf3", VariantFuture, 4},
  29. {"3051a8d7-aea7-3801-e0bf-bc539dd60cf3", VariantFuture, 5},
  30. {"d0e817e1-e4b1-1801-3fe6-b4b60ccecf9d", VariantNCSCompat, 0},
  31. {"d0e817e1-e4b1-1801-bfe6-b4b60ccecf9d", VariantIETF, 1},
  32. {"d0e817e1-e4b1-1801-dfe6-b4b60ccecf9d", VariantMicrosoft, 0},
  33. {"d0e817e1-e4b1-1801-ffe6-b4b60ccecf9d", VariantFuture, 0},
  34. }
  35. func TestPredefinedUUID(t *testing.T) {
  36. for i := range testsUUID {
  37. uuid, err := ParseUUID(testsUUID[i].input)
  38. if err != nil {
  39. t.Errorf("ParseUUID #%d: %v", i, err)
  40. continue
  41. }
  42. if str := uuid.String(); str != testsUUID[i].input {
  43. t.Errorf("String #%d: expected %q got %q", i, testsUUID[i].input, str)
  44. continue
  45. }
  46. if variant := uuid.Variant(); variant != testsUUID[i].variant {
  47. t.Errorf("Variant #%d: expected %d got %d", i, testsUUID[i].variant, variant)
  48. }
  49. if testsUUID[i].variant == VariantIETF {
  50. if version := uuid.Version(); version != testsUUID[i].version {
  51. t.Errorf("Version #%d: expected %d got %d", i, testsUUID[i].version, version)
  52. }
  53. }
  54. json, err := uuid.MarshalJSON()
  55. if err != nil {
  56. t.Errorf("MarshalJSON #%d: %v", i, err)
  57. }
  58. expectedJson := `"` + testsUUID[i].input + `"`
  59. if string(json) != expectedJson {
  60. t.Errorf("MarshalJSON #%d: expected %v got %v", i, expectedJson, string(json))
  61. }
  62. var unmarshaled UUID
  63. err = unmarshaled.UnmarshalJSON(json)
  64. if err != nil {
  65. t.Errorf("UnmarshalJSON #%d: %v", i, err)
  66. }
  67. if unmarshaled != uuid {
  68. t.Errorf("UnmarshalJSON #%d: expected %v got %v", i, uuid, unmarshaled)
  69. }
  70. }
  71. }
  72. func TestRandomUUID(t *testing.T) {
  73. for i := 0; i < 20; i++ {
  74. uuid, err := RandomUUID()
  75. if err != nil {
  76. t.Errorf("RandomUUID: %v", err)
  77. }
  78. if variant := uuid.Variant(); variant != VariantIETF {
  79. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  80. }
  81. if version := uuid.Version(); version != 4 {
  82. t.Errorf("wrong version. expected %d got %d", 4, version)
  83. }
  84. }
  85. }
  86. func TestUUIDFromTime(t *testing.T) {
  87. date := time.Date(1982, 5, 5, 12, 34, 56, 0, time.UTC)
  88. uuid := UUIDFromTime(date)
  89. if uuid.Time() != date {
  90. t.Errorf("embedded time incorrect. Expected %v got %v", date, uuid.Time())
  91. }
  92. }
  93. func TestParseUUID(t *testing.T) {
  94. uuid, _ := ParseUUID("486f3a88-775b-11e3-ae07-d231feb1dc81")
  95. if uuid.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  96. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", uuid.Time())
  97. }
  98. }
  99. func TestTimeUUID(t *testing.T) {
  100. var node []byte
  101. timestamp := int64(0)
  102. for i := 0; i < 20; i++ {
  103. uuid := TimeUUID()
  104. if variant := uuid.Variant(); variant != VariantIETF {
  105. t.Errorf("wrong variant. expected %d got %d", VariantIETF, variant)
  106. }
  107. if version := uuid.Version(); version != 1 {
  108. t.Errorf("wrong version. expected %d got %d", 1, version)
  109. }
  110. if n := uuid.Node(); !bytes.Equal(n, node) && i > 0 {
  111. t.Errorf("wrong node. expected %x, got %x", node, n)
  112. } else if i == 0 {
  113. node = n
  114. }
  115. ts := uuid.Timestamp()
  116. if ts < timestamp {
  117. t.Errorf("timestamps must grow")
  118. }
  119. timestamp = ts
  120. }
  121. }
  122. func TestUnmarshalJSON(t *testing.T) {
  123. var withHyphens, withoutHypens, tooLong UUID
  124. withHyphens.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81"`))
  125. if withHyphens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  126. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withHyphens.Time())
  127. }
  128. withoutHypens.UnmarshalJSON([]byte(`"486f3a88775b11e3ae07d231feb1dc81"`))
  129. if withoutHypens.Time().Truncate(time.Second) != time.Date(2014, 1, 7, 5, 19, 29, 0, time.UTC) {
  130. t.Errorf("Expected date of 1/7/2014 at 5:19:29, got %v", withoutHypens.Time())
  131. }
  132. err := tooLong.UnmarshalJSON([]byte(`"486f3a88-775b-11e3-ae07-d231feb1dc81486f3a88"`))
  133. if err == nil {
  134. t.Errorf("no error for invalid JSON UUID")
  135. }
  136. }