-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
333 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Run tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- develop | ||
pull_request: | ||
branches: | ||
- main | ||
- develop | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.8", "3.9"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install \ | ||
-r requirements.txt \ | ||
flake8 \ | ||
pytest \ | ||
. | ||
- name: Lint | ||
run: | | ||
flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics | ||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics | ||
- name: Test | ||
run: | | ||
pytest --capture=sys --ignore=test/test_docker.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +0,0 @@ | ||
import logging | ||
import os.path | ||
from logging.config import dictConfig | ||
|
||
from dynaconf.contrib import FlaskDynaconf | ||
from flasgger import Swagger | ||
from flask import Flask | ||
|
||
from aardvark.advisors import advisor_bp | ||
from aardvark.persistence.sqlalchemy import SQLAlchemyPersistence | ||
|
||
BLUEPRINTS = [advisor_bp] | ||
|
||
API_VERSION = "1" | ||
|
||
log = logging.getLogger("aardvark") | ||
|
||
|
||
def create_app(**kwargs): | ||
init_logging() | ||
app = Flask(__name__, static_url_path="/static") | ||
Swagger(app) | ||
persistence = SQLAlchemyPersistence() | ||
|
||
FlaskDynaconf(app, **kwargs) | ||
|
||
# For ELB and/or Eureka | ||
@app.route("/healthcheck") | ||
def healthcheck(): | ||
"""Healthcheck | ||
Simple healthcheck that indicates the services is up | ||
--- | ||
responses: | ||
200: | ||
description: service is up | ||
""" | ||
return "ok" | ||
|
||
# Blueprints | ||
for bp in BLUEPRINTS: | ||
app.register_blueprint(bp, url_prefix=f"/api/{API_VERSION}") | ||
|
||
# Extensions: | ||
persistence.init_db() | ||
|
||
return app | ||
|
||
|
||
def init_logging(): | ||
log_cfg = { | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"standard": {"format": "%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]"} | ||
}, | ||
"handlers": { | ||
"file": { | ||
"class": "logging.handlers.RotatingFileHandler", | ||
"level": "DEBUG", | ||
"formatter": "standard", | ||
"filename": "aardvark.log", | ||
"maxBytes": 10485760, | ||
"backupCount": 100, | ||
"encoding": "utf8", | ||
}, | ||
"console": { | ||
"class": "logging.StreamHandler", | ||
"level": "DEBUG", | ||
"formatter": "standard", | ||
"stream": "ext://sys.stdout", | ||
}, | ||
}, | ||
"loggers": {"aardvark": {"handlers": ["file", "console"], "level": "DEBUG"}}, | ||
} | ||
dictConfig(log_cfg) | ||
|
||
|
||
def _find_config(): | ||
"""Search for config.py in order of preference and return path if it exists, else None""" | ||
config_paths = [ | ||
os.path.join(os.getcwd(), "config.py"), | ||
"/etc/aardvark/config.py", | ||
"/apps/aardvark/config.py", | ||
] | ||
for path in config_paths: | ||
if os.path.exists(path): | ||
return path | ||
return None | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import annotations | ||
|
||
import os.path | ||
import logging | ||
import sys | ||
from logging import DEBUG, Formatter, StreamHandler | ||
from logging.config import dictConfig | ||
from typing import TYPE_CHECKING | ||
|
||
from flask_sqlalchemy import SQLAlchemy | ||
from flask import Flask | ||
from flasgger import Swagger | ||
|
||
if TYPE_CHECKING: | ||
from flask import Config | ||
|
||
db = SQLAlchemy() | ||
|
||
from aardvark.view import mod as advisor_bp # noqa | ||
|
||
BLUEPRINTS = [ | ||
advisor_bp | ||
] | ||
|
||
API_VERSION = "1" | ||
|
||
|
||
def create_app(config_override: Config = None): | ||
app = Flask(__name__, static_url_path="/static") | ||
Swagger(app) | ||
|
||
if config_override: | ||
app.config.from_mapping(config_override) | ||
else: | ||
path = _find_config() | ||
if not path: | ||
print("No config") | ||
app.config.from_pyfile("_config.py") | ||
else: | ||
app.config.from_pyfile(path) | ||
|
||
# For ELB and/or Eureka | ||
@app.route("/healthcheck") | ||
def healthcheck(): | ||
"""Healthcheck | ||
Simple healthcheck that indicates the services is up | ||
--- | ||
responses: | ||
200: | ||
description: service is up | ||
""" | ||
return "ok" | ||
|
||
# Blueprints | ||
for bp in BLUEPRINTS: | ||
app.register_blueprint(bp, url_prefix=f"/api/{API_VERSION}") | ||
|
||
# Extensions: | ||
db.init_app(app) | ||
setup_logging(app) | ||
|
||
return app | ||
|
||
|
||
def _find_config(): | ||
"""Search for config.py in order of preference and return path if it exists, else None""" | ||
config_paths = [ | ||
os.path.join(os.getcwd(), "config.py"), | ||
"/etc/aardvark/config.py", | ||
"/apps/aardvark/config.py", | ||
] | ||
for path in config_paths: | ||
if os.path.exists(path): | ||
return path | ||
return None | ||
|
||
|
||
def setup_logging(app): | ||
if not app.debug: | ||
if app.config.get("LOG_CFG"): | ||
# initialize the Flask logger (removes all handlers) | ||
dictConfig(app.config.get("LOG_CFG")) | ||
app.logger = logging.getLogger(__name__) | ||
else: | ||
handler = StreamHandler(stream=sys.stderr) | ||
|
||
handler.setFormatter(Formatter("%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]")) | ||
app.logger.setLevel(app.config.get("LOG_LEVEL", DEBUG)) | ||
app.logger.addHandler(handler) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def app_config(): | ||
"""Return a Flask configuration object for testing. The returned configuration is intended to be a good base for | ||
testing and can be customized for specific testing needs. | ||
""" | ||
from flask import Config | ||
c = Config('.') | ||
c.from_mapping({ | ||
"SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:", | ||
"SQLALCHEMY_TRACK_MODIFICATIONS": False, | ||
"DEBUG": False, | ||
"LOG_CFG": {'version': 1, 'handlers': []}, # silence logging | ||
}) | ||
return c | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_database(app_config): | ||
"""Yield an instance of flask_sqlalchemy.SQLAlchemy associated with the base model class used in aardvark.model. | ||
This is almost certainly not safe for parallel/multi-threaded use. | ||
""" | ||
from aardvark.app import db | ||
mock_db = SQLAlchemy(model_class=db.Model) | ||
|
||
from aardvark.app import create_app | ||
app = create_app(config_override=app_config) | ||
with app.app_context(): | ||
mock_db.create_all() | ||
yield mock_db | ||
mock_db.drop_all() |
Oops, something went wrong.