-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use pytest fixtures instead of setup functions
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
1 parent
d9a929f
commit 4c723b2
Showing
26 changed files
with
414 additions
and
602 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,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 |
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,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 | ||
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,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 | ||
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,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 |
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,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 | ||
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,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 |
Oops, something went wrong.