introduce blueprints and unit tests
Some checks failed
CSS Linter / CSS-Lint (push) Failing after 23s
Javascript Linter / Javascript-Lint (push) Successful in 10s
Python formatting PEP8 / Pyhton-PEP8 (push) Successful in 13s

This commit is contained in:
Finn Christiansen 2023-06-05 22:33:36 +02:00
parent f5d8f1d6e3
commit f7ede63681
7 changed files with 121 additions and 35 deletions

View file

@ -1,32 +1,23 @@
from flask import Flask, render_template from flask import Flask
import socket from config import config
import os
import json
app = Flask(__name__) app = Flask(__name__)
@app.route('/') def create_app(config_name):
def index(): app = Flask(__name__)
return render_template('index.html') 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/<color>', methods=['POST']) return app
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()

27
app/color.py Normal file
View file

@ -0,0 +1,27 @@
from flask import Blueprint
import socket
import os
import json
color_blueprint = Blueprint('color', __name__)
@color_blueprint.route('/color/<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'})

8
app/index.py Normal file
View file

@ -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')

View file

@ -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

30
config.py Normal file
View file

@ -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
}

20
tests/test_basic.py Normal file
View file

@ -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'])

19
tests/test_index.py Normal file
View file

@ -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'<h1>RGB controller</h1>', response.data)