...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/db/conn_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/db

     1  package db_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
     8  )
     9  
    10  func TestMarshallParam(t *testing.T) {
    11  	tests := []struct {
    12  		name string
    13  		v    any
    14  		want any
    15  	}{
    16  		{
    17  			name: "nil",
    18  			v:    nil,
    19  			want: nil,
    20  		},
    21  		{
    22  			name: "empty map",
    23  			v:    map[string]string{},
    24  			want: nil,
    25  		},
    26  		{
    27  			name: "empty slice",
    28  			v:    []string{},
    29  			want: nil,
    30  		},
    31  		{
    32  			name: "empty struct",
    33  			v:    struct{}{},
    34  			want: nil,
    35  		},
    36  		{
    37  			name: "non-empty map",
    38  			v:    map[string]string{"key": "value"},
    39  			want: `{"key":"value"}`,
    40  		},
    41  		{
    42  			name: "non-empty slice",
    43  			v:    []string{"value"},
    44  			want: `["value"]`,
    45  		},
    46  		{
    47  			name: "non-empty struct",
    48  			v:    struct{ Key string }{Key: "value"},
    49  			want: `{"Key":"value"}`,
    50  		},
    51  		{
    52  			name: "non-marshallable",
    53  			v:    make(chan struct{}),
    54  			want: nil,
    55  		},
    56  	}
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			if got := db.MarshallParamToJSONB(tt.v); !reflect.DeepEqual(got, tt.want) {
    60  				t.Errorf("MarshallParamToJSONB() = %v, want %v", got, tt.want)
    61  			}
    62  		})
    63  	}
    64  }
    65