Skip to content

Commit

Permalink
Made TestParsingChoiceDagWithBoolExpr pass
Browse files Browse the repository at this point in the history
  • Loading branch information
redjack96 committed Aug 31, 2024
1 parent f91a06f commit 5f85c34
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/fc/asl.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func buildTestExpr(t *asl.TestExpression) (Condition, error) {
condition = NewNot(NewEqCondition(param, NewValue(nil)))
break
case "IsNumeric":
valBool, err := function.Bool{}.Convert(val)
valBool, err := function.Bool{}.Convert(val.value)
if err != nil {
return NewConstCondition(false), fmt.Errorf("IsNumeric requires a boolean constant to be evaluated with, but it was given %v", val)
}
Expand Down
53 changes: 34 additions & 19 deletions internal/fc/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const (
Or CondEnum = "Or"
Not CondEnum = "Not"
Const CondEnum = "Const"
Eq CondEnum = "Eq"
Diff CondEnum = "Diff"
Greater CondEnum = "Greater"
Smaller CondEnum = "Smaller"
Eq CondEnum = "Eq" // also works for strings
Diff CondEnum = "Diff" // also works for strings
Greater CondEnum = "Greater" // TODO: make it work for strings
Smaller CondEnum = "Smaller" // TODO: make it work for strings
Empty CondEnum = "Empty" // for collections
IsNumeric CondEnum = "IsNumeric" // for collections
)
Expand Down Expand Up @@ -133,9 +133,10 @@ func (c Condition) findInputs(input map[string]interface{}) ([]interface{}, erro
}
value, found := input[opStr]
if !found {
ops = append(ops, false)
ops = append(ops, nil)
} else {
ops = append(ops, value)
}
ops = append(ops, value)
}
}
return ops, nil
Expand Down Expand Up @@ -218,11 +219,16 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
return false, err
}
float1, err1 := f.Convert(ops[0])
if err1 != nil {
fmt.Printf("condition Greater: first operand '%v' cannot be converted to float64\n", c.Op[0])
return false, nil
}
float2, err2 := f.Convert(ops[1])
if err1 != nil || err2 != nil {
return false, fmt.Errorf("not all operands %v can be converted to float64", c.Op)
if err2 != nil {
fmt.Printf("condition Greater: second operand '%v' cannot be converted to float64\n", c.Op[1])
return false, nil
}
return float1 > float2, nil
return float1 > float2, nil // TODO: make it work for strings (a string is greater than another string it comes alphabetically after than the other)
case Smaller:
if len(c.Op) != 2 {
return false, fmt.Errorf("you need exactly two numbers to check which is greater")
Expand All @@ -233,11 +239,16 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
return false, err
}
float1, err1 := f.Convert(ops[0])
if err1 != nil {
fmt.Printf("condition Smaller: first operand '%v' cannot be converted to float64\n", c.Op[0])
return false, nil
}
float2, err2 := f.Convert(ops[1])
if err1 != nil || err2 != nil {
return false, fmt.Errorf("not all operands %v can be converted to float64", c.Op)
if err2 != nil {
fmt.Printf("condition Smaller: second operand '%v' cannot be converted to float64\n", c.Op[1])
return false, nil
}
return float1 < float2, nil
return float1 < float2, nil // TODO: make it work for strings (a string is smaller than another string it comes alphabetically before than the other)
case Empty:
ops, err := c.findInputs(input)
if err != nil {
Expand All @@ -249,20 +260,24 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
}
return len(slice) == 0, nil
case IsNumeric:
isNumeric := func(s string) bool {
_, err := strconv.ParseFloat(s, 64)
isNumeric := func(s interface{}) bool {
_, err := function.Int{}.Convert(s)
if err == nil {
return true
}
numericStringMaybe, ok := s.(string)
if !ok {
return false
}
_, err = strconv.ParseFloat(numericStringMaybe, 64)
return err == nil
}

ops, err := c.findInputs(input)
if err != nil {
return false, err
}
strOp, ok := ops[0].(string)
if !ok {
return false, fmt.Errorf("first operand must be a string")
}
return isNumeric(strOp), nil
return isNumeric(ops[0]), nil
default:
return false, fmt.Errorf("invalid operation condition %s", c.Type)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/fc/function_composition.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cer *CompositionExecutionReport) GetSingleResult() (string, error) {
return fmt.Sprintf("%v", value), nil
}
}
return "", fmt.Errorf("there is more then one result: %d", len(cer.Result))
return "", fmt.Errorf("there is not exactly one result: there are %d result(s)", len(cer.Result))
}

