github.com/tomwright/dasel@v1.27.3/node_query_internal_test.go (about)

     1  package dasel
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func assertErrResult(t *testing.T, expErr error, gotErr error) bool {
     9  	if expErr == nil && gotErr != nil {
    10  		t.Errorf("expected err %v, got %v", expErr, gotErr)
    11  		return false
    12  	}
    13  	if expErr != nil && gotErr == nil {
    14  		t.Errorf("expected err %v, got %v", expErr, gotErr)
    15  		return false
    16  	}
    17  	if expErr != nil && gotErr != nil && gotErr.Error() != expErr.Error() {
    18  		t.Errorf("expected err %v, got %v", expErr, gotErr)
    19  		return false
    20  	}
    21  	return true
    22  }
    23  
    24  func assertQueryResult(t *testing.T, exp reflect.Value, expErr error, got reflect.Value, gotErr error) bool {
    25  	if !assertErrResult(t, expErr, gotErr) {
    26  		return false
    27  	}
    28  
    29  	var res bool
    30  	if exp.IsValid() && got.IsValid() {
    31  		res = reflect.DeepEqual(exp.Interface(), got.Interface())
    32  	} else {
    33  		res = reflect.DeepEqual(exp, got)
    34  	}
    35  
    36  	if !res {
    37  		t.Errorf("expected result %v, got %v", exp, got)
    38  		return false
    39  	}
    40  	return true
    41  }
    42  
    43  func getNodeWithValue(value interface{}) *Node {
    44  	rootNode := New(value)
    45  	nextNode := &Node{
    46  		Previous: rootNode,
    47  	}
    48  	rootNode.Next = nextNode
    49  	return nextNode
    50  }
    51  
    52  func TestFindValueProperty(t *testing.T) {
    53  	t.Run("NilValue", func(t *testing.T) {
    54  		n := getNodeWithValue(nil)
    55  		n.Previous.Selector.Current = "."
    56  		got, err := findValueProperty(n, false)
    57  		assertQueryResult(t, nilValue(), &UnexpectedPreviousNilValue{Selector: "."}, got, err)
    58  	})
    59  	t.Run("NotFound", func(t *testing.T) {
    60  		n := getNodeWithValue(map[string]interface{}{})
    61  		n.Selector.Current = "x"
    62  		got, err := findValueProperty(n, false)
    63  		assertQueryResult(t, nilValue(), &ValueNotFound{Selector: n.Selector.Current, PreviousValue: n.Previous.Value}, got, err)
    64  	})
    65  	t.Run("UnsupportedType", func(t *testing.T) {
    66  		val := 0
    67  		n := getNodeWithValue(val)
    68  		n.Selector.Current = "x"
    69  		got, err := findValueProperty(n, false)
    70  		assertQueryResult(t, nilValue(), &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, got, err)
    71  	})
    72  }
    73  
    74  func TestFindValueIndex(t *testing.T) {
    75  	t.Run("NilValue", func(t *testing.T) {
    76  		n := getNodeWithValue(nil)
    77  		n.Previous.Selector.Current = "."
    78  		got, err := findValueIndex(n, false)
    79  		assertQueryResult(t, nilValue(), &UnexpectedPreviousNilValue{Selector: "."}, got, err)
    80  	})
    81  	t.Run("NotFound", func(t *testing.T) {
    82  		n := getNodeWithValue([]interface{}{})
    83  		n.Selector.Current = "[0]"
    84  		n.Selector.Index = 0
    85  		got, err := findValueIndex(n, false)
    86  		assertQueryResult(t, nilValue(), &ValueNotFound{Selector: n.Selector.Current, PreviousValue: n.Previous.Value}, got, err)
    87  	})
    88  	t.Run("UnsupportedType", func(t *testing.T) {
    89  		val := map[string]interface{}{}
    90  		n := getNodeWithValue(val)
    91  		n.Selector.Current = "[0]"
    92  		n.Selector.Index = 0
    93  		got, err := findValueIndex(n, false)
    94  		assertQueryResult(t, nilValue(), &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, got, err)
    95  	})
    96  }
    97  
    98  func TestFindValueNextAvailableIndex(t *testing.T) {
    99  	t.Run("NotFound", func(t *testing.T) {
   100  		n := getNodeWithValue([]interface{}{})
   101  		n.Selector.Current = "[0]"
   102  		n.Selector.Index = 0
   103  		got, err := findNextAvailableIndex(n, false)
   104  		assertQueryResult(t, nilValue(), &ValueNotFound{Selector: n.Selector.Current, PreviousValue: n.Previous.Value}, got, err)
   105  	})
   106  }
   107  
   108  func TestFindValueDynamic(t *testing.T) {
   109  	t.Run("NilValue", func(t *testing.T) {
   110  		n := getNodeWithValue(nil)
   111  		n.Previous.Selector.Current = "."
   112  		got, err := findValueDynamic(n, false)
   113  		assertQueryResult(t, nilValue(), &UnexpectedPreviousNilValue{Selector: "."}, got, err)
   114  	})
   115  	t.Run("NotFound", func(t *testing.T) {
   116  		n := getNodeWithValue([]interface{}{})
   117  		n.Selector.Current = "(name=x)"
   118  		n.Selector.Conditions = []Condition{
   119  			&EqualCondition{Key: "name", Value: "x"},
   120  		}
   121  		got, err := findValueDynamic(n, false)
   122  		assertQueryResult(t, nilValue(), &ValueNotFound{Selector: n.Selector.Current, PreviousValue: n.Previous.Value}, got, err)
   123  	})
   124  	t.Run("NotFoundMap", func(t *testing.T) {
   125  		n := getNodeWithValue(map[string]interface{}{})
   126  		n.Selector.Current = "(name=x)"
   127  		n.Selector.Conditions = []Condition{
   128  			&EqualCondition{Key: "name", Value: "x"},
   129  		}
   130  		got, err := findValueDynamic(n, false)
   131  		assertQueryResult(t, nilValue(), &ValueNotFound{Selector: n.Selector.Current, PreviousValue: n.Previous.Value}, got, err)
   132  	})
   133  	t.Run("NotFoundWithCreate", func(t *testing.T) {
   134  		n := getNodeWithValue([]interface{}{})
   135  		n.Selector.Current = "(name=x)"
   136  		n.Selector.Conditions = []Condition{
   137  			&EqualCondition{Key: "name", Value: "x"},
   138  		}
   139  		got, err := findValueDynamic(n, true)
   140  		if !assertQueryResult(t, nilValue(), nil, got, err) {
   141  			return
   142  		}
   143  		if exp, got := "NEXT_AVAILABLE_INDEX", n.Selector.Type; exp != got {
   144  			t.Errorf("expected type of %s, got %s", exp, got)
   145  			return
   146  		}
   147  	})
   148  	t.Run("UnsupportedType", func(t *testing.T) {
   149  		val := 0
   150  		n := getNodeWithValue(val)
   151  		n.Selector.Current = "(name=x)"
   152  		n.Selector.Conditions = []Condition{
   153  			&EqualCondition{Key: "name", Value: "x"},
   154  		}
   155  		got, err := findValueDynamic(n, false)
   156  		assertQueryResult(t, nilValue(), &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, got, err)
   157  	})
   158  }
   159  
   160  func TestFindValueLength(t *testing.T) {
   161  	t.Run("NilValue", func(t *testing.T) {
   162  		n := getNodeWithValue(nil)
   163  		n.Previous.Selector.Current = ".[#]"
   164  		got, err := findValueLength(n, false)
   165  		assertQueryResult(t, nilValue(), &UnexpectedPreviousNilValue{Selector: ".[#]"}, got, err)
   166  	})
   167  	t.Run("UnsupportedTypeInt", func(t *testing.T) {
   168  		val := 0
   169  		n := getNodeWithValue(val)
   170  		n.Selector.Current = ".[#]"
   171  		got, err := findValueLength(n, false)
   172  		assertQueryResult(t, nilValue(), &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, got, err)
   173  	})
   174  	t.Run("UnsupportedTypeBool", func(t *testing.T) {
   175  		val := false
   176  		n := getNodeWithValue(val)
   177  		n.Selector.Current = ".[#]"
   178  		got, err := findValueLength(n, false)
   179  		assertQueryResult(t, nilValue(), &UnsupportedTypeForSelector{Selector: n.Selector, Value: reflect.ValueOf(val)}, got, err)
   180  	})
   181  	t.Run("SliceType", func(t *testing.T) {
   182  		n := getNodeWithValue([]interface{}{"x", "y"})
   183  		n.Previous.Selector.Current = ".[#]"
   184  		got, err := findValueLength(n, false)
   185  		assertQueryResult(t, reflect.ValueOf(2), nil, got, err)
   186  	})
   187  	t.Run("MapType", func(t *testing.T) {
   188  		n := getNodeWithValue(map[string]interface{}{
   189  			"x": 1,
   190  			"y": 2,
   191  		})
   192  		n.Previous.Selector.Current = ".[#]"
   193  		got, err := findValueLength(n, false)
   194  		assertQueryResult(t, reflect.ValueOf(2), nil, got, err)
   195  	})
   196  	t.Run("StringType", func(t *testing.T) {
   197  		n := getNodeWithValue("hello")
   198  		n.Previous.Selector.Current = ".[#]"
   199  		got, err := findValueLength(n, false)
   200  		assertQueryResult(t, reflect.ValueOf(5), nil, got, err)
   201  	})
   202  }
   203  
   204  func TestFindValueType(t *testing.T) {
   205  	t.Run("NilValue", func(t *testing.T) {
   206  		n := getNodeWithValue(nil)
   207  		n.Previous.Selector.Current = ".[#]"
   208  		got, err := findValueType(n, false)
   209  		assertQueryResult(t, nilValue(), &UnexpectedPreviousNilValue{Selector: ".[#]"}, got, err)
   210  	})
   211  	t.Run("Int", func(t *testing.T) {
   212  		val := 0
   213  		n := getNodeWithValue(val)
   214  		n.Selector.Current = ".[#]"
   215  		got, err := findValueType(n, false)
   216  		assertQueryResult(t, reflect.ValueOf("int"), nil, got, err)
   217  	})
   218  	t.Run("Float", func(t *testing.T) {
   219  		val := 1.1
   220  		n := getNodeWithValue(val)
   221  		n.Selector.Current = ".[#]"
   222  		got, err := findValueType(n, false)
   223  		assertQueryResult(t, reflect.ValueOf("float"), nil, got, err)
   224  	})
   225  	t.Run("Bool", func(t *testing.T) {
   226  		val := true
   227  		n := getNodeWithValue(val)
   228  		n.Selector.Current = ".[#]"
   229  		got, err := findValueType(n, false)
   230  		assertQueryResult(t, reflect.ValueOf("bool"), nil, got, err)
   231  	})
   232  	t.Run("String", func(t *testing.T) {
   233  		val := "x"
   234  		n := getNodeWithValue(val)
   235  		n.Selector.Current = ".[#]"
   236  		got, err := findValueType(n, false)
   237  		assertQueryResult(t, reflect.ValueOf("string"), nil, got, err)
   238  	})
   239  	t.Run("Map", func(t *testing.T) {
   240  		val := map[string]interface{}{"x": 1}
   241  		n := getNodeWithValue(val)
   242  		n.Selector.Current = ".[#]"
   243  		got, err := findValueType(n, false)
   244  		assertQueryResult(t, reflect.ValueOf("map"), nil, got, err)
   245  	})
   246  	t.Run("Array", func(t *testing.T) {
   247  		val := []interface{}{1}
   248  		n := getNodeWithValue(val)
   249  		n.Selector.Current = ".[#]"
   250  		got, err := findValueType(n, false)
   251  		assertQueryResult(t, reflect.ValueOf("array"), nil, got, err)
   252  	})
   253  }
   254  
   255  func TestFindValue(t *testing.T) {
   256  	t.Run("MissingPreviousNode", func(t *testing.T) {
   257  		n := New(nil)
   258  		got, err := findValue(n, false)
   259  		assertQueryResult(t, nilValue(), ErrMissingPreviousNode, got, err)
   260  	})
   261  	t.Run("UnsupportedSelector", func(t *testing.T) {
   262  		n := getNodeWithValue([]interface{}{})
   263  		n.Selector.Raw = "BAD"
   264  		got, err := findValue(n, false)
   265  		assertQueryResult(t, nilValue(), &UnsupportedSelector{Selector: "BAD"}, got, err)
   266  	})
   267  }