def test_scores_with_generate(self, generate=False): if generate: db.drop_all() db.create_all() generate.seed() self.login('*****@*****.**') else: backup = Backup.query.filter_by(submitter_id=self.user1.id, submit=True).first() score = Score(backup_id=backup.id, kind="Composition", score=2.0, message="Good work", assignment_id=self.assignment.id, user_id=backup.submitter_id, grader=self.staff1) db.session.add(score) db.session.commit() self.login(self.staff1.email) endpoint = '/admin/course/1/assignments/1/scores' response = self.client.get(endpoint) self.assert_200(response) csv_rows = list(csv.reader(StringIO(str(response.data, 'utf-8')))) scores = Score.query.filter_by(assignment_id=1).all() backup_creators = [] for s in scores: backup_creators.extend(s.backup.owners()) self.assertEquals(len(backup_creators), len(csv_rows) - 1)
def database(app, request): """Session-wide test database.""" def teardown(): try: db.engine.execute('DROP TABLE vulnerability CASCADE') except Exception: pass try: db.engine.execute('DROP TABLE vulnerability_template CASCADE') except Exception: pass db.drop_all() # Disable check_vulnerability_host_service_source_code constraint because # it doesn't work in sqlite vuln_constraints = db.metadata.tables['vulnerability'].constraints vuln_constraints.remove(next( constraint for constraint in vuln_constraints if constraint.name == 'check_vulnerability_host_service_source_code')) db.app = app db.create_all() request.addfinalizer(teardown) return db
def _create_tables(self, conn_string): print('Creating tables') from server.models import db current_app.config['SQLALCHEMY_DATABASE_URI'] = conn_string try: db.create_all() except OperationalError as ex: if 'could not connect to server' in ex.message: print('ERROR: {red}PostgreSQL service{white} is not running. Please verify that it is running in port 5432 before executing setup script.'.format(red=Fore.RED, white=Fore.WHITE)) sys.exit(1) elif 'password authentication failed' in ex.message: print('ERROR: ') sys.exit(1) else: raise except ProgrammingError as ex: print(ex) print('Please check postgres user permissions.') sys.exit(1) except ImportError as ex: if 'psycopg2' in ex: print( 'ERROR: Missing python depency {red}psycopg2{white}. Please install it with {blue}pip install psycopg2'.format(red=Fore.RED, white=Fore.WHITE, blue=Fore.BLUE)) sys.exit(1) else: raise else: from alembic.config import Config from alembic import command alembic_cfg = Config(os.path.join(os.getcwd(), 'alembic.ini')) command.stamp(alembic_cfg, "head")
def _create_tables(self, conn_string): print('Creating tables') from server.models import db current_app.config['SQLALCHEMY_DATABASE_URI'] = conn_string try: db.create_all() except OperationalError as ex: if 'could not connect to server' in ex.message: print( 'ERROR: {red}PostgreSQL service{white} is not running. Please verify that it is running in port 5432 before executing setup script.' .format(red=Fore.RED, white=Fore.WHITE)) sys.exit(1) elif 'password authentication failed' in ex.message: print('ERROR: ') sys.exit(1) else: raise except ProgrammingError as ex: print(ex) print('Please check postgres user permissions.') sys.exit(1) except ImportError as ex: if 'psycopg2' in ex: print( 'ERROR: Missing python depency {red}psycopg2{white}. Please install it with {blue}pip install psycopg2' .format(red=Fore.RED, white=Fore.WHITE, blue=Fore.BLUE)) sys.exit(1) else: raise else: from alembic.config import Config from alembic import command alembic_cfg = Config(os.path.join(os.getcwd(), 'alembic.ini')) command.stamp(alembic_cfg, "head")
def createdb(): """ Creates a database with all of the tables defined in your SQLAlchemy models """ db.create_all() directory = os.path.join('.', 'initdata', 'timeseries') for root, dirs, files in os.walk(directory): for file in files: symbol = file.split('.')[0] with open(os.path.join(root, file), 'r') as f: reader = csv.reader(f) next(reader, None) for row in reader: mock = MockTimeSeries(symbol, *row) db.session.add(mock) db.session.flush() db.session.commit() with open(os.path.join('.', 'initdata', 'portfolio', 'mock_portfolio.csv')) as f: reader = csv.reader(f) for row in reader: amount = random.randint(100, 500) symbol, company, industry = row portfolio = Portfolio( symbol, company, industry, 100 * amount, MockTimeSeries.query.filter_by( symbol=row[0].lower()).order_by('time').all()[0].close) print(portfolio.symbol) db.session.add(portfolio) db.session.commit()
def recreate_db(): """ Recreates a local database. Do not use in prod """ db.drop_all() db.create_all() db.session.commit()
def database(app, request): """Session-wide test database.""" def teardown(): if db.engine.dialect.name == 'sqlite': # since sqlite was created in a temp file we skip the drops. return try: db.engine.execute('DROP TABLE vulnerability CASCADE') except Exception: pass try: db.engine.execute('DROP TABLE vulnerability_template CASCADE') except Exception: pass db.drop_all() # Disable check_vulnerability_host_service_source_code constraint because # it doesn't work in sqlite vuln_constraints = db.metadata.tables['vulnerability'].constraints try: vuln_constraints.remove( next(constraint for constraint in vuln_constraints if constraint.name == 'check_vulnerability_host_service_source_code')) except StopIteration: pass db.init_app(app) db.create_all() request.addfinalizer(teardown) return db
def create_db(): """ Creates the db tables. """ db.create_all() populate_roles() create_admin_user()
def test_scores_with_generate(self, generate=False): if generate: db.drop_all() db.create_all() generate.seed() self.login('*****@*****.**') else: backup = Backup.query.filter_by(submitter_id=self.user1.id, submit=True).first() score = Score(backup_id=backup.id, kind="Composition", score=2.0, message="Good work", assignment_id=self.assignment.id, user_id=backup.submitter_id, grader=self.staff1) db.session.add(score) db.session.commit() self.login(self.staff1.email) endpoint = '/admin/course/1/assignments/1/scores.csv' response = self.client.get(endpoint) self.assert_200(response) csv_rows = list(csv.reader(StringIO(str(response.data, 'utf-8')))) scores = Score.query.filter_by(assignment_id=1).all() backup_creators = [] for s in scores: backup_creators.extend(s.backup.owners()) self.assertEquals(len(backup_creators), len(csv_rows) - 1)
def database(app, request): """Session-wide test database.""" def teardown(): try: db.engine.execute('DROP TABLE vulnerability CASCADE') except Exception: pass try: db.engine.execute('DROP TABLE vulnerability_template CASCADE') except Exception: pass db.drop_all() # Disable check_vulnerability_host_service_source_code constraint because # it doesn't work in sqlite vuln_constraints = db.metadata.tables['vulnerability'].constraints vuln_constraints.remove( next(constraint for constraint in vuln_constraints if constraint.name == 'check_vulnerability_host_service_source_code')) db.app = app db.create_all() request.addfinalizer(teardown) return db
def create_app() -> Flask: """Creates and returns the Flask WSGI application and initializes helping components""" # Initialize json support for wtforms wtforms_json.init() # Define the WSGI Application object app = Flask(__name__, template_folder="../../", static_folder="../../static") # Configurations app.config.from_object('server.config') # Initialize database with application db.init_app(app) with app.test_request_context(): db.create_all() # Initialize login manager with application login_manager.init_app(app) # Setup the routes define_routes(app) return app
def _init_database(app): db_path = os.path.join(app.instance_path, "server.sqlite") app.config.from_mapping(SECRET_KEY='SECRET_KEY', SQLALCHEMY_DATABASE_URI='sqlite:///' + db_path, SQLALCHEMY_TRACK_MODIFICATIONS=False) with app.app_context(): db.init_app(app) db.create_all()
def client(): app.config['TESTING'] = True test_client = app.test_client() with app.app_context(): db.drop_all() db.create_all() yield test_client
def client(): app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['TESTING'] = True test_client = app.test_client() with app.app_context(): db.drop_all() db.create_all() yield test_client
def setUp(self): self.app = server.create_app(TestConfig) self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.test_user = User('test', '*****@*****.**', 'password') db.session.add(self.test_user) db.session.commit() self.client = self.app.test_client()
def _db(database, app): """ Provide the transactional fixtures with access to the database via a Flask-SQLAlchemy database connection. """ app.config["SQLALCHEMY_DATABASE_URI"] = TEST_DATABASE_URL with app.app_context(): db.create_all() yield db db.session.remove() db.drop_all()
def initApiServer(conf): # load config app.config.from_object(conf) # init flask sqlalchemy db.app = app db.init_app(app) db.create_all() # init API endpoints manager = APIManager(app, flask_sqlalchemy_db=db) createApi(manager) return app
def client(request): app = create_app('server.settings.TestingConfig') client = app.test_client() db.app = app db.create_all() if getattr(request.module, "create_user", True): admin = User('admin', 'pass') db.session.add(admin) db.session.commit() yield client db.session.remove() db.drop_all()
def database(app, request): """Session-wide test database.""" # Disable check_vulnerability_host_service_source_code constraint because # it doesn't work in sqlite vuln_constraints = db.metadata.tables['vulnerability'].constraints try: vuln_constraints.remove( next(constraint for constraint in vuln_constraints if constraint.name == 'check_vulnerability_host_service_source_code')) except StopIteration: pass db.init_app(app) db.create_all() return db
def testapp(request): app = create_app('server.settings.TestConfig', env='dev') client = app.test_client() db.app = app db.create_all() if getattr(request.module, "create_user", True): admin = User('admin', 'supersafepassword') db.session.add(admin) db.session.commit() def teardown(): db.session.remove() db.drop_all() request.addfinalizer(teardown) return client
def testapp(request): app = create_app('server.settings.TestConfig') client = app.test_client() db.app = app db.create_all() if getattr(request.module, "create_user", True): admin = User('admin', 'supersafepassword') db.session.add(admin) db.session.commit() def teardown(): db.session.remove() db.drop_all() request.addfinalizer(teardown) return client
def create_app(instance_path=None, static_folder='../frontend/build'): """Create and configure an instance of the Flask application.""" app = Flask(__name__, instance_path=instance_path, static_url_path='/', static_folder=static_folder, instance_relative_config=True) try: os.makedirs(app.instance_path) except OSError: pass db_path = os.path.join(app.instance_path, "server.sqlite") app.config.from_mapping(SECRET_KEY='SECRET_KEY', SQLALCHEMY_DATABASE_URI='sqlite:///' + db_path, SQLALCHEMY_TRACK_MODIFICATIONS=False) db.init_app(app) # apply the blueprints to the app from server import auth, api app.register_blueprint(auth.bp) app.register_blueprint(api.bp) @app.errorhandler(ValueError) def http_error_handler(error): return jsonify(code=400, message=str(error)), 400 @app.errorhandler(HTTPException) def http_error_handler(error): return jsonify(code=error.code, message=error.description), error.code @app.route('/') def home(): return redirect('/index.html', code=302) with app.app_context(): db.create_all() return app
def init_db(): """ Initialize database: drop and create all columns """ db.drop_all() db.create_all()
from argparse import ArgumentParser from flask import Flask from server.app import AppConfig from server.models import db """ Creates the database if it does not already exist and creates all the tables inside if it Can be run with the --delete command line flag to empty the existing database """ if __name__ == '__main__': parser = ArgumentParser() parser.add_argument( '--delete', help="DELETES ALL DATA in the database and starts over", action='store_true') drop_all = parser.parse_args().delete app = Flask(__name__) app.config.from_object(AppConfig()) db.init_app(app) with app.app_context(): if drop_all: db.drop_all() db.create_all()
def setUp(self): db.drop_all() db.create_all()
def setUp(self): db.create_all()
def createdb(): """ create datebase""" db.create_all()
from core.output import FileOutput from youtube.videos import Videos from server.models import Actor, db from server.models import Videos as VideosDB, Relationship_Actor_Videos from server.queries import DBYouTube from server.main import app import time import progressbar import os import json # scrap_basic_actors_info() youtube_user = insert_actors_info() video = Videos() app.app_context().push() db.create_all() # create the tables and database with open('config/actors.json') as data_file: actors = json.load(data_file) actors_dict = actors['channels'] no_video_actors = [] with open('config/parameters.json') as data_file: parameters = json.load(data_file)['parameters'] for actor in progressbar.progressbar(actors_dict): channel_id = actor['id'] channel_username = actor['username'] channel_actor = actor['actor'] if channel_id != 'null' and channel_id: directory = 'data/' + YoutubeAPI.start_time
def createdb(): """ Creates a database with all of the tables defined in your SQLAlchemy models """ db.create_all() setup_default()
def create_tables(): db.create_all()