-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Use scoped session db #6979
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,45 @@ | ||
import pytest | ||
from app.models import db as _db | ||
from tests.all.integration.setup_database import ( | ||
Setup, | ||
create_app, | ||
set_settings, | ||
Environment, | ||
) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def app(): | ||
app = create_app() | ||
with app.test_request_context(): | ||
yield app | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def database(app): | ||
_db.create_all() | ||
set_settings(app_name='Open Event', app_environment=Environment.TESTING) | ||
yield _db | ||
Setup.drop_db() | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def connection(database): | ||
with database.engine.connect() as conn: | ||
yield conn | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def db(database, connection): | ||
transaction = connection.begin() | ||
|
||
options = dict(bind=connection, binds={}) | ||
session = database.create_scoped_session(options=options) | ||
old_session = database.session | ||
database.session = session | ||
|
||
yield database | ||
|
||
session.remove() | ||
database.session = old_session | ||
transaction.rollback() |
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,23 +1,7 @@ | ||
import pytest | ||
from flask_login import login_user, logout_user | ||
from app.api.helpers.auth import AuthManager | ||
from app.models import db as database | ||
from app.models.user import User | ||
from tests.all.integration.auth_helper import create_user | ||
from tests.all.integration.setup_database import Setup | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def app(): | ||
app = Setup.create_app() | ||
with app.test_request_context(): | ||
yield app | ||
Setup.drop_db() | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def db(app): | ||
return database | ||
|
||
|
||
def test_load_user(db): | ||
|
@@ -26,7 +10,7 @@ def test_load_user(db): | |
assert user == db.session.query(User).get(user.id) | ||
|
||
|
||
def test_verified_user(): | ||
def test_verified_user(db): | ||
"""Method to test if user is verified""" | ||
|
||
user = create_user(email='[email protected]', password='password') | ||
|
@@ -35,7 +19,7 @@ def test_verified_user(): | |
assert AuthManager.is_verified_user() == False | ||
|
||
|
||
def test_is_accessible(): | ||
def test_is_accessible(db): | ||
"""Method to test if user is accessible(authenticated)""" | ||
|
||
user = create_user(email='[email protected]', password='password') | ||
|
@@ -44,7 +28,7 @@ def test_is_accessible(): | |
assert AuthManager.is_accessible() == False | ||
|
||
|
||
def test_check_auth_admin(): | ||
def test_check_auth_admin(db): | ||
"""Method to test proper authentication & admin rights for a user""" | ||
|
||
user = create_user(email='[email protected]', password='password') | ||
|
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 |
---|---|---|
|
@@ -10,13 +10,18 @@ | |
_basedir = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
|
||
def create_app(): | ||
app.config.from_object('config.TestingConfig') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Black would make changes. |
||
app.logger.addHandler(logging.StreamHandler(sys.stdout)) | ||
app.logger.setLevel(logging.ERROR) | ||
return app | ||
|
||
|
||
class Setup(object): | ||
@staticmethod | ||
def create_app(): | ||
app.config.from_object('config.TestingConfig') | ||
app.logger.addHandler(logging.StreamHandler(sys.stdout)) | ||
app.logger.setLevel(logging.ERROR) | ||
with app.test_request_context(): | ||
test_app = create_app() | ||
with test_app.test_request_context(): | ||
db.create_all() | ||
set_settings(app_name='Open Event', app_environment=Environment.TESTING) | ||
|
||
|
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,13 +1,11 @@ | ||
import factory | ||
|
||
import tests.factories.common as common | ||
from app.models.activity import Activity, db | ||
from app.models.activity import Activity | ||
from tests.factories.base import BaseFactory | ||
|
||
|
||
class ActivityFactory(factory.alchemy.SQLAlchemyModelFactory): | ||
class ActivityFactory(BaseFactory): | ||
class Meta: | ||
model = Activity | ||
sqlalchemy_session = db.session | ||
|
||
actor = common.string_ | ||
action = common.string_ |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import factory | ||
|
||
from app.models import db | ||
|
||
|
||
class BaseFactory(factory.alchemy.SQLAlchemyModelFactory): | ||
class Meta: | ||
abstract = True | ||
sqlalchemy_session = db.session |
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,12 +1,10 @@ | ||
import factory | ||
|
||
import tests.factories.common as common | ||
from app.models.custom_system_role import CustomSysRole, db | ||
from app.models.custom_system_role import CustomSysRole | ||
from tests.factories.base import BaseFactory | ||
|
||
|
||
class CustomSysRoleFactory(factory.alchemy.SQLAlchemyModelFactory): | ||
class CustomSysRoleFactory(BaseFactory): | ||
class Meta: | ||
model = CustomSysRole | ||
sqlalchemy_session = db.session | ||
|
||
name = common.string_ |
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
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,13 +1,11 @@ | ||
import factory | ||
|
||
import tests.factories.common as common | ||
from app.models.event_location import EventLocation, db | ||
from app.models.event_location import EventLocation | ||
from tests.factories.base import BaseFactory | ||
|
||
|
||
class EventLocationFactory(factory.alchemy.SQLAlchemyModelFactory): | ||
class EventLocationFactory(BaseFactory): | ||
class Meta: | ||
model = EventLocation | ||
sqlalchemy_session = db.session | ||
|
||
name = common.string_ | ||
slug = common.slug_ |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Black would make changes.