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

     1  package command
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  var jsonData = `{
    12    "id": "1111",
    13    "details": {
    14      "name": "Tom",
    15    	"age": 27,
    16      "addresses": [
    17        {
    18          "street": "101 Some Street",
    19          "town": "Some Town",
    20          "county": "Some Country",
    21          "postcode": "XXX XXX",
    22          "primary": true
    23        },
    24        {
    25          "street": "34 Another Street",
    26          "town": "Another Town",
    27          "county": "Another County",
    28          "postcode": "YYY YYY"
    29        }
    30      ]
    31    }
    32  }`
    33  
    34  var yamlData = `
    35  id: 1111
    36  details:
    37    name: Tom
    38    age: 27
    39    addresses:
    40    - street: 101 Some Street
    41      town: Some Town
    42      county: Some County
    43      postcode: XXX XXX
    44      primary: true
    45    - street: 34 Another Street
    46      town: Another Town
    47      county: Another County
    48      postcode: YYY YYY
    49  `
    50  
    51  var tomlData = `id = "1111"
    52  [details]
    53    name = "Tom"
    54    age = 27
    55    [[details.addresses]]
    56      street =  "101 Some Street"
    57      town = "Some Town"
    58      county = "Some County"
    59      postcode = "XXX XXX"
    60      primary = true
    61    [[details.addresses]]
    62      street = "34 Another Street"
    63      town = "Another Town"
    64      county = "Another County"
    65      postcode = "YYY YYY"
    66  `
    67  
    68  var errFailingWriterErr = errors.New("i am meant to fail at writing")
    69  
    70  type failingWriter struct {
    71  }
    72  
    73  func (fp *failingWriter) Write(_ []byte) (int, error) {
    74  	return 0, errFailingWriterErr
    75  }
    76  
    77  func selectTest(in string, parser string, selector string, out string, expErr error) func(t *testing.T) {
    78  	return func(t *testing.T) {
    79  		outputBuffer := bytes.NewBuffer([]byte{})
    80  
    81  		err := runSelectCommand(selectOptions{
    82  			Parser:   parser,
    83  			Selector: selector,
    84  			Reader:   strings.NewReader(in),
    85  			Writer:   outputBuffer,
    86  		}, nil)
    87  
    88  		if expErr == nil && err != nil {
    89  			t.Errorf("expected err %v, got %v", expErr, err)
    90  			return
    91  		}
    92  		if expErr != nil && err == nil {
    93  			t.Errorf("expected err %v, got %v", expErr, err)
    94  			return
    95  		}
    96  		if expErr != nil && err != nil && err.Error() != expErr.Error() {
    97  			t.Errorf("expected err %v, got %v", expErr, err)
    98  			return
    99  		}
   100  
   101  		output, err := io.ReadAll(outputBuffer)
   102  		if err != nil {
   103  			t.Errorf("unexpected error reading output buffer: %s", err)
   104  			return
   105  		}
   106  
   107  		if out != string(output) {
   108  			t.Errorf("expected result %v, got %v", out, string(output))
   109  		}
   110  	}
   111  }
   112  
   113  func newline(x string) string {
   114  	return x + "\n"
   115  }
   116  
   117  func TestSelect(t *testing.T) {
   118  	t.Run("SingleFailingWriter", func(t *testing.T) {
   119  		err := runSelectCommand(selectOptions{
   120  			Parser:   "json",
   121  			Selector: ".",
   122  			Reader:   strings.NewReader(`{"name": "Tom"}`),
   123  			Writer:   &failingWriter{},
   124  		}, nil)
   125  
   126  		if err == nil || !errors.Is(err, errFailingWriterErr) {
   127  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
   128  			return
   129  		}
   130  	})
   131  	t.Run("MultiFailingWriter", func(t *testing.T) {
   132  		err := runSelectCommand(selectOptions{
   133  			Parser:   "json",
   134  			Selector: ".",
   135  			Reader:   strings.NewReader(`{"name": "Tom"}`),
   136  			Writer:   &failingWriter{},
   137  			Multi:    true,
   138  		}, nil)
   139  
   140  		if err == nil || !errors.Is(err, errFailingWriterErr) {
   141  			t.Errorf("expected error %v, got %v", errFailingWriterErr, err)
   142  			return
   143  		}
   144  	})
   145  }
   146  
   147  func TestSelect_JSON(t *testing.T) {
   148  	t.Run("SingleProperty", selectTest(jsonData, "json", ".id", newline(`"1111"`), nil))
   149  	t.Run("ObjectProperty", selectTest(jsonData, "json", ".details.name", newline(`"Tom"`), nil))
   150  	t.Run("Index", selectTest(jsonData, "json", ".details.addresses.[0].street", newline(`"101 Some Street"`), nil))
   151  	t.Run("Index", selectTest(jsonData, "json", ".details.addresses.[1].street", newline(`"34 Another Street"`), nil))
   152  	t.Run("DynamicString", selectTest(jsonData, "json", ".details.addresses.(postcode=XXX XXX).street", newline(`"101 Some Street"`), nil))
   153  	t.Run("DynamicString", selectTest(jsonData, "json", ".details.addresses.(postcode=YYY YYY).street", newline(`"34 Another Street"`), nil))
   154  }
   155  
   156  func TestSelect_YAML(t *testing.T) {
   157  	t.Run("SingleProperty", selectTest(yamlData, "yaml", ".id", newline(`1111`), nil))
   158  	t.Run("ObjectProperty", selectTest(yamlData, "yaml", ".details.name", newline(`Tom`), nil))
   159  	t.Run("Index", selectTest(yamlData, "yaml", ".details.addresses.[0].street", newline(`101 Some Street`), nil))
   160  	t.Run("Index", selectTest(yamlData, "yaml", ".details.addresses.[1].street", newline(`34 Another Street`), nil))
   161  	t.Run("DynamicString", selectTest(yamlData, "yaml", ".details.addresses.(postcode=XXX XXX).street", newline(`101 Some Street`), nil))
   162  	t.Run("DynamicString", selectTest(yamlData, "yaml", ".details.addresses.(postcode=YYY YYY).street", newline(`34 Another Street`), nil))
   163  }
   164  
   165  func TestSelect_TOML(t *testing.T) {
   166  	t.Run("SingleProperty", selectTest(tomlData, "toml", ".id", newline(`1111`), nil))
   167  	t.Run("ObjectProperty", selectTest(tomlData, "toml", ".details.name", newline(`Tom`), nil))
   168  	t.Run("Index", selectTest(tomlData, "toml", ".details.addresses.[0].street", newline(`101 Some Street`), nil))
   169  	t.Run("Index", selectTest(tomlData, "toml", ".details.addresses.[1].street", newline(`34 Another Street`), nil))
   170  	t.Run("DynamicString", selectTest(tomlData, "toml", ".details.addresses.(postcode=XXX XXX).street", newline(`101 Some Street`), nil))
   171  	t.Run("DynamicString", selectTest(tomlData, "toml", ".details.addresses.(postcode=YYY YYY).street", newline(`34 Another Street`), nil))
   172  }