def client(app): from backend.models import db with app.test_client() as client: with app.app_context(): db.create_all() yield client
def init_db(): app = get_app() db.init_app(app) db.create_all() db.session.commit() init_database(db) yield db db.drop_all()
def initialize_db(self, app): with app.app_context(): # Create the tables db.create_all() # Populate the stock info table if not db.session.query(StockInfo).first(): for stock in self.stocks: if not StockInfo.query.filter( StockInfo.symbol == stock[0]).first(): db.session.add( StockInfo(symbol=stock[0], name=stock[1])) db.session.commit()
def create_tables(): db.create_all() try: user = User( username=os.environ.get("ADMIN_BOT_USER"), email="*****@*****.**", password=os.environ.get("ADMIN_BOT_PASSWORD"), about="", ) db.session.add(user) db.session.commit() except Exception as e: print(str(e), flush=True)
def setUp(self): """ Executed before each test case. Place to define test variables required for test cases. :return: """ from sqlalchemy_utils import create_database, database_exists app.config['TESTING'] = True self.database_uri = DATABASE_URI # db.create_all(app=app) self.client = app.test_client() self.app = app if 'test' not in self.database_uri: self.skipTest(reason='Cannot create db!!!!') if not database_exists(self.database_uri): create_database(self.database_uri) db.create_all() self.req_content_type = 'application/json'
@manager.command def runserver(port=5000, bindhost='127.0.0.1'): "start the development server" class SilentWSGIRequestHandler(WSGIRequestHandler): def log_request(self, *args, **kwargs): pass run_simple(bindhost, port, app, request_handler=SilentWSGIRequestHandler, use_reloader=app.debug, use_debugger=app.debug) @manager.command def recreatedb(): "destroy the database (if any) and recreate it" try: reset_database(settings.SQLALCHEMY_DATABASE_URI) except DatabaseControlError, error: print("failed resetting database: %s" % (error, )) with app.app_context(): db.create_all() if __name__ == "__main__": manager.run()
def create_schema(): db.create_all()
def setup_test_environment(): app = create_app("test/database.db") app.app_context().push() db.drop_all() db.create_all()
# import our code after initialization of app from config.log import setup_logging from backend.models import db setup_logging('scss') manager = Manager(app) manager.add_command('assets', ManageAssets()) @manager.command def runserver(port=5000, bindhost='127.0.0.1'): "start the development server" class SilentWSGIRequestHandler(WSGIRequestHandler): def log_request(self, *args, **kwargs): pass run_simple(bindhost, port, app, request_handler=SilentWSGIRequestHandler, use_reloader=app.debug, use_debugger=app.debug) @manager.command def recreatedb(): "destroy the database (if any) and recreate it" try: reset_database(settings.SQLALCHEMY_DATABASE_URI) except DatabaseControlError, error: print("failed resetting database: %s" % (error,)) with app.app_context(): db.create_all() if __name__ == "__main__": manager.run()
def createdb(): """ Creates a database with all of the tables defined in your SQLAlchemy models """ db.create_all()
def initdb(): # for table in reversed(db.metadata.sorted_tables): # db.engine.execute(table.delete()) click.echo('Dropping db') db.drop_all() click.echo('Creating db') db.create_all() click.echo('Creating dummy data') # User user = User(email='*****@*****.**', first_name='Test', last_name='User', registered_at=datetime.datetime.now(), is_invite=False) user.password = '******' db.session.add(user) # Teams team1 = Team(name='Beer drinking club', slug='beer-drinkers') db.session.add(team1) team2 = Team(name='Sandwich club', slug='bread-eaters') db.session.add(team2) # Team Memberships TeamMembership(user=user, team=team1, is_admin=True) TeamMembership(user=user, team=team2, is_admin=True) # Persons persons = [ Person(team=team1, name='Pekka Piikkaaja'), Person(team=team1, name='Osku Oskunen'), Person(team=team1, name='Mira Miranen'), Person(team=team2, name='Mikko Jokunen'), Person(team=team2, name='Jaska Unkuri'), ] for i in range(10): persons.append(Person(team=team1, name='Testi Nro{}'.format(i))) persons.append(Person(team=team2, name='Testi Nro{}'.format(i))) for person in persons: db.session.add(person) # TabTypes db.session.add(TabType(name='Pilsner', price=1.5, team=team1)) db.session.add(TabType(name='IPA', price=2.5, team=team1)) db.session.add(TabType(name='Premium cider', price=3.5, team=team1)) db.session.add(TabType(name='Small bread', price=1, team=team2)) db.session.add(TabType(name='Sandwich', price=3, team=team2)) db.session.add(TabType(name='Burger', price=5, team=team2)) # TabItems for i in range(1000): price = int(random.randint(1, 5)) amount = int(random.randint(1, 5)) person = persons[i % 4] db.session.add( TabItem(name=['Item 1', 'Item 2', 'Item 3'][i % 3], price=price, amount=amount, person=person, adder=user, added_at=(datetime.datetime.now() - datetime.timedelta(minutes=i)), team=person.team)) db.session.commit() click.echo('Done')
app = Flask(__name__) # Blue prints app.register_blueprint(session_manager.bp) app.register_blueprint(index.bp) app.register_blueprint(authentication.bp) app.register_blueprint(community.bp) # SQL stuff app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///{}".format( dbname) # Test database for now app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # Initialize database session db.init_app(app) # Fixed endpoint for / app.add_url_rule("/", endpoint="index") app.add_url_rule("/community", endpoint="community") # FIXME: change the secret key! app.secret_key = "".join( [choice(ascii_letters + digits) for c in range(128)]) return app # FIXME: this is a horrible way to initialize the database # remove this once the application becomes more mature and stable if __name__ == "__main__": if len(sys.argv) == 2 and sys.argv[1] == "init-dev-db": # Create the development database app = create_app() app.app_context().push() db.drop_all() db.create_all(app=app)