From 74d9c211c9f25c68b14ccb55301828ca33c5d19e Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Wed, 11 Aug 2021 15:00:58 +0200 Subject: [PATCH] Add some live tests. --- .gitlab-ci.yml | 14 ++++-- Makefile | 6 +-- pyproject.toml | 4 +- tests/live.sh | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 9 deletions(-) create mode 100755 tests/live.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8c54d1e..29ec768 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,11 +14,19 @@ before_script: - pip install poetry - poetry install +pre-commit: + script: + - make pre-commit + test: script: - - poetry run isort src tests --check - - poetry run black src tests --check - - poetry run flake8 src tests - poetry run pytest + +test-live: + script: + - tests/live.sh + +coverage: + script: - poetry run coverage run --source=pricehist -m pytest - poetry run coverage report diff --git a/Makefile b/Makefile index e219af0..1a26a11 100644 --- a/Makefile +++ b/Makefile @@ -12,12 +12,12 @@ lint: ## Lint source code poetry run flake8 .PHONY: test -test: ## Run non-live tests - poetry run pytest -m "not live" --color=yes +test: ## Run tests + poetry run pytest --color=yes .PHONY: test-live test-live: ## Run live tests - poetry run pytest -m live --color=yes + tests/live.sh .PHONY: coverage coverage: ## Generate and open coverage report diff --git a/pyproject.toml b/pyproject.toml index 193c1fb..52a3341 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,4 @@ profile = "black" multi_line_output = 3 [tool.pytest.ini_options] -markers = [ - "live: makes a live request to a source" -] +markers = [] diff --git a/tests/live.sh b/tests/live.sh new file mode 100755 index 0000000..268111e --- /dev/null +++ b/tests/live.sh @@ -0,0 +1,131 @@ +#!/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 + +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 +} + +report(){ + total=$((passed+failed)) + if [[ "$failed" -eq "0" ]]; then + echo "SUMMARY: $passed tests passed, none failed" + else + echo "SUMMARY: $failed/$total tests failed" + exit 1 + fi +} + +name="Alpha Vantage stocks" +cmd="pricehist fetch alphavantage TSLA -s 2021-01-04 -e 2021-01-08" +read -r -d '' expected <