github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/rc/params_test.go (about)

     1  package rc
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestErrParamNotFoundError(t *testing.T) {
    13  	e := ErrParamNotFound("key")
    14  	assert.Equal(t, "Didn't find key \"key\" in input", e.Error())
    15  }
    16  
    17  func TestIsErrParamNotFound(t *testing.T) {
    18  	assert.Equal(t, true, IsErrParamNotFound(ErrParamNotFound("key")))
    19  	assert.Equal(t, false, IsErrParamNotFound(nil))
    20  	assert.Equal(t, false, IsErrParamNotFound(errors.New("potato")))
    21  }
    22  
    23  func TestNotErrParamNotFound(t *testing.T) {
    24  	assert.Equal(t, false, NotErrParamNotFound(ErrParamNotFound("key")))
    25  	assert.Equal(t, false, NotErrParamNotFound(nil))
    26  	assert.Equal(t, true, NotErrParamNotFound(errors.New("potato")))
    27  }
    28  
    29  func TestIsErrParamInvalid(t *testing.T) {
    30  	e := ErrParamInvalid{errors.New("potato")}
    31  	assert.Equal(t, true, IsErrParamInvalid(e))
    32  	assert.Equal(t, false, IsErrParamInvalid(nil))
    33  	assert.Equal(t, false, IsErrParamInvalid(errors.New("potato")))
    34  }
    35  
    36  func TestReshape(t *testing.T) {
    37  	in := Params{
    38  		"String": "hello",
    39  		"Float":  4.2,
    40  	}
    41  	var out struct {
    42  		String string
    43  		Float  float64
    44  	}
    45  	require.NoError(t, Reshape(&out, in))
    46  	assert.Equal(t, "hello", out.String)
    47  	assert.Equal(t, 4.2, out.Float)
    48  	var inCopy = Params{}
    49  	require.NoError(t, Reshape(&inCopy, out))
    50  	assert.Equal(t, in, inCopy)
    51  
    52  	// Now a failure to marshal
    53  	var in2 func()
    54  	require.Error(t, Reshape(&inCopy, in2))
    55  
    56  	// Now a failure to unmarshal
    57  	require.Error(t, Reshape(&out, "string"))
    58  
    59  }
    60  
    61  func TestParamsGet(t *testing.T) {
    62  	in := Params{
    63  		"ok": 1,
    64  	}
    65  	v1, e1 := in.Get("ok")
    66  	assert.NoError(t, e1)
    67  	assert.Equal(t, 1, v1)
    68  	v2, e2 := in.Get("notOK")
    69  	assert.Error(t, e2)
    70  	assert.Equal(t, nil, v2)
    71  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
    72  }
    73  
    74  func TestParamsGetString(t *testing.T) {
    75  	in := Params{
    76  		"string":    "one",
    77  		"notString": 17,
    78  	}
    79  	v1, e1 := in.GetString("string")
    80  	assert.NoError(t, e1)
    81  	assert.Equal(t, "one", v1)
    82  	v2, e2 := in.GetString("notOK")
    83  	assert.Error(t, e2)
    84  	assert.Equal(t, "", v2)
    85  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
    86  	v3, e3 := in.GetString("notString")
    87  	assert.Error(t, e3)
    88  	assert.Equal(t, "", v3)
    89  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
    90  }
    91  
    92  func TestParamsGetInt64(t *testing.T) {
    93  	for _, test := range []struct {
    94  		value     interface{}
    95  		result    int64
    96  		errString string
    97  	}{
    98  		{"123", 123, ""},
    99  		{"123x", 0, "couldn't parse"},
   100  		{int(12), 12, ""},
   101  		{int64(13), 13, ""},
   102  		{float64(14), 14, ""},
   103  		{float64(9.3e18), 0, "overflows int64"},
   104  		{float64(-9.3e18), 0, "overflows int64"},
   105  	} {
   106  		t.Run(fmt.Sprintf("%T=%v", test.value, test.value), func(t *testing.T) {
   107  			in := Params{
   108  				"key": test.value,
   109  			}
   110  			v1, e1 := in.GetInt64("key")
   111  			if test.errString == "" {
   112  				require.NoError(t, e1)
   113  				assert.Equal(t, test.result, v1)
   114  			} else {
   115  				require.NotNil(t, e1)
   116  				require.Error(t, e1)
   117  				assert.Contains(t, e1.Error(), test.errString)
   118  				assert.Equal(t, int64(0), v1)
   119  			}
   120  		})
   121  	}
   122  	in := Params{
   123  		"notInt64": []string{"a", "b"},
   124  	}
   125  	v2, e2 := in.GetInt64("notOK")
   126  	assert.Error(t, e2)
   127  	assert.Equal(t, int64(0), v2)
   128  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
   129  	v3, e3 := in.GetInt64("notInt64")
   130  	assert.Error(t, e3)
   131  	assert.Equal(t, int64(0), v3)
   132  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
   133  }
   134  
   135  func TestParamsGetFloat64(t *testing.T) {
   136  	for _, test := range []struct {
   137  		value     interface{}
   138  		result    float64
   139  		errString string
   140  	}{
   141  		{"123.1", 123.1, ""},
   142  		{"123x1", 0, "couldn't parse"},
   143  		{int(12), 12, ""},
   144  		{int64(13), 13, ""},
   145  		{float64(14), 14, ""},
   146  	} {
   147  		t.Run(fmt.Sprintf("%T=%v", test.value, test.value), func(t *testing.T) {
   148  			in := Params{
   149  				"key": test.value,
   150  			}
   151  			v1, e1 := in.GetFloat64("key")
   152  			if test.errString == "" {
   153  				require.NoError(t, e1)
   154  				assert.Equal(t, test.result, v1)
   155  			} else {
   156  				require.NotNil(t, e1)
   157  				require.Error(t, e1)
   158  				assert.Contains(t, e1.Error(), test.errString)
   159  				assert.Equal(t, float64(0), v1)
   160  			}
   161  		})
   162  	}
   163  	in := Params{
   164  		"notFloat64": []string{"a", "b"},
   165  	}
   166  	v2, e2 := in.GetFloat64("notOK")
   167  	assert.Error(t, e2)
   168  	assert.Equal(t, float64(0), v2)
   169  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
   170  	v3, e3 := in.GetFloat64("notFloat64")
   171  	assert.Error(t, e3)
   172  	assert.Equal(t, float64(0), v3)
   173  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
   174  }
   175  
   176  func TestParamsGetBool(t *testing.T) {
   177  	for _, test := range []struct {
   178  		value     interface{}
   179  		result    bool
   180  		errString string
   181  	}{
   182  		{true, true, ""},
   183  		{false, false, ""},
   184  		{"true", true, ""},
   185  		{"false", false, ""},
   186  		{"fasle", false, "couldn't parse"},
   187  		{int(12), true, ""},
   188  		{int(0), false, ""},
   189  		{int64(13), true, ""},
   190  		{int64(0), false, ""},
   191  		{float64(14), true, ""},
   192  		{float64(0), false, ""},
   193  	} {
   194  		t.Run(fmt.Sprintf("%T=%v", test.value, test.value), func(t *testing.T) {
   195  			in := Params{
   196  				"key": test.value,
   197  			}
   198  			v1, e1 := in.GetBool("key")
   199  			if test.errString == "" {
   200  				require.NoError(t, e1)
   201  				assert.Equal(t, test.result, v1)
   202  			} else {
   203  				require.NotNil(t, e1)
   204  				require.Error(t, e1)
   205  				assert.Contains(t, e1.Error(), test.errString)
   206  				assert.Equal(t, false, v1)
   207  			}
   208  		})
   209  	}
   210  	in := Params{
   211  		"notBool": []string{"a", "b"},
   212  	}
   213  	v2, e2 := Params{}.GetBool("notOK")
   214  	assert.Error(t, e2)
   215  	assert.Equal(t, false, v2)
   216  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
   217  	v3, e3 := in.GetBool("notBool")
   218  	assert.Error(t, e3)
   219  	assert.Equal(t, false, v3)
   220  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
   221  }
   222  
   223  func TestParamsGetStruct(t *testing.T) {
   224  	in := Params{
   225  		"struct": Params{
   226  			"String": "one",
   227  			"Float":  4.2,
   228  		},
   229  	}
   230  	var out struct {
   231  		String string
   232  		Float  float64
   233  	}
   234  	e1 := in.GetStruct("struct", &out)
   235  	assert.NoError(t, e1)
   236  	assert.Equal(t, "one", out.String)
   237  	assert.Equal(t, 4.2, out.Float)
   238  
   239  	e2 := in.GetStruct("notOK", &out)
   240  	assert.Error(t, e2)
   241  	assert.Equal(t, "one", out.String)
   242  	assert.Equal(t, 4.2, out.Float)
   243  	assert.Equal(t, ErrParamNotFound("notOK"), e2)
   244  
   245  	in["struct"] = "string"
   246  	e3 := in.GetStruct("struct", &out)
   247  	assert.Error(t, e3)
   248  	assert.Equal(t, "one", out.String)
   249  	assert.Equal(t, 4.2, out.Float)
   250  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
   251  }
   252  
   253  func TestParamsGetStructMissingOK(t *testing.T) {
   254  	in := Params{
   255  		"struct": Params{
   256  			"String": "one",
   257  			"Float":  4.2,
   258  		},
   259  	}
   260  	var out struct {
   261  		String string
   262  		Float  float64
   263  	}
   264  	e1 := in.GetStructMissingOK("struct", &out)
   265  	assert.NoError(t, e1)
   266  	assert.Equal(t, "one", out.String)
   267  	assert.Equal(t, 4.2, out.Float)
   268  
   269  	e2 := in.GetStructMissingOK("notOK", &out)
   270  	assert.NoError(t, e2)
   271  	assert.Equal(t, "one", out.String)
   272  	assert.Equal(t, 4.2, out.Float)
   273  
   274  	in["struct"] = "string"
   275  	e3 := in.GetStructMissingOK("struct", &out)
   276  	assert.Error(t, e3)
   277  	assert.Equal(t, "one", out.String)
   278  	assert.Equal(t, 4.2, out.Float)
   279  	assert.Equal(t, true, IsErrParamInvalid(e3), e3.Error())
   280  }