#!/bin/bash # These are basic happy path tests that run pricehist from the command line and # confirm that the results come out as expected. They help ensure that the main # endpoints for each source are still working. # Run this from the project root. export ALPHAVANTAGE_API_KEY="TEST_KEY_$RANDOM" cmd_prefix="poetry run" passed=0 failed=0 skipped=0 run_test(){ name=$1 cmd=$2 expected=$3 echo "TEST: $name" echo " Action: $cmd" echo -n " Result: " full_cmd="$cmd_prefix $cmd" actual=$($full_cmd 2>&1) if [[ "$actual" == "$expected" ]]; then passed=$((passed+1)) echo "passed, output as expected" else failed=$((failed+1)) echo "failed, output differs as follows..." echo diff <(echo "$expected") <(echo "$actual") fi echo } skip_test(){ name=$1 cmd=$2 echo "TEST: $name" echo " Action: $cmd" echo " Result: SKIPPED!" skipped=$((skipped+1)) echo } report(){ total=$((passed+failed)) if [[ "$skipped" -eq "0" ]]; then skipped_str="none" else skipped_str="$skipped" fi if [[ "$failed" -eq "0" ]]; then echo "SUMMARY: $passed tests passed, none failed, $skipped_str skipped" else echo "SUMMARY: $failed/$total tests failed, $skipped_str skipped" exit 1 fi } name="Alpha Vantage stocks" cmd="pricehist fetch alphavantage TSLA -s 2021-01-04 -e 2021-01-08" read -r -d '' expected <