From 360d242a443a33e2f19bba5968fbfb2e788e1828 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Thu, 7 Dec 2023 10:58:18 +0100 Subject: [PATCH] added app.py --- app.py | 11 +++++++++++ tests/__init__.py | 1 + tests/conftest.py | 9 +++++++++ tests/test_api.py | 23 +++++++++++++++++++++++ web/app.py | 22 ++++++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 app.py create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_api.py create mode 100644 web/app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..a6ec564 --- /dev/null +++ b/app.py @@ -0,0 +1,11 @@ +from flask import Flask + +app = Flask(__name__) + + +@app.route("/") +def helloworld(): + return "Helle from the hell" + +if __name__ == "__main__": + app.run(debug=True,host="0.0.0.0",port=5000) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..2850f7c --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# for pytest diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0a2cbc2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,9 @@ +import pytest + + +@pytest.fixture +def client(): + from web import app + + app.app.config['TESTING'] = True + yield app.app.test_client() diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..ed2b073 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,23 @@ +from urllib.parse import urlencode +import json + + +def call(client, path, params): + url = path + '?' + urlencode(params) + response = client.get(url) + return json.loads(response.data.decode('utf-8')) + + +def test_plus_one(client): + result = call(client, '/plus_one', {'x': 2}) + assert result['x'] == 3 + + +def test_plus_two(client): + result = call(client, '/plus_two', {'x': 2}) + assert result['x'] == 4 + + +def test_square(client): + result = call(client, '/square', {'x': 2}) + assert result['x'] == 4 diff --git a/web/app.py b/web/app.py new file mode 100644 index 0000000..27197a5 --- /dev/null +++ b/web/app.py @@ -0,0 +1,22 @@ +from flask import request, Flask +import json + +app = Flask(__name__) + + +@app.route('/plus_one') +def plus_one(): + x = int(request.args.get('x', 1)) + return json.dumps({'x': x + 1}) + + +@app.route('/plus_two') +def plus_two(): + x = int(request.args.get('x', 1)) + return json.dumps({'x': x + 2}) + + +@app.route('/square') +def square(): + x = int(request.args.get('x', 1)) + return json.dumps({'x': x * x})