github.com/CiscoM31/godata@v1.0.10/compute_parser_test.go (about)

     1  package godata
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestParseCompute(t *testing.T) {
    10  	err := DefineCustomFunctions([]CustomFunctionInput{
    11  		{Name: "zeroArgFunc", NumParams: []int{0}},
    12  		{Name: "oneArgFunc", NumParams: []int{1}},
    13  		{Name: "twoArgFunc", NumParams: []int{2}},
    14  	})
    15  
    16  	if err != nil {
    17  		t.Error(err)
    18  		t.FailNow()
    19  	}
    20  
    21  	type testcase struct {
    22  		computeStrings []string
    23  		shouldPass     bool
    24  	}
    25  
    26  	testCases := []testcase{
    27  		{[]string{"oldField as newField"}, true},
    28  		{[]string{"1 as newField"}, true},
    29  		{[]string{"one add 2 as newField"}, true},
    30  		{[]string{"one add two as extra/newField"}, true},
    31  		{[]string{"zeroArgFunc() as newField"}, true},
    32  		{[]string{"oneArgFunc(one) as newField"}, true},
    33  		{[]string{"twoArgFunc(one, two) as newField"}, true},
    34  		{[]string{"twoArgFunc(one, two) as newField", "tolower(three) as  newFieldTwo"}, true},
    35  
    36  		{[]string{"case(false:0) as newField"}, true},
    37  		{[]string{"case(false:0,true:1) as newField"}, true},
    38  		{[]string{"case(prop eq 'one':1,prop eq 'two':2) as newField"}, true},
    39  		{[]string{"case(tolower(one) eq one:'lower') as newField"}, true},
    40  		{[]string{"case(contains(haystack,'needle'):1,true:1) as newField"}, true},
    41  		{[]string{"case(tolower(one) eq one:tolower(one)) as newField"}, true},
    42  		{[]string{"case(true:2 mul 3) as newField"}, true},
    43  		{[]string{"case(false:1,false:2,false:3,false:4,false:5,false:6,false:7,false:8,false:9,false:10) as newField"}, true}, // max of 10 cases
    44  
    45  		// negative cases
    46  		{[]string{"one add two as newField2"}, false},
    47  		{[]string{"one add two newField2"}, false},
    48  		{[]string{""}, false},
    49  		{[]string{"as"}, false},
    50  		{[]string{"as newField"}, false},
    51  		{[]string{"zeroArgFunc() as "}, false},
    52  
    53  		{[]string{"case as newField"}, false},
    54  		{[]string{"case() as newField"}, false},
    55  		{[]string{"case(false:,true:1) as newField"}, false},
    56  		{[]string{"case(false,true:1) as newField"}, false},
    57  		{[]string{"case(false,true:1) as newField"}, false},
    58  		{[]string{"case(1:1,true:1) as newField"}, false},
    59  		{[]string{"case(1:1,true:1) as newField"}, false},
    60  		{[]string{"case(false:1,false:2,false:3,false:4,false:5,false:6,false:7,false:8,false:9,false:10,false:11) as newField"}, false}, // max of 10 cases
    61  	}
    62  
    63  	for i, v := range testCases {
    64  
    65  		var result *GoDataComputeQuery
    66  		result, err = ParseComputeString(context.Background(), strings.Join(v.computeStrings, ","))
    67  		if v.shouldPass {
    68  			if err != nil {
    69  				t.Errorf("testcase %d: %v", i, err)
    70  				t.FailNow()
    71  			}
    72  			if result == nil {
    73  				t.Errorf("testcase %d: nil result", i)
    74  				t.FailNow()
    75  			}
    76  
    77  			if len(result.ComputeItems) != len(v.computeStrings) {
    78  				t.Errorf("testcase %d: wrong number of result items", i)
    79  				t.FailNow()
    80  			}
    81  		} else {
    82  			if err == nil {
    83  				t.Errorf("testcase %d: expected error", i)
    84  				t.FailNow()
    85  			}
    86  		}
    87  
    88  	}
    89  }