...

Source file src/github.com/google/uuid/sql_test.go

Documentation: github.com/google/uuid

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

View as plain text