...

Source file src/github.com/jackc/pgx/v5/pgtype/point_test.go

Documentation: github.com/jackc/pgx/v5/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/jackc/pgx/v5/pgtype"
     9  	"github.com/jackc/pgx/v5/pgxtest"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPointCodec(t *testing.T) {
    14  	skipCockroachDB(t, "Server does not support type point")
    15  
    16  	pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "point", []pgxtest.ValueRoundTripTest{
    17  		{
    18  			pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Valid: true},
    19  			new(pgtype.Point),
    20  			isExpectedEq(pgtype.Point{P: pgtype.Vec2{1.234, 5.6789012345}, Valid: true}),
    21  		},
    22  		{
    23  			pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true},
    24  			new(pgtype.Point),
    25  			isExpectedEq(pgtype.Point{P: pgtype.Vec2{-1.234, -5.6789}, Valid: true}),
    26  		},
    27  		{pgtype.Point{}, new(pgtype.Point), isExpectedEq(pgtype.Point{})},
    28  		{nil, new(pgtype.Point), isExpectedEq(pgtype.Point{})},
    29  	})
    30  }
    31  
    32  func TestPoint_MarshalJSON(t *testing.T) {
    33  	tests := []struct {
    34  		name  string
    35  		point pgtype.Point
    36  		want  []byte
    37  	}{
    38  		{
    39  			name: "second",
    40  			point: pgtype.Point{
    41  				P:     pgtype.Vec2{X: 12.245, Y: 432.12},
    42  				Valid: true,
    43  			},
    44  			want: []byte(`"(12.245,432.12)"`),
    45  		},
    46  		{
    47  			name: "third",
    48  			point: pgtype.Point{
    49  				P: pgtype.Vec2{},
    50  			},
    51  			want: []byte("null"),
    52  		},
    53  	}
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			got, err := tt.point.MarshalJSON()
    57  			require.NoError(t, err)
    58  			if !reflect.DeepEqual(got, tt.want) {
    59  				t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestPoint_UnmarshalJSON(t *testing.T) {
    66  	tests := []struct {
    67  		name    string
    68  		valid   bool
    69  		arg     []byte
    70  		wantErr bool
    71  	}{
    72  		{
    73  			name:    "first",
    74  			valid:   true,
    75  			arg:     []byte(`"(123.123,54.12)"`),
    76  			wantErr: false,
    77  		},
    78  		{
    79  			name:    "second",
    80  			valid:   false,
    81  			arg:     []byte(`"(123.123,54.1sad2)"`),
    82  			wantErr: true,
    83  		},
    84  		{
    85  			name:    "third",
    86  			valid:   false,
    87  			arg:     []byte("null"),
    88  			wantErr: false,
    89  		},
    90  	}
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			dst := &pgtype.Point{}
    94  			if err := dst.UnmarshalJSON(tt.arg); (err != nil) != tt.wantErr {
    95  				t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
    96  			}
    97  			if dst.Valid != tt.valid {
    98  				t.Errorf("Valid mismatch: %v != %v", dst.Valid, tt.valid)
    99  			}
   100  		})
   101  	}
   102  }
   103  

View as plain text