mirror of
https://github.com/Doctorado-ML/beflask.git
synced 2025-08-15 15:15:52 +00:00
Begin testing
This commit is contained in:
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@@ -2,5 +2,10 @@
|
||||
"[python]": {
|
||||
"editor.defaultFormatter": "ms-python.black-formatter"
|
||||
},
|
||||
"python.formatting.provider": "none"
|
||||
"python.formatting.provider": "none",
|
||||
"python.testing.pytestArgs": [
|
||||
"tests"
|
||||
],
|
||||
"python.testing.unittestEnabled": false,
|
||||
"python.testing.pytestEnabled": true
|
||||
}
|
@@ -4,7 +4,7 @@ from flask import Flask
|
||||
from flask_bootstrap import Bootstrap5
|
||||
from flask_login import LoginManager
|
||||
from flask_socketio import SocketIO
|
||||
from .config import config, load_env
|
||||
from .config import config
|
||||
from .models import User, db
|
||||
|
||||
from .results.main_results import results
|
||||
@@ -19,19 +19,19 @@ login_manager = LoginManager()
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return User.query.get(int(user_id))
|
||||
return db.session.get(User, int(user_id))
|
||||
|
||||
|
||||
def make_shell_context():
|
||||
return {"db": db, "User": User}
|
||||
|
||||
|
||||
def create_app():
|
||||
def create_app(config_name=None):
|
||||
if config_name is None:
|
||||
config_name = os.environ.get("BEFLASK_ENV", "development")
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config[config_name])
|
||||
bootstrap.init_app(app)
|
||||
load_env()
|
||||
config_object = config[os.getenv("BEFLASK_ENV", "development")]
|
||||
app.config.from_object(config_object)
|
||||
db.init_app(app)
|
||||
login_manager.init_app(app)
|
||||
login_manager.login_view = "main.login"
|
||||
|
@@ -8,10 +8,9 @@ def get_base_dir():
|
||||
return os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
def load_env():
|
||||
dotenv_file = ".env"
|
||||
file_name = os.path.join(get_base_dir(), dotenv_file)
|
||||
load_dotenv(file_name)
|
||||
dotenv_file = ".env"
|
||||
file_name = os.path.join(get_base_dir(), dotenv_file)
|
||||
load_dotenv(file_name)
|
||||
|
||||
|
||||
class Config(object):
|
||||
@@ -40,6 +39,7 @@ class ProductionConfig(Config):
|
||||
class TestingConfig(Config):
|
||||
TESTING = True
|
||||
SQLALCHEMY_DATABASE_URI = "sqlite://"
|
||||
WTF_CSRF_ENABLED = False
|
||||
SOCKETIO_MESSAGE_QUEUE = None
|
||||
|
||||
|
||||
|
@@ -1,5 +1,2 @@
|
||||
COMPARE=True
|
||||
DEBUG=True
|
||||
SECRET_KEY=Really-hard-to-guess-string
|
||||
# possible values: development, testing, productions
|
||||
BEFLASK_ENV=development
|
||||
|
@@ -9,7 +9,8 @@ with app.app_context():
|
||||
b = Benchmark(
|
||||
name="discretizbench",
|
||||
folder="/Users/rmontanana/Code/discretizbench",
|
||||
description="Experiments with local discretization and Bayesian classifiers",
|
||||
description="Experiments with local discretization and Bayesian "
|
||||
"classifiers",
|
||||
)
|
||||
db.session.add(b)
|
||||
b = Benchmark(
|
||||
@@ -30,7 +31,7 @@ with app.app_context():
|
||||
admin=True,
|
||||
benchmark_id=1,
|
||||
)
|
||||
u.set_password("patata")
|
||||
u.set_password("galeote")
|
||||
u1 = User(
|
||||
username="guest",
|
||||
email="guest@example.com",
|
||||
|
@@ -9,7 +9,8 @@ with app.app_context():
|
||||
b = Benchmark(
|
||||
name="discretizbench",
|
||||
folder="/app/discretizbench",
|
||||
description="Experiments with local discretization and Bayesian classifiers",
|
||||
description="Experiments with local discretization and Bayesian "
|
||||
"classifiers",
|
||||
)
|
||||
db.session.add(b)
|
||||
b = Benchmark(
|
||||
@@ -30,7 +31,7 @@ with app.app_context():
|
||||
admin=True,
|
||||
benchmark_id=1,
|
||||
)
|
||||
u.set_password("patata")
|
||||
u.set_password("galeote")
|
||||
u1 = User(
|
||||
username="guest",
|
||||
email="guest@example.com",
|
||||
|
@@ -37,14 +37,18 @@ classifiers = [
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = ["black", "pre-commit", "flake8", "mypy"]
|
||||
dev = ["black", "pre-commit", "flake8", "mypy", "coverage"]
|
||||
|
||||
[project.urls]
|
||||
Home = "https://github.com/doctorado-ml/beflask"
|
||||
|
||||
[tool.coverage.run]
|
||||
branch = true
|
||||
source = ["beflask"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
|
||||
[tool.black]
|
||||
line-length = 79
|
||||
target_version = ['py38', 'py39', 'py310']
|
||||
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
87
tests/conftest.py
Normal file
87
tests/conftest.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import pytest
|
||||
from beflask import app as application
|
||||
from flask_login import FlaskLoginClient
|
||||
from beflask.models import Benchmark, User, db
|
||||
|
||||
|
||||
class AuthActions(object):
|
||||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def login(
|
||||
self, username="guest", password="patata", follow_redirects=False
|
||||
):
|
||||
return self._client.post(
|
||||
"/login",
|
||||
data={"username": username, "password": password},
|
||||
follow_redirects=follow_redirects,
|
||||
)
|
||||
|
||||
def logout(self):
|
||||
return self._client.get("/logout")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def auth(client):
|
||||
return AuthActions(client)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
socketio, app = application.create_app("testing")
|
||||
app.test_client_class = FlaskLoginClient
|
||||
with app.app_context():
|
||||
db_seed(db)
|
||||
return socketio, app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app[1].test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runner(app):
|
||||
return app[1].test_cli_runner()
|
||||
|
||||
|
||||
def db_seed(db):
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
b = Benchmark(
|
||||
name="discretizbench",
|
||||
folder="/Users/rmontanana/Code/discretizbench",
|
||||
description="Experiments with local discretization and Bayesian "
|
||||
"classifiers",
|
||||
)
|
||||
db.session.add(b)
|
||||
b = Benchmark(
|
||||
name="odtebench",
|
||||
folder="/Users/rmontanana/Code/odtebench",
|
||||
description="Experiments with STree and Ensemble classifiers",
|
||||
)
|
||||
db.session.add(b)
|
||||
b = Benchmark(
|
||||
name="covbench",
|
||||
folder="/Users/rmontanana/Code/covbench",
|
||||
description="Experiments with COVID-19 dataset",
|
||||
)
|
||||
db.session.add(b)
|
||||
u = User(
|
||||
username="rmontanana",
|
||||
email="rmontanana@gmail.com",
|
||||
admin=True,
|
||||
benchmark_id=1,
|
||||
)
|
||||
u.set_password("patito")
|
||||
u1 = User(
|
||||
username="guest",
|
||||
email="guest@example.com",
|
||||
admin=False,
|
||||
benchmark_id=1,
|
||||
)
|
||||
u1.set_password("patata")
|
||||
db.session.add(b)
|
||||
db.session.add(u)
|
||||
db.session.add(u1)
|
||||
db.session.commit()
|
35
tests/test_login.py
Normal file
35
tests/test_login.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import pytest
|
||||
from urllib.parse import urlparse
|
||||
from flask import session, g
|
||||
|
||||
|
||||
def test_login(client, auth):
|
||||
assert client.get("/login").status_code == 200
|
||||
response = auth.login()
|
||||
assert response.headers["Location"] == "/index"
|
||||
auth.logout()
|
||||
|
||||
response = auth.login(username="rmontanana", password="patito")
|
||||
assert response.headers["Location"] == "/index"
|
||||
|
||||
with client:
|
||||
client.get("/index")
|
||||
assert session["_user_id"] == "1"
|
||||
assert g._login_user.username == "rmontanana"
|
||||
auth.logout()
|
||||
|
||||
|
||||
def test_login_invalid(client, auth):
|
||||
response = auth.login(
|
||||
username="rmontanana", password="patato", follow_redirects=True
|
||||
)
|
||||
assert b"Invalid username or password" in response.data
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_logout(client, auth):
|
||||
auth.login()
|
||||
|
||||
with client:
|
||||
auth.logout()
|
||||
assert "user_id" not in session
|
12
tests/test_main.py
Normal file
12
tests/test_main.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from beflask import app
|
||||
|
||||
|
||||
def test_config():
|
||||
assert not app.create_app()[1].testing
|
||||
assert app.create_app("testing")[1].testing
|
||||
|
||||
|
||||
def test_index(client):
|
||||
response = client.get("/")
|
||||
# check image is in the response
|
||||
assert b"img/robert-lukeman-_RBcxo9AU-U-unsplash.jpg" in response.data
|
Reference in New Issue
Block a user