OSDN Git Service

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