def app_fixture(): app.config['SQLALCHEMY_DATABASE_URI'] = TEST_DATABASE_URI app.config['TESTING'] = True client = app.test_client() with app.app_context(): db.init_app(app) app.app_context().push() db.create_all() yield client os.remove("app/test_project.db")
def create_user(): def create_and_add_user(email): password = prompt('Enter password') user = user_datastore.create_user(email=email, password=password) super_user = prompt_bool('Should user be admin?') if super_user: super_user_role = user_datastore.find_or_create_role('superuser') user_datastore.add_role_to_user(user, super_user_role) else: user_role = user_datastore.find_or_create_role('user') user_datastore.add_role_to_user(user, user_role) db.session.add(user) db.session.commit() print('New credentials are {}/{}'.format(email, password)) with app.app_context(): email = prompt('Enter email') user = user_datastore.find_user(email=email) if user: if prompt_bool('User already exists. Override?'): user_datastore.delete_user(user) db.session.commit() create_and_add_user(email) else: print('Exiting...') exit(0) else: create_and_add_user(email)
def send_async_email(email_data): msg = Message(email_data['subject'], sender='*****@*****.**', recipients=[email_data['to']]) msg.body = email_data['body'] with app.app_context(): mail.send(msg)
def _set_system(self): from app.main import app with app.app_context(): try: cpu_stat = system.get_cpu_stat() io_counters = system.get_io_counters() system_info = { 'loadavg': system.get_loadavg(), 'intensive_processes': system.get_intensive_processes(), # 'pstree': system.get_pstree(), 'mem_info': system.get_mem_info() } with self.lock: self._set_stat(cpu_stat) self._set_io(io_counters) system_info['cpu_stat'] = self._compute_last_stat( self.cpu_stat) system_info['io_counters'] = self._compute_io_counters( self.io_counters) self.system_info = system_info except Exception as e: app.logger.exception(str(e))
def test_log_file_not_found_is_none(self): with app.app_context(): res = LogParser.get_stats("./bad/file/path") self.assertIsNone(res)
def test_log_parser_works_with_cache(self): with app.app_context(): # init log_file_path = "./app/tests/logfile" LogParser.init(log_file_path) # cache and data cache = LogParser.cache[log_file_path] self.assertTrue(cache) actual_log_file_path = cache.log_file_path res = LogParser.get_stats(log_file_path) self.assertEqual(log_file_path, actual_log_file_path) actual_top_10_hosts = cache.top_10_hosts expected_top_10_hosts = { "unicomp6.unicomp.net": 6, "burger.letters.com": 5, "199.120.110.21": 4, "d104.aa.net": 3, "129.94.144.152": 2, "199.72.81.55": 1 } self.assertDictEqual(expected_top_10_hosts, actual_top_10_hosts) actual_codes_first_week = cache.codes_first_week expected_codes_first_week = { "01/Jul": { "2xx": 0, "3xx": 1, "4xx": 1, "5xx": 0 }, "02/Jul": { "2xx": 1, "3xx": 1, "4xx": 1, "5xx": 0 }, "03/Jul": { "2xx": 1, "3xx": 1, "4xx": 0, "5xx": 0 }, "04/Jul": { "2xx": 1, "3xx": 1, "4xx": 0, "5xx": 0 }, "05/Jul": { "2xx": 1, "3xx": 0, "4xx": 1, "5xx": 1 }, "06/Jul": { "2xx": 0, "3xx": 1, "4xx": 0, "5xx": 0 }, "07/Jul": { "2xx": 1, "3xx": 1, "4xx": 0, "5xx": 0 } } self.assertDictEqual(expected_codes_first_week, actual_codes_first_week) # json response res_json = res.get_as_json() self.assertEqual(200, res_json.status_code) self.assertEqual("application/json", res_json.mimetype)
def client(): with app.test_client() as client: with app.app_context(): # context config pass yield client
from app.main import app from app import db if __name__ == "__main__": with app.app_context(): db.create_all() app.run(debug=True)
#!/usr/bin/env python from app.main import update_averages, app with app.app_context(): update_averages()
def pre_run(): with app.app_context(): db.init_app(app) user_service.create_admin()
import unittest from flask_migrate import Migrate, MigrateCommand from flask_script import Manager from app import blueprint from app.main import db, app # noinspection PyUnresolvedReferences from app.main.controller import auth_controller, user_controller, track_controller, room_controller from app.main.service import user_service app.register_blueprint(blueprint) app.app_context().push() manager = Manager(app) migrate = Migrate(app, db) manager.add_command('db', MigrateCommand) def pre_run(): with app.app_context(): db.init_app(app) user_service.create_admin() @manager.command def run(): pre_run()
def play_next_callback(self, playlist_id: int): with app.app_context(): self.play_next(playlist_id)