...

Source file src/github.com/jackc/pgtype/xid_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 TestXIDTranscode(t *testing.T) {
    12  	pgTypeName := "xid"
    13  	values := []interface{}{
    14  		&pgtype.XID{Uint: 42, Status: pgtype.Present},
    15  		&pgtype.XID{Status: pgtype.Null},
    16  	}
    17  	eqFunc := func(a, b interface{}) bool {
    18  		return reflect.DeepEqual(a, b)
    19  	}
    20  
    21  	testutil.TestPgxSuccessfulTranscodeEqFunc(t, pgTypeName, values, eqFunc)
    22  
    23  	for _, driverName := range []string{"github.com/lib/pq", "github.com/jackc/pgx/stdlib"} {
    24  		testutil.TestDatabaseSQLSuccessfulTranscodeEqFunc(t, driverName, pgTypeName, values, eqFunc)
    25  	}
    26  }
    27  
    28  func TestXIDSet(t *testing.T) {
    29  	successfulTests := []struct {
    30  		source interface{}
    31  		result pgtype.XID
    32  	}{
    33  		{source: uint32(1), result: pgtype.XID{Uint: 1, Status: pgtype.Present}},
    34  	}
    35  
    36  	for i, tt := range successfulTests {
    37  		var r pgtype.XID
    38  		err := r.Set(tt.source)
    39  		if err != nil {
    40  			t.Errorf("%d: %v", i, err)
    41  		}
    42  
    43  		if r != tt.result {
    44  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    45  		}
    46  	}
    47  }
    48  
    49  func TestXIDAssignTo(t *testing.T) {
    50  	var ui32 uint32
    51  	var pui32 *uint32
    52  
    53  	simpleTests := []struct {
    54  		src      pgtype.XID
    55  		dst      interface{}
    56  		expected interface{}
    57  	}{
    58  		{src: pgtype.XID{Uint: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
    59  		{src: pgtype.XID{Status: pgtype.Null}, dst: &pui32, expected: ((*uint32)(nil))},
    60  	}
    61  
    62  	for i, tt := range simpleTests {
    63  		err := tt.src.AssignTo(tt.dst)
    64  		if err != nil {
    65  			t.Errorf("%d: %v", i, err)
    66  		}
    67  
    68  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
    69  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
    70  		}
    71  	}
    72  
    73  	pointerAllocTests := []struct {
    74  		src      pgtype.XID
    75  		dst      interface{}
    76  		expected interface{}
    77  	}{
    78  		{src: pgtype.XID{Uint: 42, Status: pgtype.Present}, dst: &pui32, expected: uint32(42)},
    79  	}
    80  
    81  	for i, tt := range pointerAllocTests {
    82  		err := tt.src.AssignTo(tt.dst)
    83  		if err != nil {
    84  			t.Errorf("%d: %v", i, err)
    85  		}
    86  
    87  		if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
    88  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
    89  		}
    90  	}
    91  
    92  	errorTests := []struct {
    93  		src pgtype.XID
    94  		dst interface{}
    95  	}{
    96  		{src: pgtype.XID{Status: pgtype.Null}, dst: &ui32},
    97  	}
    98  
    99  	for i, tt := range errorTests {
   100  		err := tt.src.AssignTo(tt.dst)
   101  		if err == nil {
   102  			t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
   103  		}
   104  	}
   105  }
   106  

View as plain text