migration and testing
This commit is contained in:
6
Makefile
6
Makefile
@ -91,3 +91,9 @@ wait:
|
|||||||
sleep 2; echo -n "."; \
|
sleep 2; echo -n "."; \
|
||||||
done; echo "\n[✅] $$WEB_CONTAINER is healthy!"'
|
done; echo "\n[✅] $$WEB_CONTAINER is healthy!"'
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
flask db migrate -m "auto"
|
||||||
|
|
||||||
|
upgrade:
|
||||||
|
flask db upgrade
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import importlib.util
|
|
||||||
import importlib
|
import importlib
|
||||||
|
import importlib.util
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
@ -9,9 +10,10 @@ from flask_login import LoginManager
|
|||||||
from flask_wtf.csrf import CSRFProtect
|
from flask_wtf.csrf import CSRFProtect
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
# Load environment variables from .env
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
# Initialize extensions
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
migrate = Migrate()
|
migrate = Migrate()
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
@ -21,9 +23,9 @@ csrf = CSRFProtect()
|
|||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object('app.config.Config')
|
app.config.from_object('app.config.Config')
|
||||||
csrf.init_app(app)
|
|
||||||
|
|
||||||
# Initialize core extensions
|
# Initialize core extensions
|
||||||
|
csrf.init_app(app)
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
migrate.init_app(app, db)
|
migrate.init_app(app, db)
|
||||||
login_manager.init_app(app)
|
login_manager.init_app(app)
|
||||||
@ -33,8 +35,7 @@ def create_app():
|
|||||||
from .errors import bp as errors_bp
|
from .errors import bp as errors_bp
|
||||||
app.register_blueprint(errors_bp)
|
app.register_blueprint(errors_bp)
|
||||||
|
|
||||||
# Plugin auto-loader
|
# Auto-discover and register plugins
|
||||||
# Plugin auto-loader
|
|
||||||
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
||||||
for plugin in os.listdir(plugin_path):
|
for plugin in os.listdir(plugin_path):
|
||||||
if plugin.endswith('.noload'):
|
if plugin.endswith('.noload'):
|
||||||
@ -45,7 +46,7 @@ def create_app():
|
|||||||
if not os.path.isdir(plugin_dir):
|
if not os.path.isdir(plugin_dir):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Register routes
|
# 1. Register routes
|
||||||
route_file = os.path.join(plugin_dir, 'routes.py')
|
route_file = os.path.join(plugin_dir, 'routes.py')
|
||||||
if os.path.isfile(route_file):
|
if os.path.isfile(route_file):
|
||||||
try:
|
try:
|
||||||
@ -57,25 +58,35 @@ def create_app():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[⚠️] Failed to load routes from plugin '{plugin}': {e}")
|
print(f"[⚠️] Failed to load routes from plugin '{plugin}': {e}")
|
||||||
|
|
||||||
# Register CLI commands and plugin entry points
|
# Define paths
|
||||||
init_file = os.path.join(plugin_dir, '__init__.py')
|
init_file = os.path.join(plugin_dir, '__init__.py')
|
||||||
|
plugin_json = os.path.join(plugin_dir, 'plugin.json')
|
||||||
|
model_file = os.path.join(plugin_dir, 'models.py')
|
||||||
|
|
||||||
|
# 2. Register CLI commands and run entry point
|
||||||
if os.path.isfile(init_file):
|
if os.path.isfile(init_file):
|
||||||
try:
|
try:
|
||||||
cli_module = importlib.import_module(f"plugins.{plugin}")
|
cli_module = importlib.import_module(f"plugins.{plugin}")
|
||||||
if hasattr(cli_module, 'register_cli'):
|
if hasattr(cli_module, 'register_cli'):
|
||||||
cli_module.register_cli(app)
|
cli_module.register_cli(app)
|
||||||
|
|
||||||
plugin_json = os.path.join(plugin_dir, 'plugin.json')
|
|
||||||
if os.path.isfile(plugin_json):
|
if os.path.isfile(plugin_json):
|
||||||
try:
|
with open(plugin_json, 'r') as f:
|
||||||
meta = json.load(open(plugin_json, 'r'))
|
meta = json.load(f)
|
||||||
entry = meta.get('entry_point')
|
entry = meta.get('entry_point')
|
||||||
if entry and hasattr(cli_module, entry):
|
if entry and hasattr(cli_module, entry):
|
||||||
getattr(cli_module, entry)(app)
|
getattr(cli_module, entry)(app)
|
||||||
except Exception as e:
|
|
||||||
print(f"[⚠️] Failed to run entry_point '{entry}' for plugin '{plugin}': {e}")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[⚠️] Failed to load CLI from plugin '{plugin}': {e}")
|
print(f"[⚠️] Failed to load CLI for plugin '{plugin}': {e}")
|
||||||
|
|
||||||
|
# 3. Auto-load plugin models for migrations
|
||||||
|
if os.path.isfile(model_file):
|
||||||
|
try:
|
||||||
|
spec = importlib.util.spec_from_file_location(f"plugins.{plugin}.models", model_file)
|
||||||
|
mod = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(mod)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[⚠️] Failed to load models from plugin '{plugin}': {e}")
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_current_year():
|
def inject_current_year():
|
||||||
|
@ -2,5 +2,5 @@ import click
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
def register_cli(app: Flask):
|
def register_cli(app: Flask):
|
||||||
# No CLI commands yet for admin plugin
|
# CLI entry-point for admin
|
||||||
pass
|
pass
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from app import create_app
|
from app import create_app
|
||||||
|
|
||||||
def test_app_loads_plugins():
|
def test_app_loads_plugins():
|
||||||
app = create_app({'TESTING': True})
|
app = create_app({'TESTING': True})
|
||||||
# Assuming app.plugins is a dict of loaded plugin modules
|
assert hasattr(app, 'plugins'), "App missing plugins attribute"
|
||||||
assert hasattr(app, 'plugins'), "App object missing 'plugins' attribute"
|
|
||||||
for plugin in ["auth", "admin", "plant", "cli"]:
|
for plugin in ["auth", "admin", "plant", "cli"]:
|
||||||
assert plugin in app.plugins, f"Plugin '{plugin}' not loaded into app.plugins"
|
assert plugin in app.plugins, f"Plugin {plugin} not loaded"
|
||||||
|
12
tests/test_database_smoke.py
Normal file
12
tests/test_database_smoke.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import pytest
|
||||||
|
from app import create_app, db
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
def test_database_smoke(tmp_path):
|
||||||
|
db_file = tmp_path / "test.db"
|
||||||
|
app = create_app({"TESTING": True, "SQLALCHEMY_DATABASE_URI": f"sqlite:///{db_file}"})
|
||||||
|
with app.app_context():
|
||||||
|
db.drop_all()
|
||||||
|
db.create_all()
|
||||||
|
tables = inspect(db.engine).get_table_names()
|
||||||
|
assert 'users' in tables or 'user' in tables
|
@ -3,20 +3,16 @@ from app.hooks import EventDispatcher, listen_event
|
|||||||
|
|
||||||
def test_register_and_dispatch():
|
def test_register_and_dispatch():
|
||||||
results = []
|
results = []
|
||||||
def listener1(a, b=None):
|
def listener(a, b=None):
|
||||||
results.append(('l1', a, b))
|
results.append((a, b))
|
||||||
def listener2(a, b=None):
|
EventDispatcher.register('evt', listener)
|
||||||
results.append(('l2', a, b))
|
EventDispatcher.dispatch('evt', 1, b=2)
|
||||||
EventDispatcher.register('test_event', listener1)
|
assert results == [(1, 2)]
|
||||||
EventDispatcher.register('test_event', listener2)
|
|
||||||
EventDispatcher.dispatch('test_event', 1, b=2)
|
|
||||||
assert ('l1', 1, 2) in results
|
|
||||||
assert ('l2', 1, 2) in results
|
|
||||||
|
|
||||||
def test_listen_event_decorator():
|
def test_listen_event_decorator():
|
||||||
results = []
|
results = []
|
||||||
@listen_event('decorated_event')
|
@listen_event('evt2')
|
||||||
def handler(x):
|
def handler(x):
|
||||||
results.append(x)
|
results.append(x)
|
||||||
EventDispatcher.dispatch('decorated_event', 'hello')
|
EventDispatcher.dispatch('evt2', 'hello')
|
||||||
assert results == ['hello']
|
assert results == ['hello']
|
||||||
|
@ -1,25 +1,21 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import pytest
|
import pytest
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
# Directory containing plugins
|
|
||||||
PLUGINS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
PLUGINS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
||||||
|
|
||||||
@pytest.mark.parametrize("plugin", ["auth", "admin", "plant", "cli"])
|
@pytest.mark.parametrize("plugin", ["auth", "admin", "plant", "cli"])
|
||||||
def test_plugin_metadata_and_init(plugin, tmp_path, monkeypatch):
|
def test_plugin_metadata_and_init(plugin):
|
||||||
# Construct plugin path
|
|
||||||
plugin_path = os.path.join(PLUGINS_DIR, plugin)
|
plugin_path = os.path.join(PLUGINS_DIR, plugin)
|
||||||
# Test plugin.json exists and contains required keys
|
|
||||||
meta_path = os.path.join(plugin_path, 'plugin.json')
|
meta_path = os.path.join(plugin_path, 'plugin.json')
|
||||||
assert os.path.isfile(meta_path), f"plugin.json missing for {plugin}"
|
assert os.path.isfile(meta_path), f"plugin.json missing for {plugin}"
|
||||||
meta = json.loads(open(meta_path).read())
|
meta = json.loads(open(meta_path).read())
|
||||||
for key in ("name", "version", "description"):
|
for key in ("name", "version", "description"):
|
||||||
assert key in meta, f"{{key}} missing in {plugin}/plugin.json"
|
assert key in meta, f"{key} missing in {plugin}/plugin.json"
|
||||||
# Test __init__.py can be imported and has register_cli
|
init_path = os.path.join(plugin_path, '__init__.py')
|
||||||
module_name = f"plugins.{plugin}"
|
if os.path.exists(init_path):
|
||||||
spec = importlib.util.spec_from_file_location(module_name, os.path.join(plugin_path, '__init__.py'))
|
spec = importlib.util.spec_from_file_location(f"plugins.{plugin}", init_path)
|
||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
assert hasattr(module, "register_cli"), f"register_cli missing in {plugin}/__init__.py"
|
assert hasattr(module, "register_cli"), f"register_cli missing in {plugin}/__init__.py"
|
||||||
|
Reference in New Issue
Block a user