Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

symbols: fix test and check error before passing to jq #7

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/binance.sh
Original file line number Diff line number Diff line change
@@ -540,14 +540,17 @@ API_URLS=(
# 0 on success, non-zero on error.
#######################################
symbols() {
local product base_url
local product base_url response

product=${1:?missing required <product> argument}

base_url=${API_URLS[$product]:?API URL is not set}

# todo: check response before passing to jq
http.get "${base_url}/exchangeInfo" | jq -r '.symbols[].symbol'
response=$(http.get "${base_url}/exchangeInfo") || {
fail "Error: $response"?
}

echo "$response" | jq -r '.symbols[].symbol'
}

#######################################
@@ -571,14 +574,16 @@ klines() {
local interval=${3:?missing required <interval> argument}
local start_time=${4:?missing required <start_time> argument}
local end_time=${5:?missing required <end_time> argument}

local base_url=${API_URLS[$product]:?API URL is not set}
local query="symbol=${symbol}&interval=${interval}&limit=1000"

local start_time_ms end_time_ms url response klines="[]"

if ! is_date "$start_time" || ! is_date "$end_time"; then
fail "<start_time> and <end_time> must be valid date"
fi

start_time_ms=$(date_to_ms "$start_time")
end_time_ms=$(date_to_ms "$end_time")

@@ -591,6 +596,7 @@ klines() {
fi

klines=$(echo "$klines" "$response" | jq -s 'add')
# todo: add max_iterations and check start_time to avoid infinite loop
start_time_ms=$(echo "$response" | jq -r '.[-1][6]') # get last close time
url=$(urlparam "$url" startTime "$start_time_ms")
done
9 changes: 6 additions & 3 deletions src/symbols.sh
Original file line number Diff line number Diff line change
@@ -12,12 +12,15 @@
# 0 on success, non-zero on error.
#######################################
symbols() {
local product base_url
local product base_url response

product=${1:?missing required <product> argument}

base_url=${API_URLS[$product]:?API URL is not set}

# todo: check response before passing to jq
http.get "${base_url}/exchangeInfo" | jq -r '.symbols[].symbol'
response=$(http.get "${base_url}/exchangeInfo") || {
fail "Error: $response"?
}

echo "$response" | jq -r '.symbols[].symbol'
}
26 changes: 26 additions & 0 deletions tests/symbols.bats
Original file line number Diff line number Diff line change
@@ -6,6 +6,25 @@ setup() {
load "../lib/utils.sh"
load "../src/config.sh"
load "../src/symbols.sh"

# Mock http.get to return the output of the example command
http_get_mock() {
local url=$1
case "$url" in
*//api*)
echo '{"symbols":[{"symbol":"BTCUSDT"},{"symbol":"ETHUSDT"}]}'
;;
*)
echo '{"error":"unexpected"}'
return 1
;;
esac
}

# Override http.get function
http.get() {
http_get_mock "$@"
}
}

# Test: symbols should return a list of symbols for a valid product
@@ -27,3 +46,10 @@ setup() {
run symbols invalid_product
assert_failure
}

# Test: symbols should fail for an invalid response
@test "symbols fails for an invalid response" {
run symbols um
assert_failure
assert_line --partial "Error:"
}