func (cer *CompositionExecutionReport) GetIntSingleResult() (int, error) {
Expand All @@ -55,7 +55,7 @@ func (cer *CompositionExecutionReport) GetIntSingleResult() (int, error) {
return valueInt, nil
}
}
return 0, fmt.Errorf("there is more then one result: %d", len(cer.Result))
return 0, fmt.Errorf("there is not exactly one result: there are %d result(s)", len(cer.Result))
}

func (cer *CompositionExecutionReport) GetAllResults() string {
Expand Down
10 changes: 10 additions & 0 deletions internal/function/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ func (i Int) Convert(val interface{}) (int, error) {
return int(t), nil
case int64:
return int(t), nil
case uint:
return int(t), nil
case uint8:
return int(t), nil
case uint16:
return int(t), nil
case uint32:
return int(t), nil
case uint64:
return int(t), nil
case string:
val, err := strconv.Atoi(val.(string))
if err == nil {
Expand Down
12 changes: 12 additions & 0 deletions internal/test/aslparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestParsingChoiceDagWithBoolExpr(t *testing.T) {
utils.AssertNilMsg(t, err, "unable to parse json")

// 1st branch (type != "Private")
fmt.Println("1st branch: (type != 'Private') -> inc + inc")
params := make(map[string]interface{})
params["type"] = "Public"
params["value"] = 1
Expand All @@ -253,6 +254,7 @@ func TestParsingChoiceDagWithBoolExpr(t *testing.T) {
fmt.Println("Result: ", output)

// 2nd branch (type == "Private", value is present, value is numeric, value >= 20, value < 30)
fmt.Println("2nd branch: (type == \"Private\", value is present, value is numeric, value >= 20, value < 30) -> double + inc")
params2 := make(map[string]interface{})
params2["type"] = "Private"
params2["value"] = 20
Expand All @@ -265,6 +267,16 @@ func TestParsingChoiceDagWithBoolExpr(t *testing.T) {
utils.AssertNilMsg(t, err, "failed to get int single result")
utils.AssertEquals(t, 41, output2)
fmt.Println("Result: ", output2)

// 2nd branch (type == "Private", value is present, value is numeric, value >= 20, value < 30)
fmt.Println("default branch (we specify nothing instead of a number)")
params3 := make(map[string]interface{})
params3["type"] = "Private"
request3 := fc.NewCompositionRequest(shortuuid.New(), comp, params3)
resultMap3, err2 := comp.Invoke(request3)
utils.AssertNil(t, err2)
fmt.Printf("Composition Execution Report: %s\n", resultMap3.String())
// no results to check
}

func TestParsingDagWithMalformedJson(t *testing.T) {}
Expand Down
3 changes: 2 additions & 1 deletion internal/test/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func TestIsNumeric(t *testing.T) {
isNumeric = fc.NewIsNumericParamCondition(fc.NewParam("foo"))
testMap = make(map[string]interface{})
ok, err = isNumeric.Test(testMap)
utils.AssertNonNil(t, err)
// foo is not specified, so ok should be false. No errors should be expected
utils.AssertNil(t, err)
utils.AssertFalse(t, ok)
}

0 comments on commit 5f85c34

Please sign in to comment.