Skip to content

Commit 54892cc

Browse files
committed
Add error handling and js based path comparision for path_eq
1 parent a7a60cc commit 54892cc

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

api-test.sh

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,13 @@ test_factory() {
200200
tput cuf 2
201201
echo "${UNDERLINE}b. Checking condition for body${RESET}"
202202
test_runner $TEST_CASE "body" "$RESPONSE_BODY"
203-
204203
echo ""
205204
echo ""
206205
done
207206
}
208207

209208
test_runner() {
210-
for test in ""contains eq hasKey[]""; do
209+
for test in ""contains eq path_eq hasKey[]""; do
211210
local TEST_SCENARIO=$(jq -r ".testCases.$1.expect.$2.$test? | select(. !=null)" $FILE)
212211
if [[ -z $TEST_SCENARIO ]]; then
213212
continue
@@ -219,6 +218,9 @@ test_runner() {
219218
elif [[ $test == "eq" ]]; then
220219
echo "Checking equality comparision${RESET}"
221220
check_eq "$TEST_SCENARIO" "$3"
221+
elif [[ $test == "path_eq" ]]; then
222+
echo "Checking path equality comparision${RESET}"
223+
path_eq "$TEST_SCENARIO" "$3"
222224
else
223225
echo "Checking has key comparision${RESET}"
224226
has_key "$TEST_SCENARIO" "$3"
@@ -271,7 +273,7 @@ has_key() {
271273

272274
check_eq() {
273275
tput cuf 6
274-
local type=$(jq -r --argjson a "$1" -n '$a|type')
276+
local type=$(jq -r --argjson a "$1" -n '$a|type')
275277
local check
276278
if [[ $type == "object" || $type == "array" ]]; then
277279
check=$(jq --argjson a "$1" --argjson b "$2" -n 'def post_recurse(f): def r: (f | select(. != null) | r), .; r; def post_recurse: post_recurse(.[]?); ($a | (post_recurse | arrays) |= sort) as $a | ($b | (post_recurse | arrays) |= sort) as $b | $a == $b')
@@ -290,6 +292,27 @@ check_eq() {
290292
fi
291293
}
292294

295+
path_eq() {
296+
local keys=$(jq -r --argjson a "$1" -n '$a | keys[]')
297+
if [[ -z "$keys" ]]; then
298+
return
299+
fi
300+
for key in "$keys"; do
301+
tput cuf 6
302+
local value=$(jq -c -r --argjson a "$1" -n "\$a | .\"$key\"")
303+
echo "When path is '$key' and value is $value"
304+
local compare_value=$(jq -r --argjson a "$2" -n "\$a | try .$key catch \"OBJECT_FETCH_ERROR_JQ_API_TEST\"" 2>/dev/null)
305+
if [[ -z "$compare_value" ]]; then
306+
tput cuf 8
307+
echo "${RED}${BOLD}Check Failed${RESET}"
308+
tput cuf 2
309+
echo "INVALID PATH SYNTAX: ${RED}data[0]target_id${RESET}"
310+
return
311+
fi
312+
tput cuf 2
313+
check_eq "$value" "$compare_value"
314+
done
315+
}
293316

294317
run() {
295318
for arg in "$@"; do

0 commit comments

Comments
 (0)