github.com/viant/toolbox@v0.34.5/data/udf/util_test.go (about)

     1  package udf
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"github.com/viant/toolbox"
     6  	"github.com/viant/toolbox/data"
     7  	"testing"
     8  )
     9  
    10  func Test_Length(t *testing.T) {
    11  	{
    12  		value, err := Length(4.3, nil)
    13  		assert.Nil(t, err)
    14  		assert.Equal(t, 0, value)
    15  	}
    16  	{
    17  		value, err := Length("abcd", nil)
    18  		assert.Nil(t, err)
    19  		assert.Equal(t, 4, value)
    20  	}
    21  	{
    22  		value, err := Length(map[int]int{
    23  			2: 3,
    24  			1: 1,
    25  			6: 3,
    26  		}, nil)
    27  		assert.Nil(t, err)
    28  		assert.Equal(t, 3, value)
    29  	}
    30  	{
    31  		value, err := Length([]int{1, 2, 3}, nil)
    32  		assert.Nil(t, err)
    33  		assert.Equal(t, 3, value)
    34  	}
    35  }
    36  
    37  func Test_Keys(t *testing.T) {
    38  
    39  	{
    40  		var keys, err = Keys(map[string]interface{}{}, nil)
    41  		assert.Nil(t, err)
    42  		assert.NotNil(t, keys)
    43  	}
    44  	{
    45  		var keys, err = Keys("{\"abc\":1}", nil)
    46  		assert.Nil(t, err)
    47  		assert.EqualValues(t, []interface{}{"abc"}, keys)
    48  	}
    49  }
    50  
    51  func Test_Values(t *testing.T) {
    52  
    53  	{
    54  		var keys, err = Values(map[string]interface{}{}, nil)
    55  		assert.Nil(t, err)
    56  		assert.NotNil(t, keys)
    57  	}
    58  	{
    59  		var keys, err = Values("{\"abc\":1}", nil)
    60  		assert.Nil(t, err)
    61  		assert.EqualValues(t, []interface{}{1}, keys)
    62  	}
    63  }
    64  
    65  func Test_Join(t *testing.T) {
    66  	{
    67  		var joined, err = Join([]interface{}{
    68  			[]interface{}{1, 2, 3},
    69  			",",
    70  		}, nil)
    71  		assert.Nil(t, err)
    72  		assert.NotNil(t, joined)
    73  		assert.EqualValues(t, "1,2,3", joined)
    74  	}
    75  }
    76  
    77  func Test_Split(t *testing.T) {
    78  	{
    79  		var joined, err = Split([]interface{}{
    80  			"abc , zc",
    81  			",",
    82  		}, nil)
    83  		assert.Nil(t, err)
    84  		assert.NotNil(t, joined)
    85  		assert.EqualValues(t, []string{"abc", "zc"}, joined)
    86  	}
    87  }
    88  
    89  func Test_IndexOf(t *testing.T) {
    90  	{
    91  		index, _ := IndexOf([]interface{}{"this is test", "is"}, nil)
    92  		assert.EqualValues(t, 2, index)
    93  	}
    94  	{
    95  		index, _ := IndexOf([]interface{}{[]string{"this", "is", "test"}, "is"}, nil)
    96  		assert.EqualValues(t, 1, index)
    97  	}
    98  }
    99  
   100  func Test_Base64Decode(t *testing.T) {
   101  	decoded, _ := Base64DecodeText("IkhlbGxvIFdvcmxkIg==", nil)
   102  	assert.EqualValues(t, `"Hello World"`, decoded)
   103  
   104  }
   105  
   106  func TestTrimSpace(t *testing.T) {
   107  	trimmed, _ := TrimSpace(" erer ", nil)
   108  	assert.EqualValues(t, `erer`, trimmed)
   109  
   110  }
   111  
   112  func TestSum(t *testing.T) {
   113  	{ //sum slice keys
   114  		var aMap = data.NewMap()
   115  		var collection = data.NewCollection()
   116  		collection.Push(map[string]interface{}{
   117  			"amount": 2,
   118  		})
   119  		collection.Push(map[string]interface{}{
   120  			"amount": 12,
   121  		})
   122  		aMap.SetValue("node1.obj", collection)
   123  		total, err := Sum("node1/obj/*/amount", aMap)
   124  		assert.Nil(t, err)
   125  		assert.Equal(t, 14, total)
   126  	}
   127  	{ //sum map keys
   128  		var aMap = data.NewMap()
   129  		aMap.SetValue("node1.obj.k1.amount", 1)
   130  		aMap.SetValue("node1.obj.k2.amount", 2)
   131  		aMap.SetValue("node1.obj.k3.amount", 3)
   132  		total, err := Sum("node1/obj/*/amount", aMap)
   133  		assert.Nil(t, err)
   134  		assert.Equal(t, 6, total)
   135  	}
   136  }
   137  
   138  func TestCount(t *testing.T) {
   139  	{ //sum slice keys
   140  		var aMap = data.NewMap()
   141  		var collection = data.NewCollection()
   142  		collection.Push(map[string]interface{}{
   143  			"amount": 2,
   144  		})
   145  		collection.Push(map[string]interface{}{
   146  			"amount": 12,
   147  		})
   148  		aMap.SetValue("node1.obj", collection)
   149  		total, err := Count("node1/obj/*/amount", aMap)
   150  		assert.Nil(t, err)
   151  		assert.Equal(t, 2, total)
   152  	}
   153  	{ //sum map keys
   154  		var aMap = data.NewMap()
   155  		aMap.SetValue("node1.obj.k1.amount", 1)
   156  		aMap.SetValue("node1.obj.k2.amount", 2)
   157  		aMap.SetValue("node1.obj.k3.amount", 3)
   158  		total, err := Count("node1/obj/*/amount", aMap)
   159  		assert.Nil(t, err)
   160  		assert.Equal(t, 3, total)
   161  	}
   162  }
   163  
   164  func TestSelect(t *testing.T) {
   165  	{ //sum slice keys
   166  		var aMap = data.NewMap()
   167  		var collection = data.NewCollection()
   168  		collection.Push(map[string]interface{}{
   169  			"amount": 2,
   170  			"id":     2,
   171  			"name":   "p1",
   172  			"vendor": "v1",
   173  		})
   174  		collection.Push(map[string]interface{}{
   175  			"amount": 12,
   176  			"id":     3,
   177  			"name":   "p2",
   178  			"vendor": "v2",
   179  		})
   180  		aMap.SetValue("node1.obj", collection)
   181  
   182  		records, err := Select([]interface{}{"node1/obj/*", "id", "name:product"}, aMap)
   183  		assert.Nil(t, err)
   184  		assert.Equal(t, []interface{}{
   185  			map[string]interface{}{
   186  				"id":      2,
   187  				"product": "p1",
   188  			},
   189  			map[string]interface{}{
   190  				"id":      3,
   191  				"product": "p2",
   192  			},
   193  		}, records)
   194  
   195  	}
   196  
   197  }
   198  
   199  func TestRand(t *testing.T) {
   200  	{
   201  		randValue, err := Rand(nil, nil)
   202  		assert.Nil(t, err)
   203  		floatValue, err := toolbox.ToFloat(randValue)
   204  		assert.Nil(t, err)
   205  		assert.True(t, toolbox.IsFloat(randValue) && floatValue >= 0.0 && floatValue < 1.0)
   206  	}
   207  	{
   208  		randValue, err := Rand([]interface{}{2, 15}, nil)
   209  		assert.Nil(t, err)
   210  		intValue, err := toolbox.ToInt(randValue)
   211  		assert.Nil(t, err)
   212  		assert.True(t, toolbox.IsInt(randValue) && intValue >= 2 && intValue < 15)
   213  	}
   214  }
   215  
   216  func TestConcat(t *testing.T) {
   217  	{
   218  		result, err := Concat([]interface{}{"a", "b", "c"}, nil)
   219  		assert.Nil(t, err)
   220  		assert.EqualValues(t, "abc", result)
   221  	}
   222  
   223  	{
   224  		result, err := Concat([]interface{}{[]interface{}{"a", "b"}, "c"}, nil)
   225  		assert.Nil(t, err)
   226  		assert.EqualValues(t, []interface{}{"a", "b", "c"}, result)
   227  	}
   228  
   229  }
   230  
   231  func TestMerge(t *testing.T) {
   232  	{
   233  		result, err := Merge([]interface{}{map[string]interface{}{
   234  			"k1": 1,
   235  		},
   236  			map[string]interface{}{
   237  				"k2": 2,
   238  			},
   239  		}, nil)
   240  		assert.Nil(t, err)
   241  		assert.EqualValues(t, map[string]interface{}{
   242  			"k1": 1,
   243  			"k2": 2,
   244  		}, result)
   245  	}
   246  }