# Path: pypi_portal/application.py
# def create_app(config_obj, no_sql=False):
#     """Flask application factory. Initializes and returns the Flask application.
# 
#     Blueprints are registered in here.
# 
#     Modeled after: http://flask.pocoo.org/docs/patterns/appfactories/
# 
#     Positional arguments:
#     config_obj -- configuration object to load into app.config.
# 
#     Keyword arguments:
#     no_sql -- does not run init_app() for the SQLAlchemy instance. For Celery compatibility.
# 
#     Returns:
#     The initialized Flask application.
#     """
#     # Initialize app. Flatten config_obj to dictionary (resolve properties).
#     app = Flask(__name__, template_folder=TEMPLATE_FOLDER, static_folder=STATIC_FOLDER)
#     config_dict = dict([(k, getattr(config_obj, k)) for k in dir(config_obj) if not k.startswith('_')])
#     app.config.update(config_dict)
# 
#     # Import DB models. Flask-SQLAlchemy doesn't do this automatically like Celery does.
#     with app.app_context():
#         for module in app.config.get('DB_MODELS_IMPORTS', list()):
#             import_module(module)
# 
#     # Setup redirects and register blueprints.
#     app.add_url_rule('/favicon.ico', 'favicon', lambda: app.send_static_file('favicon.ico'))
#     for bp in all_blueprints:
#         import_module(bp.import_name)
#         app.register_blueprint(bp)
# 
#     # Initialize extensions/add-ons/plugins.
#     if not no_sql:
#         db.init_app(app)
#     Statics(app)  # Enable Flask-Statics-Helper features.
#     redis.init_app(app)
#     celery.init_app(app)
#     mail.init_app(app)
# 
#     # Activate middleware.
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # For filters inside the middleware file.
#     with app.app_context():
#         import_module('pypi_portal.middleware')
# 
#     # Return the application instance.
#     return app
# 
# def get_config(config_class_string, yaml_files=None):
#     """Load the Flask config from a class.
# 
#     Positional arguments:
#     config_class_string -- string representation of a configuration class that will be loaded (e.g.
#         'pypi_portal.config.Production').
#     yaml_files -- List of YAML files to load. This is for testing, leave None in dev/production.
# 
#     Returns:
#     A class object to be fed into app.config.from_object().
#     """
#     config_module, config_class = config_class_string.rsplit('.', 1)
#     config_class_object = getattr(import_module(config_module), config_class)
#     config_obj = config_class_object()
# 
#     # Expand some options.
#     celery_fmt = 'pypi_portal.tasks.{}'
#     db_fmt = 'pypi_portal.models.{}'
#     if getattr(config_obj, 'CELERY_IMPORTS', False):
#         config_obj.CELERY_IMPORTS = [celery_fmt.format(m) for m in config_obj.CELERY_IMPORTS]
#     for definition in getattr(config_obj, 'CELERYBEAT_SCHEDULE', dict()).values():
#         definition.update(task=celery_fmt.format(definition['task']))
#     if getattr(config_obj, 'DB_MODELS_IMPORTS', False):
#         config_obj.DB_MODELS_IMPORTS = [db_fmt.format(m) for m in config_obj.DB_MODELS_IMPORTS]
#     #for script_name, script_file in getattr(config_obj, 'REDIS_SCRIPTS', dict()).items():
#     #    config_obj.REDIS_SCRIPTS[script_name] = os.path.join(REDIS_SCRIPTS_FOLDER, script_file)
# 
#     # Load additional configuration settings.
#     yaml_files = yaml_files or [f for f in [
#         os.path.join('/', 'etc', 'pypi_portal', 'config.yml'),
#         os.path.abspath(os.path.join(APP_ROOT_FOLDER, '..', 'config.yml')),
#         os.path.join(APP_ROOT_FOLDER, 'config.yml'),
#     ] if os.path.exists(f)]
#     additional_dict = dict()
#     for y in yaml_files:
#         with open(y) as f:
#             additional_dict.update(load(f.read()))
# 
#     # Merge the rest into the Flask app config.
#     for key, value in additional_dict.items():
#         setattr(config_obj, key, value)
# 
#     return config_obj
# 
# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):


import xmlrpclib
import pytest
from pypi_portal.application import create_app, get_config
from pypi_portal.extensions import db
        return self.VALUE


@pytest.fixture(autouse=True, scope='session')
def create_all():
    """Create all database tables."""
    db.create_all()


@pytest.fixture(scope='session')
def alter_xmlrpc(request):
    """Replaces the ServerProxy class in the xmlrpclib library with a fake class.

    Class is restored after testing.
    """
    old_method = xmlrpclib.ServerProxy
    xmlrpclib.ServerProxy = FakeServerProxy

    def func(value):
        FakeServerProxy.VALUE = value

    def fin():
        xmlrpclib.ServerProxy = old_method
    request.addfinalizer(fin)

    return func


# Initialize the application and sets the app context to avoid needing 'with app.app_context():'.
# This must happen before any Celery tasks are imported automatically by py.test during test discovery.

