From fa446d20efac7732e71d0efc50c5bf92d65bbe0a Mon Sep 17 00:00:00 2001 From: "morpheus.0" Date: Tue, 18 Jul 2017 13:44:26 +0900 Subject: [PATCH] Refactoring the structure of project, and add test using pytest * README.md; modify the explanation * Refactoring the structure of project * Dockerfile3 * app/__init__.py * app/api_only_flask.py * run.py * common/__init__.py * common/src/__init__.py * app/requirements.txt * Add test using pytest * test/__init__.py * test/test_api_only_flask.py * test/test_app.py * test/pytest.ini --- python/test-flask/API/Dockerfile3 | 9 ++++----- python/test-flask/API/README.md | 9 ++++++++- python/test-flask/API/app/__init__.py | 7 +++++++ .../test-flask/API/{ => app}/api_only_flask.py | 18 ++++++------------ .../test-flask/API/{ => app}/requirements.txt | 0 python/test-flask/API/common/__init__.py | 0 python/test-flask/API/common/src/__init__.py | 0 python/test-flask/API/run.py | 5 +++++ python/test-flask/API/test/__init__.py | 0 python/test-flask/API/test/pytest.ini | 4 ++++ .../test-flask/API/test/test_api_only_flask.py | 13 +++++++++++++ python/test-flask/API/test/test_app.py | 3 +++ 12 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 python/test-flask/API/app/__init__.py rename python/test-flask/API/{ => app}/api_only_flask.py (81%) rename python/test-flask/API/{ => app}/requirements.txt (100%) create mode 100644 python/test-flask/API/common/__init__.py create mode 100644 python/test-flask/API/common/src/__init__.py create mode 100644 python/test-flask/API/run.py create mode 100644 python/test-flask/API/test/__init__.py create mode 100644 python/test-flask/API/test/pytest.ini create mode 100644 python/test-flask/API/test/test_api_only_flask.py diff --git a/python/test-flask/API/Dockerfile3 b/python/test-flask/API/Dockerfile3 index f4e05996..b17a0bbe 100644 --- a/python/test-flask/API/Dockerfile3 +++ b/python/test-flask/API/Dockerfile3 @@ -5,15 +5,14 @@ RUN apt-get install -y python3-pip python3-dev build-essential RUN mkdir /app RUN mkdir /app/logs -COPY ./api_only_flask.py /app -COPY ./settings.py /app -COPY ./requirements.txt /app +COPY ./run.py /app/run.py +COPY ./app/* /app/app/ COPY ./common /app/common COPY ./test /app/test WORKDIR /app RUN pip3 install --upgrade pip -RUN pip3 install -r requirements.txt +RUN pip3 install -r app/requirements.txt ENTRYPOINT ["python3"] -CMD ["api_only_flask.py"] +CMD ["run.py"] diff --git a/python/test-flask/API/README.md b/python/test-flask/API/README.md index bbe0aa74..ce1b4eb5 100644 --- a/python/test-flask/API/README.md +++ b/python/test-flask/API/README.md @@ -4,7 +4,7 @@ Flask API * Execution ``` - $ docker run --env-file=common/config/settings.env --rm -v `pwd`/logs:/app/logs -p 59459:5000 test-flask-api + $ docker run --env-file=common/config/settings.env --rm -v `pwd`/logs:/app/logs -v /etc/localtime:/etc/localtime:ro -p 59459:5000 test-flask-api * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! @@ -33,3 +33,10 @@ Flask API ``` $ docker build -f Dockerfile3 -t test-flask-api:latest . ``` +* Test + + ``` + $ pip3 install -U pytest + $ pip3 install pytest-env + $ pytest -v -c test/pytest.ini + ``` diff --git a/python/test-flask/API/app/__init__.py b/python/test-flask/API/app/__init__.py new file mode 100644 index 00000000..6113c80d --- /dev/null +++ b/python/test-flask/API/app/__init__.py @@ -0,0 +1,7 @@ +from flask import Flask + + +app = Flask(__name__) + + +from app import api_only_flask diff --git a/python/test-flask/API/api_only_flask.py b/python/test-flask/API/app/api_only_flask.py similarity index 81% rename from python/test-flask/API/api_only_flask.py rename to python/test-flask/API/app/api_only_flask.py index 21908a96..1a97242e 100644 --- a/python/test-flask/API/api_only_flask.py +++ b/python/test-flask/API/app/api_only_flask.py @@ -1,7 +1,6 @@ -# -*- coding: utf8 -*- +from app import app from flask import Flask from flask import request -from flask_pytest import FlaskPytest import logging import os import sys @@ -17,11 +16,6 @@ common.setup_logging(os.environ.get('LOGGING_CONFIG_FILEPATH')) -app = Flask(__name__) -app.config.from_pyfile('settings.py') -app = FlaskPytest(app) - - @app.route('/') def api_root(): return 'Welcome' @@ -81,8 +75,8 @@ def stop(): return 'stop' -if __name__ == '__main__': - # IP_ADDRESS = socket.gethostbyname(socket.gethostname()) - app.logger.setLevel(logging.DEBUG) - # app.run(host=IP_ADDRESS, port=59459, debug=True) - app.run(host='0.0.0.0', debug=True) +# if __name__ == '__main__': +# # IP_ADDRESS = socket.gethostbyname(socket.gethostname()) +# app.logger.setLevel(logging.DEBUG) +# # app.run(host=IP_ADDRESS, port=59459, debug=True) +# app.run(host='0.0.0.0', debug=True) diff --git a/python/test-flask/API/requirements.txt b/python/test-flask/API/app/requirements.txt similarity index 100% rename from python/test-flask/API/requirements.txt rename to python/test-flask/API/app/requirements.txt diff --git a/python/test-flask/API/common/__init__.py b/python/test-flask/API/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/test-flask/API/common/src/__init__.py b/python/test-flask/API/common/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/test-flask/API/run.py b/python/test-flask/API/run.py new file mode 100644 index 00000000..0d71c192 --- /dev/null +++ b/python/test-flask/API/run.py @@ -0,0 +1,5 @@ +from app import app + +if __name__ == '__main__': + # app.run(host='0.0.0.0', port=59459, debug=True) + app.run(host='0.0.0.0', debug=True) diff --git a/python/test-flask/API/test/__init__.py b/python/test-flask/API/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/test-flask/API/test/pytest.ini b/python/test-flask/API/test/pytest.ini new file mode 100644 index 00000000..0d90d1df --- /dev/null +++ b/python/test-flask/API/test/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +env = + DEBUG=True + LOGGING_CONFIG_FILEPATH=common/config/logging.yaml diff --git a/python/test-flask/API/test/test_api_only_flask.py b/python/test-flask/API/test/test_api_only_flask.py new file mode 100644 index 00000000..2af843d0 --- /dev/null +++ b/python/test-flask/API/test/test_api_only_flask.py @@ -0,0 +1,13 @@ +import os +import sys +import py.test + + +sys.path.append(os.path.join('.', '..',)) + + +from app import api_only_flask + + +def test_api_root(): + assert api_only_flask.api_root() == 'Welcome' diff --git a/python/test-flask/API/test/test_app.py b/python/test-flask/API/test/test_app.py index c7cf807d..077f2be8 100644 --- a/python/test-flask/API/test/test_app.py +++ b/python/test-flask/API/test/test_app.py @@ -1,2 +1,5 @@ +import pytest + + def test_app(): assert True