introduce blueprints and unit tests
This commit is contained in:
parent
f5d8f1d6e3
commit
f7ede63681
7 changed files with 121 additions and 35 deletions
|
@ -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/<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
|
||||
|
|
27
app/color.py
Normal file
27
app/color.py
Normal 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
8
app/index.py
Normal 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')
|
|
@ -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
30
config.py
Normal 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
20
tests/test_basic.py
Normal 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
19
tests/test_index.py
Normal 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)
|
Loading…
Reference in a new issue