From f7ede6368189d09c817d606cf67b2e6f106499f4 Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Mon, 5 Jun 2023 22:33:36 +0200 Subject: [PATCH] introduce blueprints and unit tests --- app/__init__.py | 43 +++++++++++++++++-------------------------- app/color.py | 27 +++++++++++++++++++++++++++ app/index.py | 8 ++++++++ app/requirements.txt | 9 --------- config.py | 30 ++++++++++++++++++++++++++++++ tests/test_basic.py | 20 ++++++++++++++++++++ tests/test_index.py | 19 +++++++++++++++++++ 7 files changed, 121 insertions(+), 35 deletions(-) create mode 100644 app/color.py create mode 100644 app/index.py delete mode 100644 app/requirements.txt create mode 100644 config.py create mode 100644 tests/test_basic.py create mode 100644 tests/test_index.py diff --git a/app/__init__.py b/app/__init__.py index 44ef5ef..57befa5 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,32 +1,23 @@ -from flask import Flask, render_template -import socket -import os -import json +from flask import Flask +from config import config app = Flask(__name__) -@app.route('/') -def index(): - return render_template('index.html') +def create_app(config_name): + app = Flask(__name__) + from .index import index_blueprint + from .color import color_blueprint + app.register_blueprint(index_blueprint) + app.register_blueprint(color_blueprint) + app.config.from_object(config[config_name]) + config[config_name].init_app(app) + @app.cli.command() + def test(): + """Run the unit tests.""" + import unittest + tests = unittest.TestLoader().discover('tests') + unittest.TextTestRunner(verbosity=2).run(tests) -@app.route('/color/', methods=['POST']) -def color(color): - socket_path = '/var/www/vhosts/rgb.local/rgb_socket' - if os.path.exists(socket_path): - client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - client.connect(socket_path) - client.sendall(color.encode()) - client.close() - return (json.dumps({'success': True}), - 200, - {'ContentType': 'application/json'}) - else: - return (json.dumps({'success': False}), - 500, - {'ContentType': 'application/json'}) - - -if __name__ == "__main__": - app.run() + return app diff --git a/app/color.py b/app/color.py new file mode 100644 index 0000000..ebdd8e0 --- /dev/null +++ b/app/color.py @@ -0,0 +1,27 @@ +from flask import Blueprint +import socket +import os +import json + + +color_blueprint = Blueprint('color', __name__) + + +@color_blueprint.route('/color/', methods=['POST']) +def color(color): + socket_path = '/var/www/vhosts/rgb.local/rgb_socket' + if os.path.exists(socket_path): + client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + client.connect(socket_path) + client.sendall(color.encode()) + client.close() + return (json.dumps({'success': True}), + 200, + {'ContentType': 'application/json'}) + else: + return (json.dumps({ + 'success': False, + 'message': 'socket not found' + }), + 500, + {'ContentType': 'application/json'}) diff --git a/app/index.py b/app/index.py new file mode 100644 index 0000000..f332cf7 --- /dev/null +++ b/app/index.py @@ -0,0 +1,8 @@ +from flask import render_template, Blueprint + +index_blueprint = Blueprint('index', __name__) + + +@index_blueprint.route('/') +def index(): + return render_template('index.html') diff --git a/app/requirements.txt b/app/requirements.txt deleted file mode 100644 index 6c478c5..0000000 --- a/app/requirements.txt +++ /dev/null @@ -1,9 +0,0 @@ -blinker==1.6.2 -click==8.1.3 -Flask==2.3.2 -importlib-metadata==6.6.0 -itsdangerous==2.1.2 -Jinja2==3.1.2 -MarkupSafe==2.1.2 -Werkzeug==2.3.4 -zipp==3.15.0 diff --git a/config.py b/config.py new file mode 100644 index 0000000..f1329c9 --- /dev/null +++ b/config.py @@ -0,0 +1,30 @@ +import os +basedir = os.path.abspath(os.path.dirname(__file__)) + + +class Config: + SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string' + + @staticmethod + def init_app(app): + pass + + +class DevelopmentConfig(Config): + DEBUG = True + + +class TestingConfig(Config): + TESTING = True + + +class ProductionConfig(Config): + pass + + +config = { + 'development': DevelopmentConfig, + 'testing': TestingConfig, + 'production': ProductionConfig, + 'default': DevelopmentConfig +} diff --git a/tests/test_basic.py b/tests/test_basic.py new file mode 100644 index 0000000..0ed0561 --- /dev/null +++ b/tests/test_basic.py @@ -0,0 +1,20 @@ +import unittest +from flask import current_app +from app import create_app + + +class BasicsTestCase(unittest.TestCase): + + def setUp(self): + self.app = create_app('testing') + self.app_context = self.app.app_context() + self.app_context.push() + + def tearDown(self): + self.app_context.pop() + + def test_app_exists(self): + self.assertFalse(current_app is None) + + def test_app_is_testing(self): + self.assertTrue(current_app.config['TESTING']) diff --git a/tests/test_index.py b/tests/test_index.py new file mode 100644 index 0000000..8f3f267 --- /dev/null +++ b/tests/test_index.py @@ -0,0 +1,19 @@ +import unittest +from app import create_app + + +class BasicsTestCase(unittest.TestCase): + + def setUp(self): + self.app = create_app('testing') + self.app_context = self.app.app_context() + self.app_context.push() + + def tearDown(self): + self.app_context.pop() + + def test_index(self): + with self.app.test_client() as test_client: + response = test_client.get('/') + self.assertEqual(response.status_code, 200) + self.assertIn(b'

RGB controller

', response.data)