<commit_before>import pytest
import os
from flask import Flask
from typing import Generator
from commandment import create_app
from commandment.models import db as _db
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import scoped_session
from tests.client import MDMClient
from alembic.command import upgrade
from alembic.config import Config

# For testing, every test uses an in-memory database with migrations that are applied in the fixture setup phase.
# This ensures every test is fully isolated.

TEST_DATABASE_URI = 'sqlite:////tmp/test.db'
TEST_DIR = os.path.realpath(os.path.dirname(__file__))
ALEMBIC_CONFIG = os.path.realpath(TEST_DIR + '/alembic_test.ini')
TEST_APP_CONFIG = os.path.realpath(TEST_DIR + '/../travis-ci-settings.cfg')


def apply_migrations(connection):
    """Applies all Alembic migrations."""
    config = Config(ALEMBIC_CONFIG)

    # Issues with running upgrade() in a fixture with SQLite in-memory:
    # https://github.com/miguelgrinberg/Flask-Migrate/issues/153
    # Basically: Alembic always spawns a new connection from upgrade() unless you specify a connection
    # in config.attributes['connection']
    config.attributes['connection'] = connection
    upgrade(config, 'head')


@pytest.yield_fixture(scope='function')
def app() -> Generator[Flask, None, None]:
    """Flask Application Fixture"""
    a = create_app(TEST_APP_CONFIG)
    a.config['TESTING'] = True
    a.config['SQLALCHEMY_DATABASE_URI'] = TEST_DATABASE_URI

    ctx = a.app_context()
    ctx.push()
    yield a
    ctx.pop()


@pytest.yield_fixture(scope='function')
def db(app: Flask) -> Generator[SQLAlchemy, None, None]:
    """Flask-SQLAlchemy Fixture"""
    _db.app = app
    # _db.init_app(app)
    #_db.create_all()

    yield _db

    # _db.drop_all()


@pytest.yield_fixture(scope='function')
def session(db: SQLAlchemy) -> Generator[scoped_session, None, None]:
    """SQLAlchemy session Fixture"""
    connection = _db.engine.connect()
    # transaction = connection.begin()<commit_msg>drop tables after db fixture is released<commit_after>import pytest
import os
from flask import Flask
from typing import Generator
from commandment import create_app
from commandment.models import db as _db
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import scoped_session
from tests.client import MDMClient
from alembic.command import upgrade
from alembic.config import Config

# For testing, every test uses an in-memory database with migrations that are applied in the fixture setup phase.
# This ensures every test is fully isolated.

TEST_DATABASE_URI = 'sqlite:////tmp/test.db'
TEST_DIR = os.path.realpath(os.path.dirname(__file__))
ALEMBIC_CONFIG = os.path.realpath(TEST_DIR + '/alembic_test.ini')
TEST_APP_CONFIG = os.path.realpath(TEST_DIR + '/../travis-ci-settings.cfg')


def apply_migrations(connection):
    """Applies all Alembic migrations."""
    config = Config(ALEMBIC_CONFIG)

    # Issues with running upgrade() in a fixture with SQLite in-memory:
    # https://github.com/miguelgrinberg/Flask-Migrate/issues/153
    # Basically: Alembic always spawns a new connection from upgrade() unless you specify a connection
    # in config.attributes['connection']
    config.attributes['connection'] = connection
    upgrade(config, 'head')


@pytest.yield_fixture(scope='function')
def app() -> Generator[Flask, None, None]:
    """Flask Application Fixture"""
    a = create_app(TEST_APP_CONFIG)
    a.config['TESTING'] = True
    a.config['SQLALCHEMY_DATABASE_URI'] = TEST_DATABASE_URI

    ctx = a.app_context()
    ctx.push()
    yield a
    ctx.pop()


@pytest.yield_fixture(scope='function')
def db(app: Flask) -> Generator[SQLAlchemy, None, None]:
    """Flask-SQLAlchemy Fixture"""
    _db.app = app
    # _db.init_app(app)
    #_db.create_all()

    yield _db
    _db.drop_all()


@pytest.yield_fixture(scope='function')
def session(db: SQLAlchemy) -> Generator[scoped_session, None, None]:
    """SQLAlchemy session Fixture"""
    connection = _db.engine.connect()
    # transaction = connection.begin()
{'fraction_non_alphanumeric': 0.11053668143771542, 'fraction_numerical': 0.0014771048744460858, 'mean_word_length': 4.468371467025572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1309296', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1287, 'n_words': 339}