...

Source file src/github.com/jackc/pgtype/oid_value_test.go

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/jackc/pgtype"
     8  	"github.com/jackc/pgtype/testutil"
     9  )
    10  
    11  func TestOIDValueTranscode(t *testing.T) {
    12  	testutil.TestSuccessfulTranscode(t, "oid", []interface{}{
    13  		&pgtype.OIDValue{Uint: 42, Status: pgtype.Present},
    14  		&pgtype.OIDValue{Status: pgtype.Null},
    15  	})
    16  }
    17  
    18  func TestOIDValueSet(t *testing.T) {
    19  	successfulTests := []struct {
    20  		source interface{}
    21  		result pgtype.OIDValue
    22  	}{
    23  		{source: uint32(1), result: pgtype.OIDValue{Uint: 1, Status: pgtype.Present}},
    24  	}
    25  
    26  	for i, tt := range successfulTests {
    27  		var r pgtype.OIDValue
    28  		err := r.Set(tt.source)
    29  		if err != nil {
    30  			t.Errorf("%d: %v", i, err)
    31  		}
    32  
    33  		if r != tt.result {
    34  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    35  		}
    36  	}
    37  }
    38  
    39  func TestOIDValueAssignTo(t *testing.T) {
    40  	var ui32 uint32
    41  	var pui32 *uint32
    42  
    43  	simpleTests := []struct {
    44  		src      pgtype.OIDValue
    45  		dst      interface{}
    46  		expected interface{}
    47  	}{
    48  		{src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
    49  		{src: pgtype.OIDValue{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))},
    50  	}
    51  
    52  	for i, tt := range simpleTests {
    53  		err := tt.src.AssignTo(tt.dst)
    54  		if err != nil {
    55  			t.Errorf("%d: %v", i, err)
    56  		}
    57  
    58  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
    59  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
    60  		}
    61  	}
    62  
    63  	pointerAllocTests := []struct {
    64  		src      pgtype.OIDValue
    65  		dst      interface{}
    66  		expected interface{}
    67  	}{
    68  		{src: pgtype.OIDValue{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)},
    69  	}
    70  
    71  	for i, tt := range pointerAllocTests {
    72  		err := tt.src.AssignTo(tt.dst)
    73  		if err != nil {
    74  			t.Errorf("%d: %v", i, err)
    75  		}
    76  
    77  		if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
    78  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
    79  		}
    80  	}
    81  
    82  	errorTests := []struct {
    83  		src pgtype.OIDValue
    84  		dst interface{}
    85  	}{
    86  		{src: pgtype.OIDValue{Status: pgtype.Null}, dst: &ui32},
    87  	}
    88  
    89  	for i, tt := range errorTests {
    90  		err := tt.src.AssignTo(tt.dst)
    91  		if err == nil {
    92  			t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
    93  		}
    94  	}
    95  }
    96  

View as plain text