github.com/tomwright/dasel@v1.27.3/internal/command/put_object_internal_test.go (about)

     1  package command
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestPut_Object(t *testing.T) {
    11  	t.Run("SingleFailingWriter", func(t *testing.T) {
    12  		err := runPutObjectCommand(putObjectOpts{
    13  			Parser:      "json",
    14  			Selector:    ".[0]",
    15  			Reader:      strings.NewReader(`[{"name": "Tom"}]`),
    16  			InputValues: []string{"name=Frank"},
    17  			InputTypes:  []string{"string"},
    18  			Writer:      &failingWriter{},
    19  		}, nil)
    20  
    21  		if err == nil || !errors.Is(err, errFailingWriterErr) {
    22  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
    23  			return
    24  		}
    25  	})
    26  	t.Run("MultiFailingWriter", func(t *testing.T) {
    27  		err := runPutObjectCommand(putObjectOpts{
    28  			Parser:      "json",
    29  			Selector:    ".[*]",
    30  			Reader:      strings.NewReader(`[{"name": "Tom"}]`),
    31  			InputValues: []string{"name=Frank"},
    32  			InputTypes:  []string{"string"},
    33  			Writer:      &failingWriter{},
    34  			Multi:       true,
    35  		}, nil)
    36  
    37  		if err == nil || !errors.Is(err, errFailingWriterErr) {
    38  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
    39  			return
    40  		}
    41  	})
    42  }
    43  
    44  func TestGetMapFromTypes(t *testing.T) {
    45  	t.Run("Valid", func(t *testing.T) {
    46  		exp := map[string]interface{}{
    47  			"name":   "Tom",
    48  			"age":    int64(27),
    49  			"active": true,
    50  		}
    51  		got, err := getMapFromTypesValues([]string{"string", "int", "bool"}, []string{"name=Tom", "age=27", "active=true"})
    52  		if err != nil {
    53  			t.Errorf("unexpected error: %s", err)
    54  			return
    55  		}
    56  		if !reflect.DeepEqual(exp, got) {
    57  			t.Errorf("expected %v, got %v", exp, got)
    58  		}
    59  	})
    60  	t.Run("InvalidTypes", func(t *testing.T) {
    61  		_, err := getMapFromTypesValues([]string{}, []string{"name=Tom"})
    62  		if exp := "exactly 1 types are required, got 0"; err == nil || exp != err.Error() {
    63  			t.Errorf("expected %v, got %v", exp, err)
    64  			return
    65  		}
    66  	})
    67  	t.Run("InvalidValue", func(t *testing.T) {
    68  		_, err := getMapFromTypesValues([]string{"int"}, []string{"x=asd"})
    69  		if exp := "could not parse value [x]: could not parse int [asd]: strconv.ParseInt: parsing \"asd\": invalid syntax"; err == nil || exp != err.Error() {
    70  			t.Errorf("expected %v, got %v", exp, err)
    71  			return
    72  		}
    73  	})
    74  }