sql_test.go 2.0 KB

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