...

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

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

     1  package pgtype_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/jackc/pgx/v5/pgtype"
     8  	"github.com/jackc/pgx/v5/pgxtest"
     9  )
    10  
    11  func TestBoolCodec(t *testing.T) {
    12  	pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "bool", []pgxtest.ValueRoundTripTest{
    13  		{true, new(bool), isExpectedEq(true)},
    14  		{false, new(bool), isExpectedEq(false)},
    15  		{true, new(pgtype.Bool), isExpectedEq(pgtype.Bool{Bool: true, Valid: true})},
    16  		{pgtype.Bool{}, new(pgtype.Bool), isExpectedEq(pgtype.Bool{})},
    17  		{nil, new(*bool), isExpectedEq((*bool)(nil))},
    18  	})
    19  }
    20  
    21  func TestBoolMarshalJSON(t *testing.T) {
    22  	successfulTests := []struct {
    23  		source pgtype.Bool
    24  		result string
    25  	}{
    26  		{source: pgtype.Bool{}, result: "null"},
    27  		{source: pgtype.Bool{Bool: true, Valid: true}, result: "true"},
    28  		{source: pgtype.Bool{Bool: false, Valid: true}, result: "false"},
    29  	}
    30  	for i, tt := range successfulTests {
    31  		r, err := tt.source.MarshalJSON()
    32  		if err != nil {
    33  			t.Errorf("%d: %v", i, err)
    34  		}
    35  
    36  		if string(r) != tt.result {
    37  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
    38  		}
    39  	}
    40  }
    41  
    42  func TestBoolUnmarshalJSON(t *testing.T) {
    43  	successfulTests := []struct {
    44  		source string
    45  		result pgtype.Bool
    46  	}{
    47  		{source: "null", result: pgtype.Bool{}},
    48  		{source: "true", result: pgtype.Bool{Bool: true, Valid: true}},
    49  		{source: "false", result: pgtype.Bool{Bool: false, Valid: true}},
    50  	}
    51  	for i, tt := range successfulTests {
    52  		var r pgtype.Bool
    53  		err := r.UnmarshalJSON([]byte(tt.source))
    54  		if err != nil {
    55  			t.Errorf("%d: %v", i, err)
    56  		}
    57  
    58  		if r != tt.result {
    59  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    60  		}
    61  	}
    62  }
    63  

View as plain text