sql_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2016 Google Inc. 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 uuid
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. func TestScan(t *testing.T) {
  10. stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479"
  11. badTypeTest := 6
  12. invalidTest := "f47ac10b-58cc-0372-8567-0e02b2c3d4"
  13. byteTest := make([]byte, 16)
  14. byteTestUUID := Must(Parse(stringTest))
  15. copy(byteTest, byteTestUUID[:])
  16. // sunny day tests
  17. var uuid UUID
  18. err := (&uuid).Scan(stringTest)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. err = (&uuid).Scan([]byte(stringTest))
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. err = (&uuid).Scan(byteTest)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. // bad type tests
  31. err = (&uuid).Scan(badTypeTest)
  32. if err == nil {
  33. t.Error("int correctly parsed and shouldn't have")
  34. }
  35. if !strings.Contains(err.Error(), "unable to scan type") {
  36. t.Error("attempting to parse an int returned an incorrect error message")
  37. }
  38. // invalid/incomplete uuids
  39. err = (&uuid).Scan(invalidTest)
  40. if err == nil {
  41. t.Error("invalid uuid was parsed without error")
  42. }
  43. if !strings.Contains(err.Error(), "invalid UUID") {
  44. t.Error("attempting to parse an invalid UUID returned an incorrect error message")
  45. }
  46. err = (&uuid).Scan(byteTest[:len(byteTest)-2])
  47. if err == nil {
  48. t.Error("invalid byte uuid was parsed without error")
  49. }
  50. if !strings.Contains(err.Error(), "invalid UUID") {
  51. t.Error("attempting to parse an invalid byte UUID returned an incorrect error message")
  52. }
  53. // empty tests
  54. uuid = UUID{}
  55. var emptySlice []byte
  56. err = (&uuid).Scan(emptySlice)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. for _, v := range uuid {
  61. if v != 0 {
  62. t.Error("UUID was not nil after scanning empty byte slice")
  63. }
  64. }
  65. uuid = UUID{}
  66. var emptyString string
  67. err = (&uuid).Scan(emptyString)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. for _, v := range uuid {
  72. if v != 0 {
  73. t.Error("UUID was not nil after scanning empty byte slice")
  74. }
  75. }
  76. uuid = UUID{}
  77. err = (&uuid).Scan(nil)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. for _, v := range uuid {
  82. if v != 0 {
  83. t.Error("UUID was not nil after scanning nil")
  84. }
  85. }
  86. }
  87. func TestValue(t *testing.T) {
  88. stringTest := "f47ac10b-58cc-0372-8567-0e02b2c3d479"
  89. uuid := Must(Parse(stringTest))
  90. val, _ := uuid.Value()
  91. if val != stringTest {
  92. t.Error("Value() did not return expected string")
  93. }
  94. }