Skip to content

Commit

Permalink
Use pytest fixtures instead of setup functions
Browse files Browse the repository at this point in the history
This allows us to have (almost) a single place where we have to think
about Babel (in the `babel`) fixture.

This should make it easier for us to, in the future, run tests without
babel or flask-babel installed, to validate flask-admin for users
without that package. This is important because the `flask_admin.babel`
module has two clearly different codepaths depending on whether the
package is installed.

Importantly, because of the fixture chaining (`admin` requires `babel`),
it also means that `babel` (if available) is always configured for every
flask app instance for every test.
  • Loading branch information
samuelhwilliams committed Jul 17, 2024
1 parent d9a929f commit 4c723b2
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 602 deletions.
30 changes: 30 additions & 0 deletions flask_admin/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from flask import Flask

from flask_admin import Admin


@pytest.fixture(scope='function')
def app():
app = Flask(__name__)
app.config['SECRET_KEY'] = '1'
app.config['WTF_CSRF_ENABLED'] = False

yield app


@pytest.fixture
def babel(app):
try:
from flask_babel import Babel
_ = Babel(app)
except ImportError:
pass

yield babel


@pytest.fixture
def admin(app, babel):
admin = Admin(app)
yield admin
11 changes: 0 additions & 11 deletions flask_admin/tests/fileadmin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
from flask import Flask
from flask_admin import Admin


def setup():
app = Flask(__name__)
app.config['SECRET_KEY'] = '1'
app.config['CSRF_ENABLED'] = False

admin = Admin(app)
return app, admin
41 changes: 27 additions & 14 deletions flask_admin/tests/fileadmin/test_fileadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ def fileadmin_class(self):
def fileadmin_args(self):
raise NotImplementedError

def test_file_admin(self):
def test_file_admin(self, app, admin):
fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()

app, admin = setup()

class MyFileAdmin(fileadmin_class):
editable_extensions = ('txt',)

Expand Down Expand Up @@ -129,10 +127,8 @@ class MyFileAdmin(fileadmin_class):
assert 'path=dummy_renamed_dir' not in rv.data.decode('utf-8')
assert 'path=dummy.txt' in rv.data.decode('utf-8')

def test_modal_edit(self):
# bootstrap 2 - test edit_modal
app_bs2 = Flask(__name__)
admin_bs2 = Admin(app_bs2, template_mode="bootstrap2")
def test_modal_edit_bs2(self, app, babel):
admin_bs2 = Admin(app, template_mode="bootstrap2")

fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()
Expand All @@ -156,7 +152,7 @@ class EditModalOff(fileadmin_class):
admin_bs2.add_view(edit_modal_on)
admin_bs2.add_view(edit_modal_off)

client_bs2 = app_bs2.test_client()
client_bs2 = app.test_client()

# bootstrap 2 - ensure modal window is added when edit_modal is
# enabled
Expand All @@ -171,14 +167,32 @@ class EditModalOff(fileadmin_class):
data = rv.data.decode('utf-8')
assert 'fa_modal_window' not in data

# bootstrap 3
app_bs3 = Flask(__name__)
admin_bs3 = Admin(app_bs3, template_mode="bootstrap3")
def test_modal_edit_bs3(self, app, babel):
admin_bs3 = Admin(app, template_mode="bootstrap3")

fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()

class EditModalOn(fileadmin_class):
edit_modal = True
editable_extensions = ('txt',)

class EditModalOff(fileadmin_class):
edit_modal = False
editable_extensions = ('txt',)

on_view_kwargs = dict(fileadmin_kwargs)
on_view_kwargs.setdefault('endpoint', 'edit_modal_on')
edit_modal_on = EditModalOn(*fileadmin_args, **on_view_kwargs)

off_view_kwargs = dict(fileadmin_kwargs)
off_view_kwargs.setdefault('endpoint', 'edit_modal_off')
edit_modal_off = EditModalOff(*fileadmin_args, **off_view_kwargs)

admin_bs3.add_view(edit_modal_on)
admin_bs3.add_view(edit_modal_off)

client_bs3 = app_bs3.test_client()
client_bs3 = app.test_client()

# bootstrap 3 - ensure modal window is added when edit_modal is
# enabled
Expand All @@ -201,10 +215,9 @@ def fileadmin_class(self):
def fileadmin_args(self):
return (self._test_files_root, '/files'), {}

def test_fileadmin_sort_bogus_url_param(self):
def test_fileadmin_sort_bogus_url_param(self, app, admin):
fileadmin_class = self.fileadmin_class()
fileadmin_args, fileadmin_kwargs = self.fileadmin_args()
app, admin = setup()

class MyFileAdmin(fileadmin_class):
editable_extensions = ('txt',)
Expand Down
20 changes: 0 additions & 20 deletions flask_admin/tests/geoa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
from flask import Flask
from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy


def setup():
app = Flask(__name__)
app.config['SECRET_KEY'] = '1'
app.config['CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost/flask_admin_test'
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy()
db.init_app(app)
admin = Admin(app)

app.app_context().push()

return app, db, admin
24 changes: 24 additions & 0 deletions flask_admin/tests/geoa/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy


@pytest.fixture
def db():
db = SQLAlchemy()
yield db


@pytest.fixture
def admin(app, babel, db):
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost/flask_admin_test'
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db.init_app(app)

app.app_context().push()

admin = Admin(app)
yield admin
8 changes: 2 additions & 6 deletions flask_admin/tests/geoa/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from geoalchemy2 import Geometry
from geoalchemy2.shape import to_shape

from . import setup


def create_models(db):
class GeoModel(db.Model):
Expand All @@ -28,8 +26,7 @@ def __unicode__(self):


@pytest.mark.filterwarnings("ignore:Please update your type formatter:UserWarning")
def test_model():
app, db, admin = setup()
def test_model(app, db, admin):
GeoModel = create_models(db)
with app.app_context():
db.create_all()
Expand Down Expand Up @@ -128,8 +125,7 @@ def test_model():
assert db.session.query(GeoModel).count() == 0


def test_none():
app, db, admin = setup()
def test_none(app, db, admin):
GeoModel = create_models(db)
with app.app_context():
db.create_all()
Expand Down
17 changes: 0 additions & 17 deletions flask_admin/tests/mongoengine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
from flask import Flask
from flask_admin import Admin
from flask_mongoengine import MongoEngine


def setup():
app = Flask(__name__)
app.config['SECRET_KEY'] = '1'
app.config['CSRF_ENABLED'] = False
app.config['MONGODB_SETTINGS'] = {'DB': 'tests'}

db = MongoEngine()
db.init_app(app)

admin = Admin(app)

return app, db, admin
20 changes: 20 additions & 0 deletions flask_admin/tests/mongoengine/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from flask_admin import Admin
from flask_mongoengine import MongoEngine


@pytest.fixture
def db():
db = MongoEngine()
yield db


@pytest.fixture
def admin(app, babel, db):
app.config['MONGODB_SETTINGS'] = {'DB': 'tests'}

db.init_app(app)

admin = Admin(app)
yield admin
Loading

0 comments on commit 4c723b2

Please sign in to comment.