...

Source file src/github.com/lib/pq/uuid_test.go

Documentation: github.com/lib/pq

     1  package pq
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestDecodeUUIDBinaryError(t *testing.T) {
    10  	t.Parallel()
    11  	_, err := decodeUUIDBinary([]byte{0x12, 0x34})
    12  
    13  	if err == nil {
    14  		t.Fatal("Expected error, got none")
    15  	}
    16  	if !strings.HasPrefix(err.Error(), "pq:") {
    17  		t.Errorf("Expected error to start with %q, got %q", "pq:", err.Error())
    18  	}
    19  	if !strings.Contains(err.Error(), "bad length: 2") {
    20  		t.Errorf("Expected error to contain length, got %q", err.Error())
    21  	}
    22  }
    23  
    24  func BenchmarkDecodeUUIDBinary(b *testing.B) {
    25  	x := []byte{0x03, 0xa3, 0x52, 0x2f, 0x89, 0x28, 0x49, 0x87, 0x84, 0xd6, 0x93, 0x7b, 0x36, 0xec, 0x27, 0x6f}
    26  
    27  	for i := 0; i < b.N; i++ {
    28  		decodeUUIDBinary(x)
    29  	}
    30  }
    31  
    32  func TestDecodeUUIDBackend(t *testing.T) {
    33  	db := openTestConn(t)
    34  	defer db.Close()
    35  
    36  	var s = "a0ecc91d-a13f-4fe4-9fce-7e09777cc70a"
    37  	var scanned interface{}
    38  
    39  	err := db.QueryRow(`SELECT $1::uuid`, s).Scan(&scanned)
    40  	if err != nil {
    41  		t.Fatalf("Expected no error, got %v", err)
    42  	}
    43  	if !reflect.DeepEqual(scanned, []byte(s)) {
    44  		t.Errorf("Expected []byte(%q), got %T(%q)", s, scanned, scanned)
    45  	}
    46  }
    47  

View as plain text