...

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

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/jackc/pgtype"
     7  	"github.com/jackc/pgtype/testutil"
     8  )
     9  
    10  func TestPolygonTranscode(t *testing.T) {
    11  	testutil.TestSuccessfulTranscode(t, "polygon", []interface{}{
    12  		&pgtype.Polygon{
    13  			P:      []pgtype.Vec2{{3.14, 1.678901234}, {7.1, 5.234}, {5.0, 3.234}},
    14  			Status: pgtype.Present,
    15  		},
    16  		&pgtype.Polygon{
    17  			P:      []pgtype.Vec2{{3.14, -1.678}, {7.1, -5.234}, {23.1, 9.34}},
    18  			Status: pgtype.Present,
    19  		},
    20  		&pgtype.Polygon{Status: pgtype.Null},
    21  	})
    22  }
    23  
    24  func TestPolygon_Set(t *testing.T) {
    25  	tests := []struct {
    26  		name    string
    27  		arg     interface{}
    28  		status  pgtype.Status
    29  		wantErr bool
    30  	}{
    31  		{
    32  			name:    "string",
    33  			arg:     "((3.14,1.678901234),(7.1,5.234),(5.0,3.234))",
    34  			status:  pgtype.Present,
    35  			wantErr: false,
    36  		}, {
    37  			name:    "[]float64",
    38  			arg:     []float64{1, 2, 3.45, 6.78, 1.23, 4.567, 8.9, 1.0},
    39  			status:  pgtype.Present,
    40  			wantErr: false,
    41  		}, {
    42  			name:    "[]Vec2",
    43  			arg:     []pgtype.Vec2{{1, 2}, {2.3, 4.5}, {6.78, 9.123}},
    44  			status:  pgtype.Present,
    45  			wantErr: false,
    46  		}, {
    47  			name:    "null",
    48  			arg:     nil,
    49  			status:  pgtype.Null,
    50  			wantErr: false,
    51  		}, {
    52  			name:    "invalid_string_1",
    53  			arg:     "((3.14,1.678901234),(7.1,5.234),(5.0,3.234x))",
    54  			status:  pgtype.Undefined,
    55  			wantErr: true,
    56  		}, {
    57  			name:    "invalid_string_2",
    58  			arg:     "(3,4)",
    59  			status:  pgtype.Undefined,
    60  			wantErr: true,
    61  		}, {
    62  			name:    "invalid_[]float64",
    63  			arg:     []float64{1, 2, 3.45, 6.78, 1.23, 4.567, 8.9},
    64  			status:  pgtype.Undefined,
    65  			wantErr: true,
    66  		}, {
    67  			name:    "invalid_type",
    68  			arg:     []int{1, 2, 3, 6},
    69  			status:  pgtype.Undefined,
    70  			wantErr: true,
    71  		}, {
    72  			name:    "empty_[]float64",
    73  			arg:     []float64{},
    74  			status:  pgtype.Null,
    75  			wantErr: false,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			dst := &pgtype.Polygon{}
    81  			if err := dst.Set(tt.arg); (err != nil) != tt.wantErr {
    82  				t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
    83  			}
    84  			if dst.Status != tt.status {
    85  				t.Errorf("Expected status: %v; got: %v", tt.status, dst.Status)
    86  			}
    87  		})
    88  	}
    89  }
    90  

View as plain text