...

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

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"net"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/jackc/pgtype"
     9  	"github.com/jackc/pgtype/testutil"
    10  )
    11  
    12  func TestMacaddrArrayTranscode(t *testing.T) {
    13  	testutil.TestSuccessfulTranscode(t, "macaddr[]", []interface{}{
    14  		&pgtype.MacaddrArray{
    15  			Elements:   nil,
    16  			Dimensions: nil,
    17  			Status:     pgtype.Present,
    18  		},
    19  		&pgtype.MacaddrArray{
    20  			Elements: []pgtype.Macaddr{
    21  				{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
    22  				{Status: pgtype.Null},
    23  			},
    24  			Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
    25  			Status:     pgtype.Present,
    26  		},
    27  		&pgtype.MacaddrArray{Status: pgtype.Null},
    28  	})
    29  }
    30  
    31  func TestMacaddrArraySet(t *testing.T) {
    32  	successfulTests := []struct {
    33  		source interface{}
    34  		result pgtype.MacaddrArray
    35  	}{
    36  		{
    37  			source: []net.HardwareAddr{mustParseMacaddr(t, "01:23:45:67:89:ab")},
    38  			result: pgtype.MacaddrArray{
    39  				Elements:   []pgtype.Macaddr{{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}},
    40  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
    41  				Status:     pgtype.Present},
    42  		},
    43  		{
    44  			source: (([]net.HardwareAddr)(nil)),
    45  			result: pgtype.MacaddrArray{Status: pgtype.Null},
    46  		},
    47  		{
    48  			source: [][]net.HardwareAddr{
    49  				{mustParseMacaddr(t, "01:23:45:67:89:ab")},
    50  				{mustParseMacaddr(t, "cd:ef:01:23:45:67")}},
    51  			result: pgtype.MacaddrArray{
    52  				Elements: []pgtype.Macaddr{
    53  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
    54  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}},
    55  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
    56  				Status:     pgtype.Present},
    57  		},
    58  		{
    59  			source: [][][][]net.HardwareAddr{
    60  				{{{
    61  					mustParseMacaddr(t, "01:23:45:67:89:ab"),
    62  					mustParseMacaddr(t, "cd:ef:01:23:45:67"),
    63  					mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}},
    64  				{{{
    65  					mustParseMacaddr(t, "45:67:89:ab:cd:ef"),
    66  					mustParseMacaddr(t, "fe:dc:ba:98:76:54"),
    67  					mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}},
    68  			result: pgtype.MacaddrArray{
    69  				Elements: []pgtype.Macaddr{
    70  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
    71  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present},
    72  					{Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present},
    73  					{Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present},
    74  					{Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present},
    75  					{Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}},
    76  				Dimensions: []pgtype.ArrayDimension{
    77  					{LowerBound: 1, Length: 2},
    78  					{LowerBound: 1, Length: 1},
    79  					{LowerBound: 1, Length: 1},
    80  					{LowerBound: 1, Length: 3}},
    81  				Status: pgtype.Present},
    82  		},
    83  		{
    84  			source: [2][1]net.HardwareAddr{
    85  				{mustParseMacaddr(t, "01:23:45:67:89:ab")},
    86  				{mustParseMacaddr(t, "cd:ef:01:23:45:67")}},
    87  			result: pgtype.MacaddrArray{
    88  				Elements: []pgtype.Macaddr{
    89  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
    90  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}},
    91  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
    92  				Status:     pgtype.Present},
    93  		},
    94  		{
    95  			source: [2][1][1][3]net.HardwareAddr{
    96  				{{{
    97  					mustParseMacaddr(t, "01:23:45:67:89:ab"),
    98  					mustParseMacaddr(t, "cd:ef:01:23:45:67"),
    99  					mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}},
   100  				{{{
   101  					mustParseMacaddr(t, "45:67:89:ab:cd:ef"),
   102  					mustParseMacaddr(t, "fe:dc:ba:98:76:54"),
   103  					mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}},
   104  			result: pgtype.MacaddrArray{
   105  				Elements: []pgtype.Macaddr{
   106  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
   107  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present},
   108  					{Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present},
   109  					{Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present},
   110  					{Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present},
   111  					{Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}},
   112  				Dimensions: []pgtype.ArrayDimension{
   113  					{LowerBound: 1, Length: 2},
   114  					{LowerBound: 1, Length: 1},
   115  					{LowerBound: 1, Length: 1},
   116  					{LowerBound: 1, Length: 3}},
   117  				Status: pgtype.Present},
   118  		},
   119  	}
   120  
   121  	for i, tt := range successfulTests {
   122  		var r pgtype.MacaddrArray
   123  		err := r.Set(tt.source)
   124  		if err != nil {
   125  			t.Errorf("%d: %v", i, err)
   126  		}
   127  
   128  		if !reflect.DeepEqual(r, tt.result) {
   129  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
   130  		}
   131  	}
   132  }
   133  
   134  func TestMacaddrArrayAssignTo(t *testing.T) {
   135  	var macaddrSlice []net.HardwareAddr
   136  	var macaddrSliceDim2 [][]net.HardwareAddr
   137  	var macaddrSliceDim4 [][][][]net.HardwareAddr
   138  	var macaddrArrayDim2 [2][1]net.HardwareAddr
   139  	var macaddrArrayDim4 [2][1][1][3]net.HardwareAddr
   140  
   141  	simpleTests := []struct {
   142  		src      pgtype.MacaddrArray
   143  		dst      interface{}
   144  		expected interface{}
   145  	}{
   146  		{
   147  			src: pgtype.MacaddrArray{
   148  				Elements:   []pgtype.Macaddr{{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present}},
   149  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
   150  				Status:     pgtype.Present,
   151  			},
   152  			dst:      &macaddrSlice,
   153  			expected: []net.HardwareAddr{mustParseMacaddr(t, "01:23:45:67:89:ab")},
   154  		},
   155  		{
   156  			src: pgtype.MacaddrArray{
   157  				Elements:   []pgtype.Macaddr{{Status: pgtype.Null}},
   158  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
   159  				Status:     pgtype.Present,
   160  			},
   161  			dst:      &macaddrSlice,
   162  			expected: []net.HardwareAddr{nil},
   163  		},
   164  		{
   165  			src:      pgtype.MacaddrArray{Status: pgtype.Null},
   166  			dst:      &macaddrSlice,
   167  			expected: (([]net.HardwareAddr)(nil)),
   168  		},
   169  		{
   170  			src:      pgtype.MacaddrArray{Status: pgtype.Present},
   171  			dst:      &macaddrSlice,
   172  			expected: []net.HardwareAddr{},
   173  		},
   174  		{
   175  			src: pgtype.MacaddrArray{
   176  				Elements: []pgtype.Macaddr{
   177  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
   178  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}},
   179  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
   180  				Status:     pgtype.Present},
   181  			dst: &macaddrSliceDim2,
   182  			expected: [][]net.HardwareAddr{
   183  				{mustParseMacaddr(t, "01:23:45:67:89:ab")},
   184  				{mustParseMacaddr(t, "cd:ef:01:23:45:67")}},
   185  		},
   186  		{
   187  			src: pgtype.MacaddrArray{
   188  				Elements: []pgtype.Macaddr{
   189  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
   190  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present},
   191  					{Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present},
   192  					{Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present},
   193  					{Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present},
   194  					{Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}},
   195  				Dimensions: []pgtype.ArrayDimension{
   196  					{LowerBound: 1, Length: 2},
   197  					{LowerBound: 1, Length: 1},
   198  					{LowerBound: 1, Length: 1},
   199  					{LowerBound: 1, Length: 3}},
   200  				Status: pgtype.Present},
   201  			dst: &macaddrSliceDim4,
   202  			expected: [][][][]net.HardwareAddr{
   203  				{{{
   204  					mustParseMacaddr(t, "01:23:45:67:89:ab"),
   205  					mustParseMacaddr(t, "cd:ef:01:23:45:67"),
   206  					mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}},
   207  				{{{
   208  					mustParseMacaddr(t, "45:67:89:ab:cd:ef"),
   209  					mustParseMacaddr(t, "fe:dc:ba:98:76:54"),
   210  					mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}},
   211  		},
   212  		{
   213  			src: pgtype.MacaddrArray{
   214  				Elements: []pgtype.Macaddr{
   215  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
   216  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present}},
   217  				Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
   218  				Status:     pgtype.Present},
   219  			dst: &macaddrArrayDim2,
   220  			expected: [2][1]net.HardwareAddr{
   221  				{mustParseMacaddr(t, "01:23:45:67:89:ab")},
   222  				{mustParseMacaddr(t, "cd:ef:01:23:45:67")}},
   223  		},
   224  		{
   225  			src: pgtype.MacaddrArray{
   226  				Elements: []pgtype.Macaddr{
   227  					{Addr: mustParseMacaddr(t, "01:23:45:67:89:ab"), Status: pgtype.Present},
   228  					{Addr: mustParseMacaddr(t, "cd:ef:01:23:45:67"), Status: pgtype.Present},
   229  					{Addr: mustParseMacaddr(t, "89:ab:cd:ef:01:23"), Status: pgtype.Present},
   230  					{Addr: mustParseMacaddr(t, "45:67:89:ab:cd:ef"), Status: pgtype.Present},
   231  					{Addr: mustParseMacaddr(t, "fe:dc:ba:98:76:54"), Status: pgtype.Present},
   232  					{Addr: mustParseMacaddr(t, "32:10:fe:dc:ba:98"), Status: pgtype.Present}},
   233  				Dimensions: []pgtype.ArrayDimension{
   234  					{LowerBound: 1, Length: 2},
   235  					{LowerBound: 1, Length: 1},
   236  					{LowerBound: 1, Length: 1},
   237  					{LowerBound: 1, Length: 3}},
   238  				Status: pgtype.Present},
   239  			dst: &macaddrArrayDim4,
   240  			expected: [2][1][1][3]net.HardwareAddr{
   241  				{{{
   242  					mustParseMacaddr(t, "01:23:45:67:89:ab"),
   243  					mustParseMacaddr(t, "cd:ef:01:23:45:67"),
   244  					mustParseMacaddr(t, "89:ab:cd:ef:01:23")}}},
   245  				{{{
   246  					mustParseMacaddr(t, "45:67:89:ab:cd:ef"),
   247  					mustParseMacaddr(t, "fe:dc:ba:98:76:54"),
   248  					mustParseMacaddr(t, "32:10:fe:dc:ba:98")}}}},
   249  		},
   250  	}
   251  
   252  	for i, tt := range simpleTests {
   253  		err := tt.src.AssignTo(tt.dst)
   254  		if err != nil {
   255  			t.Errorf("%d: %v", i, err)
   256  		}
   257  
   258  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
   259  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
   260  		}
   261  	}
   262  }
   263  

View as plain text