github.com/devtron-labs/ci-runner@v0.0.0-20240518055909-b2672f3349d7/helper/ExpressionEvaluater_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func Test_evaluateExpression(t *testing.T) {
     8  	type args struct {
     9  		condition *ConditionObject
    10  		variables []*VariableObject
    11  	}
    12  	tests := []struct {
    13  		name       string
    14  		args       args
    15  		wantStatus bool
    16  		wantErr    bool
    17  	}{
    18  		{name: "Eval_false",
    19  			args: args{condition: &ConditionObject{
    20  				ConditionOnVariable: "age",
    21  				ConditionalOperator: ">",
    22  				ConditionalValue:    "10",
    23  			}, variables: []*VariableObject{{Name: "age", Value: "8", Format: NUMBER}}},
    24  			wantErr:    false,
    25  			wantStatus: false},
    26  		{name: "eval_true",
    27  			args: args{condition: &ConditionObject{
    28  				//ConditionType:       "",
    29  				ConditionOnVariable: "age",
    30  				ConditionalOperator: ">",
    31  				ConditionalValue:    "10",
    32  			}, variables: []*VariableObject{{Name: "age", Value: "12", Format: NUMBER}}},
    33  			wantErr:    false,
    34  			wantStatus: true},
    35  		{name: "Eval_true_date",
    36  			args: args{condition: &ConditionObject{
    37  				ConditionOnVariable: "today",
    38  				ConditionalOperator: ">",
    39  				ConditionalValue:    "Tue Apr 10 13:55:21 IST 2022",
    40  			}, variables: []*VariableObject{{Name: "today", Value: "Tue Apr 12 13:55:21 IST 2022", Format: DATE}}},
    41  			wantErr:    false,
    42  			wantStatus: true},
    43  		{name: "Eval_false_date",
    44  			args: args{condition: &ConditionObject{
    45  				//ConditionType:       "",
    46  				ConditionOnVariable: "today",
    47  				ConditionalOperator: "<",
    48  				ConditionalValue:    "'Tue Apr 10 13:55:21 IST 2022'",
    49  			}, variables: []*VariableObject{{Name: "today", Value: "'Tue Apr 8 13:55:21 IST 2022'", Format: DATE}}},
    50  			wantErr:    false,
    51  			wantStatus: false},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			gotStatus, err := evaluateExpression(tt.args.condition, tt.args.variables)
    56  			if (err != nil) != tt.wantErr {
    57  				t.Errorf("evaluateExpression() error = %v, wantErr %v", err, tt.wantErr)
    58  				return
    59  			}
    60  			if gotStatus != tt.wantStatus {
    61  				t.Errorf("evaluateExpression() gotStatus = %v, want %v", gotStatus, tt.wantStatus)
    62  			}
    63  		})
    64  	}
    65  }