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

     1  package command
     2  
     3  import (
     4  	"errors"
     5  	"github.com/tomwright/dasel/storage"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestPut_Document(t *testing.T) {
    11  	t.Run("SingleFailingWriter", func(t *testing.T) {
    12  		err := runPutDocumentCommand(putDocumentOpts{
    13  			Parser:         "json",
    14  			Selector:       ".[0]",
    15  			Reader:         strings.NewReader(`[{"name": "Tom"}]`),
    16  			DocumentString: `{"name": "Frank"}`,
    17  			Writer:         &failingWriter{},
    18  		}, nil)
    19  
    20  		if err == nil || !errors.Is(err, errFailingWriterErr) {
    21  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
    22  			return
    23  		}
    24  	})
    25  	t.Run("MultiFailingWriter", func(t *testing.T) {
    26  		err := runPutDocumentCommand(putDocumentOpts{
    27  			Parser:         "json",
    28  			Selector:       ".[*]",
    29  			Reader:         strings.NewReader(`[{"name": "Tom"}]`),
    30  			DocumentString: `{"name": "Frank"}`,
    31  			Writer:         &failingWriter{},
    32  			Multi:          true,
    33  		}, nil)
    34  
    35  		if err == nil || !errors.Is(err, errFailingWriterErr) {
    36  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
    37  			return
    38  		}
    39  	})
    40  	t.Run("InvalidDocumentParser", func(t *testing.T) {
    41  		err := runPutDocumentCommand(putDocumentOpts{
    42  			Parser:         "json",
    43  			Selector:       ".[*]",
    44  			Reader:         strings.NewReader(`[{"name": "Tom"}]`),
    45  			DocumentString: `{"name": "Frank"}`,
    46  			DocumentParser: "bad",
    47  		}, nil)
    48  
    49  		exp := &storage.UnknownParserErr{Parser: "bad"}
    50  
    51  		if err == nil || !strings.HasSuffix(err.Error(), exp.Error()) {
    52  			t.Errorf("expected error %v, got %v", exp, err)
    53  			return
    54  		}
    55  	})
    56  	t.Run("InvalidDocument", func(t *testing.T) {
    57  		err := runPutDocumentCommand(putDocumentOpts{
    58  			Parser:         "json",
    59  			Selector:       ".[*]",
    60  			Reader:         strings.NewReader(`[{"name": "Tom"}]`),
    61  			DocumentString: `{"name": "Frank}`,
    62  			DocumentParser: "json",
    63  		}, nil)
    64  
    65  		exp := "could not parse document: could not unmarshal data: unexpected EOF"
    66  
    67  		if err == nil || err.Error() != exp {
    68  			t.Errorf("expected error %v, got %v", exp, err)
    69  			return
    70  		}
    71  	})
    72  }