# Path: pypi_portal/application.py
# def create_app(config_obj, no_sql=False):
#     """Flask application factory. Initializes and returns the Flask application.
# 
#     Blueprints are registered in here.
# 
#     Modeled after: http://flask.pocoo.org/docs/patterns/appfactories/
# 
#     Positional arguments:
#     config_obj -- configuration object to load into app.config.
# 
#     Keyword arguments:
#     no_sql -- does not run init_app() for the SQLAlchemy instance. For Celery compatibility.
# 
#     Returns:
#     The initialized Flask application.
#     """
#     # Initialize app. Flatten config_obj to dictionary (resolve properties).
#     app = Flask(__name__, template_folder=TEMPLATE_FOLDER, static_folder=STATIC_FOLDER)
#     config_dict = dict([(k, getattr(config_obj, k)) for k in dir(config_obj) if not k.startswith('_')])
#     app.config.update(config_dict)
# 
#     # Import DB models. Flask-SQLAlchemy doesn't do this automatically like Celery does.
#     with app.app_context():
#         for module in app.config.get('DB_MODELS_IMPORTS', list()):
#             import_module(module)
# 
#     # Setup redirects and register blueprints.
#     app.add_url_rule('/favicon.ico', 'favicon', lambda: app.send_static_file('favicon.ico'))
#     for bp in all_blueprints:
#         import_module(bp.import_name)
#         app.register_blueprint(bp)
# 
#     # Initialize extensions/add-ons/plugins.
#     if not no_sql:
#         db.init_app(app)
#     Statics(app)  # Enable Flask-Statics-Helper features.
#     redis.init_app(app)
#     celery.init_app(app)
#     mail.init_app(app)
# 
#     # Activate middleware.
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # For filters inside the middleware file.
#     with app.app_context():
#         import_module('pypi_portal.middleware')
# 
#     # Return the application instance.
#     return app
# 
# def get_config(config_class_string, yaml_files=None):
#     """Load the Flask config from a class.
# 
#     Positional arguments:
#     config_class_string -- string representation of a configuration class that will be loaded (e.g.
#         'pypi_portal.config.Production').
#     yaml_files -- List of YAML files to load. This is for testing, leave None in dev/production.
# 
#     Returns:
#     A class object to be fed into app.config.from_object().
#     """
#     config_module, config_class = config_class_string.rsplit('.', 1)
#     config_class_object = getattr(import_module(config_module), config_class)
#     config_obj = config_class_object()
# 
#     # Expand some options.
#     celery_fmt = 'pypi_portal.tasks.{}'
#     db_fmt = 'pypi_portal.models.{}'
#     if getattr(config_obj, 'CELERY_IMPORTS', False):
#         config_obj.CELERY_IMPORTS = [celery_fmt.format(m) for m in config_obj.CELERY_IMPORTS]
#     for definition in getattr(config_obj, 'CELERYBEAT_SCHEDULE', dict()).values():
#         definition.update(task=celery_fmt.format(definition['task']))
#     if getattr(config_obj, 'DB_MODELS_IMPORTS', False):
#         config_obj.DB_MODELS_IMPORTS = [db_fmt.format(m) for m in config_obj.DB_MODELS_IMPORTS]
#     #for script_name, script_file in getattr(config_obj, 'REDIS_SCRIPTS', dict()).items():
#     #    config_obj.REDIS_SCRIPTS[script_name] = os.path.join(REDIS_SCRIPTS_FOLDER, script_file)
# 
#     # Load additional configuration settings.
#     yaml_files = yaml_files or [f for f in [
#         os.path.join('/', 'etc', 'pypi_portal', 'config.yml'),
#         os.path.abspath(os.path.join(APP_ROOT_FOLDER, '..', 'config.yml')),
#         os.path.join(APP_ROOT_FOLDER, 'config.yml'),
#     ] if os.path.exists(f)]
#     additional_dict = dict()
#     for y in yaml_files:
#         with open(y) as f:
#             additional_dict.update(load(f.read()))
# 
#     # Merge the rest into the Flask app config.
#     for key, value in additional_dict.items():
#         setattr(config_obj, key, value)
# 
#     return config_obj
# 
# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):


# Path: tests/conftest.py
import xmlrpclib
import pytest
from pypi_portal.application import create_app, get_config
from pypi_portal.extensions import db

        return self.VALUE


@pytest.fixture(autouse=True, scope='session')
def create_all():
    """Create all database tables."""
    db.create_all()


@pytest.fixture(scope='session')
def alter_xmlrpc(request):
    """Replaces the ServerProxy class in the xmlrpclib library with a fake class.

    Class is restored after testing.
    """
    old_method = xmlrpclib.ServerProxy
    xmlrpclib.ServerProxy = FakeServerProxy

    def func(value):
        FakeServerProxy.VALUE = value

    def fin():
        xmlrpclib.ServerProxy = old_method
    request.addfinalizer(fin)

    return func


# Initialize the application and sets the app context to avoid needing 'with app.app_context():'.
# This must happen before any Celery tasks are imported automatically by py.test during test discovery.

create_app(get_config('pypi_portal.config.Testing')).app_context().push()