From 0db92f46daf58a6fd6b4deca6dd6f1c06b6725db Mon Sep 17 00:00:00 2001 From: TwiN Date: Thu, 19 Jan 2023 01:37:21 -0500 Subject: [PATCH] test: Add several tests for numerical conditions --- core/condition.go | 7 +++++++ core/condition_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/core/condition.go b/core/condition.go index 7ef26c7f..35e857f7 100644 --- a/core/condition.go +++ b/core/condition.go @@ -11,6 +11,7 @@ import ( "github.com/TwiN/gatus/v5/pattern" ) +// Placeholders const ( // StatusPlaceholder is a placeholder for a HTTP status. // @@ -49,7 +50,10 @@ const ( // DomainExpirationPlaceholder is a placeholder for the duration before the domain expires, in milliseconds. DomainExpirationPlaceholder = "[DOMAIN_EXPIRATION]" +) +// Functions +const ( // LengthFunctionPrefix is the prefix for the length function // // Usage: len([BODY].articles) == 10, len([BODY].name) > 5 @@ -72,7 +76,10 @@ const ( // FunctionSuffix is the suffix for all functions FunctionSuffix = ")" +) +// Other constants +const ( // InvalidConditionElementSuffix is the suffix that will be appended to an invalid condition InvalidConditionElementSuffix = "(INVALID)" diff --git a/core/condition_test.go b/core/condition_test.go index 005316b5..a321c138 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -182,6 +182,34 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: true, ExpectedOutput: "[BODY] == test", }, + { + Name: "body-numerical-equal", + Condition: Condition("[BODY] == 123"), + Result: &Result{body: []byte("123")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY] == 123", + }, + { + Name: "body-numerical-less-than", + Condition: Condition("[BODY] < 124"), + Result: &Result{body: []byte("123")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY] < 124", + }, + { + Name: "body-numerical-greater-than", + Condition: Condition("[BODY] > 122"), + Result: &Result{body: []byte("123")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY] > 122", + }, + { + Name: "body-numerical-greater-than-failure", + Condition: Condition("[BODY] > 123"), + Result: &Result{body: []byte("100")}, + ExpectedSuccess: false, + ExpectedOutput: "[BODY] (100) > 123", + }, { Name: "body-jsonpath", Condition: Condition("[BODY].status == UP"), @@ -594,6 +622,7 @@ func TestCondition_evaluate(t *testing.T) { } for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) { + t.Parallel() scenario.Condition.evaluate(scenario.Result, scenario.DontResolveFailedConditions) if scenario.Result.ConditionResults[0].Success != scenario.ExpectedSuccess { t.Errorf("Condition '%s' should have been success=%v", scenario.Condition, scenario.ExpectedSuccess)