From fb109ced0696f0f175931b69e81436ed06ca9b96 Mon Sep 17 00:00:00 2001 From: SamR1 Date: Sat, 13 Jan 2018 13:00:05 +0100 Subject: [PATCH] docker-compose-ci.yml: adding api tests --- .travis.yml | 3 +-- Makefile | 2 +- Makefile.config | 12 ++++++------ mpwo_api/server.py | 17 ++++++++++++++--- test.sh | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 test.sh diff --git a/.travis.yml b/.travis.yml index 944807d1..c3cd313a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,9 +21,8 @@ before_script: script: - psql -c 'create database mpwo_test;' -U postgres - - echo $DATABASE_TEST_URL - - psql -c '\list' -U postgres - docker-compose -f docker-compose-ci.yml run api flask init_db + - sh test.sh after_script: - docker-compose down diff --git a/Makefile b/Makefile index a49b35a1..5ed4ca74 100644 --- a/Makefile +++ b/Makefile @@ -31,4 +31,4 @@ test-e2e: $(NPM) test test-python: - $(FLASK) test + $(FLASK) test_local diff --git a/Makefile.config b/Makefile.config index a5571b42..0dcdeb49 100644 --- a/Makefile.config +++ b/Makefile.config @@ -2,12 +2,12 @@ HOST = 0.0.0.0 API_PORT = 5000 CLIENT_PORT = 3000 -export REACT_APP_API_URL ?= http://$(HOST):$(API_PORT)/api/ -export FLASK_APP ?= $(PWD)/mpwo_api/server.py -export TEST_URL ?= http://$(HOST):$(CLIENT_PORT) -export REQUIREMENTS ?= $(PWD)/mpwo_api/requirements.txt -export DATABASE_URL ?= postgres://mpwo:mpwo@$(HOST):5432/mpwo -export DATABASE_TEST_URL ?= postgres://mpwo:mpwo@$(HOST):5432/mpwo_test +export REACT_APP_API_URL = http://$(HOST):$(API_PORT)/api/ +export FLASK_APP = $(PWD)/mpwo_api/server.py +export TEST_URL = http://$(HOST):$(CLIENT_PORT) +export REQUIREMENTS = $(PWD)/mpwo_api/requirements.txt +export DATABASE_URL = postgres://mpwo:mpwo@$(HOST):5432/mpwo +export DATABASE_TEST_URL = postgres://mpwo:mpwo@$(HOST):5432/mpwo_test # Python env PYTHON_VERSION ?= python diff --git a/mpwo_api/server.py b/mpwo_api/server.py index 92c30964..5f2e4218 100644 --- a/mpwo_api/server.py +++ b/mpwo_api/server.py @@ -19,16 +19,27 @@ def init_db(): print('Database initialization done.') -@app.cli.command() -def test(): +def run_test(test_path='mpwo_api/tests'): """Runs the tests without code coverage.""" tests = unittest.TestLoader().discover( - 'mpwo_api/mpwo_api/tests', pattern='test*.py') + test_path, pattern='test*.py') result = unittest.TextTestRunner(verbosity=2).run(tests) if result.wasSuccessful(): return 0 return 1 +@app.cli.command() +def test(test_path='mpwo_api/tests'): + """Runs the tests without code coverage.""" + run_test() + + +@app.cli.command() +def test_local(): + """Runs the tests without code coverage in local machine w/ make.""" + run_test('mpwo_api/mpwo_api/tests') + + if __name__ == '__main__': app.run() diff --git a/test.sh b/test.sh new file mode 100644 index 00000000..3d7d0de9 --- /dev/null +++ b/test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +fails='' + +inspect() { + if [ $1 -ne 0 ]; then + fails="${fails} $2" + fi +} + +docker-compose -f docker-compose-ci.yml run api flask test +inspect $? api + +if [ -n "${fails}" ]; + then + echo "Tests failed: ${fails}" + exit 1 + else + echo "Tests passed!" + exit 0 +fi