Skip to content

Commit

Permalink
Handle null numeric assert
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <tsfarr@amazon.com>
  • Loading branch information
Xtansia committed Sep 20, 2024
1 parent 46f8eb5 commit fa47729
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions tests/Tests.YamlRunner/OperationExecutor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -308,40 +308,48 @@ type OperationExecutor(client:IOpenSearchLowLevelClient) =
let found, expected = stashes.TryGetValue id
match found with
| true -> Token(OperationExecutor.ToJToken expected)
| false -> Fail(Fail.Create op "%A not stashed at this point" id)
| false -> Fail(Fail.Create op $"%A{id} not stashed at this point")

let expectedValue (value:JToken) =
let expectedValue (value:JToken): Nullable<float> option =
match value with
| :? JValue as v -> Some(Convert.ChangeType(v.Value, typeof<double>) :?> double)
| :? JArray as a -> Some <| Convert.ToDouble(a.Count)
| :? JObject as o -> Some <| Convert.ToDouble(o.Properties().Count())
| :? JValue as v ->
if v.Value <> null then
Convert.ChangeType(v.Value, typeof<float>) :?> float
|> Nullable
|> Some
else
Some(Nullable())
| :? JArray as a -> Convert.ToDouble(a.Count) |> Nullable |> Some
| :? JObject as o -> Convert.ToDouble(o.Properties().Count()) |> Nullable |> Some
| _ -> None

let actual =
match path with
| "$body" ->
let dictOrArray = stashes.Response().Dictionary.Count
Some <| Convert.ToDouble(dictOrArray)
Convert.ToDouble(dictOrArray)
|> Nullable
|> Some
| _ ->
let a = OperationExecutor.ToJToken <| (stashes.GetResponseValue progress path :> Object)
expectedValue a

let numMatch a e s c =
let e = expectedValue e
match e with
| None -> Failed <| Fail.Create op "Can not get numeric value from expected %O" e
| None -> Failed <| Fail.Create op $"Can not get numeric value from expected {e}"
| Some e when c a e -> Succeeded op
| Some e -> Failed <| Fail.Create op "Expected %f %s %f " e s a
| Some e -> Failed <| Fail.Create op $"Expected {e} %s{s} {a}"

match (assertion, actual, expected) with
| (_, _, Fail f) -> Failed <| f
| (_, None, _) -> Failed <| Fail.Create op "Can not get numeric value from actual %O" actual
| (_, None, _) -> Failed <| Fail.Create op $"Can not get numeric value from actual {actual}"
| (Equal, Some a, Token e) -> OperationExecutor.JTokenDeepEquals op e a
| (Length, Some a, Token e) -> numMatch a e "=" <| (fun a e -> a = e)
| (LowerThan, Some a, Token e) -> numMatch a e "<" <| (fun a e -> a < e)
| (GreaterThan, Some a, Token e) -> numMatch a e ">" <| (fun a e -> a > e)
| (GreaterThanOrEqual, Some a, Token e) -> numMatch a e ">=" <| (fun a e -> a >= e)
| (LowerThanOrEqual, Some a, Token e) -> numMatch a e "<=" <| (fun a e -> a <= e)
| (LowerThan, Some a, Token e) -> numMatch a e "<" <| (fun a e -> a.Value < e.Value)
| (GreaterThan, Some a, Token e) -> numMatch a e ">" <| (fun a e -> a.Value > e.Value)
| (GreaterThanOrEqual, Some a, Token e) -> numMatch a e ">=" <| (fun a e -> a.Value >= e.Value)
| (LowerThanOrEqual, Some a, Token e) -> numMatch a e "<=" <| (fun a e -> a.Value <= e.Value)

let asserts =
m
Expand Down

0 comments on commit fa47729

Please sign in to comment.