added tests/

this runs off pytest
currently we only tested the CLI commands
This commit is contained in:
dogeystamp 2023-03-10 11:05:22 -05:00
parent 950265e013
commit 6afe0cc521
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38
7 changed files with 88 additions and 18 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ __pycache__/
.env
config.yml
instance/
.coverage

View File

@ -1,17 +1,27 @@
attrs==22.2.0
bcrypt==4.0.1
click==8.1.3
coverage==7.2.1
exceptiongroup==1.1.0
Flask==2.2.3
Flask-Bcrypt==1.0.1
Flask-Cors==3.0.10
Flask-Script==2.0.6
Flask-SQLAlchemy==3.0.3
Flask-Testing==0.8.1
greenlet==2.0.2
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
packaging==23.0
pluggy==1.0.0
PyJWT==2.6.0
pytest==7.2.2
pytest-cov==4.0.0
PyYAML==6.0
six==1.16.0
SQLAlchemy==2.0.5.post1
tomli==2.0.1
typing_extensions==4.5.0
Werkzeug==2.2.3

View File

@ -35,14 +35,9 @@ def create_user(admin, username, password):
manage.create_user(admin, username, password)
@user_cli.command("delete")
@click.option("--username", "selector_type", help="Delete by username.", flag_value="username", default=True)
@click.option("--id", "selector_type", help="Delete by ID.", flag_value="ID", default=True)
@click.argument("selector")
@click.argument("username")
@click.option('--yes', is_flag=True, expose_value=False, prompt=f"Are you sure you want to delete this user?")
def delete_user(selector_type, selector):
if selector_type == "username":
manage.delete_user_by_username(selector)
elif selector_type == "ID":
manage.delete_user_by_id(selector)
def delete_user(username):
manage.delete_user_by_username(username)
app.cli.add_command(user_cli)

View File

@ -18,7 +18,7 @@ def create_user(admin, username, password):
db.session.add(user)
db.session.commit()
else:
raise KeyError(f"User '{username}' already exists (ID: {user.id})")
raise KeyError(f"User '{username}' already exists.")
def delete_user_by_username(username):
user = User.query.filter_by(username=username).first()
@ -28,12 +28,3 @@ def delete_user_by_username(username):
else:
db.session.delete(user)
db.session.commit()
def delete_user_by_id(id):
user = User.query.filter_by(id=id).first()
if not user:
raise KeyError(f"User with ID {id} does not exist.")
else:
db.session.delete(user)
db.session.commit()

0
tests/__init__.py Normal file
View File

34
tests/conftest.py Normal file
View File

@ -0,0 +1,34 @@
import pytest
from click.testing import CliRunner
import os
os.environ["APP_SETTINGS"] = "sachet.server.config.TestingConfig"
from sachet.server import app, db
@pytest.fixture
def flask_app():
"""Flask application with DB already set up and ready."""
with app.test_client() as client:
with app.app_context():
db.drop_all()
db.create_all()
db.session.commit()
yield app.test_client
db.session.remove()
db.drop_all()
@pytest.fixture
def flask_app_bare():
"""Flask application with empty DB."""
with app.test_client() as client:
with app.app_context():
yield client
db.drop_all()
@pytest.fixture
def cli():
"""click's testing fixture"""
return CliRunner()

39
tests/test_cli.py Normal file
View File

@ -0,0 +1,39 @@
import pytest
from sachet.server.commands import create_db, drop_db, create_user, delete_user
from sachet.server import app, db
from sqlalchemy import inspect
from sachet.server.models import User
def test_db(flask_app_bare, cli):
"""Test the CLI's ability to create and drop the DB."""
# make tables
result = cli.invoke(create_db)
assert result.exit_code == 0
assert "users" in inspect(db.engine).get_table_names()
# tear down
result = cli.invoke(drop_db, ["--yes"])
assert result.exit_code == 0
assert "users" not in inspect(db.engine).get_table_names()
def test_user(flask_app, cli):
"""Test the CLI's ability to create then delete a user."""
# create user
result = cli.invoke(create_user, ["--username", "jeff", "--password", "1234"])
assert result.exit_code == 0
assert User.query.filter_by(username="jeff").first() is not None
# create duplicate user
result = cli.invoke(create_user, ["--username", "jeff", "--password", "1234"])
assert isinstance(result.exception, KeyError)
# delete user
result = cli.invoke(delete_user, ["--yes", "jeff"])
assert result.exit_code == 0
assert User.query.filter_by(username="jeff").first() is None
# delete non-existent user
result = cli.invoke(delete_user, ["--yes", "jeff"])
assert isinstance(result.exception, KeyError)