def forge(): db.create_all() name = "aaa" movies = [ {'title':'功夫之王','year':'2010'}, {'title':'机器之血','year':'2015'}, {'title':'复仇者联盟3','year':'2017'}, {'title':'微微一笑很倾城','year':'2018'}, {'title':'百鸟朝凤','year':'2019'}, {'title':'唐人街探案3','year':'2020'}, {'title':'杀破狼','year':'2010'}, {'title':'扫毒','year':'2010'}, {'title':'机器之血','year':'2010'}, {'title':'分手大师','year':'2010'}, {'title':'这个杀手不太冷','year':'2010'}, {'title':'邻里的人们','year':'2010'}, {'title':'釜山行','year':'2010'}, {'title':'拯救大兵瑞恩','year':'2010'}, {'title':'我的特工爷爷','year':'2010'}, {'title':'战狼','year':'2010'} ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'],year=m['year']) db.session.add(movie) db.session.commit() click.echo('导入数据完成')
def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( BASE_DIR, 'test.db') self.app = app.test_client() db.create_all()
def setUp(self): #调用app app.config.update( #处于测试环境 TESTING=True, #用缓存数据库 memory 内存数据库 SQLALCHEMY_DATABASE_URI="sqlite:///:memory:") # 往库中 db.create_all() user = User(name="Test", username="******") user.set_password("123456") movie = Movie(title="Test Movie Title", year="2020") #add一条上传 add_all 多条列表上传 db.session.add_all([user, movie]) db.session.commit() # 创建一个测试用的客户端模拟 浏览器 self.client = app.test_client() #创建测试命令运行器 self.runner = app.test_cli_runner()
def setUp(self): '''每个测试之前执行''' self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() # Flask内建的测试客户端,模拟浏览器行为
def setUp(self): # Not sure why but this creates test.db in blog subdirectory db_path = os.path.join(os.path.split(__file__)[0], 'test.db') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format(db_path) app.config['TESTING'] = True app.config['SECRET_KEY'] = 'abc123' # Apparently foreign keys get turned on by __init__.py / #self.app = app.test_client() #self.session = self.app.session_transaction() db.create_all() uncategorized = Category(name='Uncategorized', description='Uncategorized', url_name='uncategorized') db.session.add(uncategorized) projects = Category(name='Projects', description='Projects', url_name='projects') db.session.add(projects) password = sha256_crypt.encrypt('password') user = User(first_name='Matthew', last_name='Moisen', email='*****@*****.**', password=password, is_admin=True) db.session.add(user) db.session.commit() self.projects_id = projects.id self.uncategorized_id = uncategorized.id self.user_id = user.id
def initdb(drop): """Initialize the database.""" if drop: db.drop_all() db.create_all() # 输出提示信息 click.echo('Initialized database')
def setUp(self): db.create_all() self.user = User(login='******', password_hash=generate_password_hash('test_password')) db.session.add(self.user) db.session.commit() self.new_user = User.query.filter_by(login='******').first()
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles() self.client = self.app.test_client(use_cookies=True)
def setUpClass(cls): cls.app = app.test_client() db.create_all() test_user = User("TestUser", "*****@*****.**", "password") db.session.add(test_user) test_post = Post("Post Title", "Post Content", test_user) db.session.add(test_post) db.session.commit()
def setUp(self): self.app = create_app(TestingConfig) self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles() self.user = self.register_user() self.post = self.add_post()
def recreate_database(): from blog import db from blog import models db.reflect() # hacky solution # ref: http://jrheard.tumblr.com/post/12759432733/dropping-all-tables-on-postgres-using db.drop_all() db.create_all()
def homepage1(): db.create_all() #从session会话中获取用户名 if 'username' in session: name = session['username'] else: name = 'Human' return render_template('home1.html', name=name)
def setUp(self) -> None: """Setting up application for tests""" self.app = create_app() self.app.config.from_object(TestConfig) self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def test_create_admin(self): db.drop_all() db.create_all() result = self.runner.invoke( args=['admin', '--username', 'test2', '--password', '123456']) self.assertIn('Create admin...', result.output) self.assertIn('Done', result.output) self.assertEqual(User.query.count(), 1) self.assertEqual(User.query.first().username, 'test2') self.assertTrue(User.query.first().validate_password('123456'))
def initdb(drop): """Initialize the database.""" if drop: click.confirm( 'This operation will delete the database, do you want to continue?', abort=True) db.drop_all() click.echo('Drop tables.') db.create_all() click.echo('Initialized database.')
def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, TEST_DB) self.app = app.test_client() db.create_all() self.assertEquals(app.debug, False)
def create_db_default_user(): # Create the user, be sure to create a new user and remove this one immediately name = app.config.get('DEFAULT_NAME', 'admin') password = app.config.get('DEFAULT_PASSWORD', 'admin') password_hash = bcrypt.generate_password_hash(password) default_user = User(name=name, shortname=name, password_hash=password_hash) # Create the db and then add the user in db.create_all() db.session.add(default_user) db.session.commit()
def setUp(self): self.app = create_app(config['testDB']) self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.user = User(username='******', email='*****@*****.**', password='******', image_file='Night view.png') db.session.add(self.user) db.session.commit()
def register(): db.create_all() if request.method == 'GET': return render_template('register.html') else: username = request.form.get('username') password = request.form.get('password') email = request.form.get('email') users = Users(username, password, email) db.session.add(users) db.session.commit() return redirect(url_for('homepage1'))
def init_db(migration): db.drop_all() if migration: # create database using migrations print('applying migration') upgrade() else: # create database from model schema directly db.create_all() db.session.commit() cfg = alembic.config.Config('var/migrations/alembic.ini') alembic.command.stamp(cfg, "head")
def setUp(self): self.app = create_app(config['testDB']) self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.user = User(username='******', email='*****@*****.**', password='******', image_file='Night view.png') db.session.add(self.user) db.session.commit() self.post = Post(title='Random post', date_posted=parser.parse("2020 05 17 12:00AM"), content='Some words', user_id=self.user.id) db.session.add(self.post) db.session.commit()
def forge(): db.create_all() movies = [ { 'title': 'My Neighbor Totoro', 'year': '1988' }, { 'title': 'Dead Poets Society', 'year': '1989' }, { 'title': 'A Perfect World', 'year': '1993' }, { 'title': 'Leon', 'year': '1994' }, { 'title': 'Mahjong', 'year': '1996' }, { 'title': 'Swallowtail Butterfly', 'year': '1996' }, { 'title': 'King of Comedy', 'year': '1999' }, { 'title': 'Devils on the Doorstep', 'year': '1999' }, { 'title': 'WALL-E', 'year': '2008' }, { 'title': 'The Pork of Music', 'year': '2012' }, ] for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() # 输出提示信息 click.echo('Done')
def admin(username, password): db.create_all() user = User.query.first() if user is not None: click.echo('更新用户') user.username = username user.set_password(password) else: click.echo('创建用户') user = User(username=username, name="ZY") user.set_password(password) db.session.add(user) db.session.commit() click.echo('创建管理员账号完成')
def create_db_default_user(): """ Create a default user and all tables for the the DB """ # Create the user, be sure to create a new user and remove this one immediately name = app.config.get('DEFAULT_USER', 'admin') password = app.config.get('DEFAULT_PASSWORD', 'admin') password_hash = bcrypt.generate_password_hash(password) default_user = User(name=name, shortname=name, password_hash=password_hash) # Create the db and then add the user in db.create_all() db.session.add(default_user) db.session.commit()
def setUp(self): # 更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', username='******') user.set_password('123456') movies = Movies(title='test movie title', year=2020) db.session.add_all([user, movies]) db.session.commit() self.client = app.test_client() # 测试客户端 self.runner = app.test_cli_runner() # 创建测试
def setUp(self): app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') db.create_all() user = User(name='Test', username='******') user.set_password('123456') movie = Movie(title='测试电影名称', year='2020') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() self.runner = app.test_cli_runner()
def init_db(): db.create_all() user1 = User(username="******") user1.set_password("test") user2 = User(username="******") user2.set_password("password2") post1 = Post(title="Post by user 1", contents="Contens from user 1", user=user1) post2 = Post(title="Post by user 2", contents="Contens from user 2", user=user2) db.session.add(user1, user2) db.session.add(post1, post2) db.session.commit()
def admin(username, password): """Create user.""" db.create_all() user = User.query.first() if user is not None: click.echo('Updating user...') user.username = username user.set_password(password) else: click.echo('Creating user...') user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo('Done.')
def adduser(email, username): """Regist a new user""" from getpass import getpass password = getpass() password2 = getpass() if password != password2: import sys sys.exit("Create user failed: passwords do not match!") from blog import db from blog.models.user import User db.create_all() user = User(email=email, username=username, password=password, name=username) db.session.add(user) db.session.commit() print("Create user success: {0}".format(username))
def setUp(self): # 更新配置 在开发和测试时候通常配置是不一样 app.config.update( TESTING=True, # 开启测试模式 出错时候不会输出多余的信息 SQLALCHEMY_DATABASE_URI= 'sqlite:///:memory:' # SQLite内存型数据库,不会干扰开发时使用的数据库文件 ) # 创建数据库和表 db.create_all() # 创建测试数据,一个用户,一个电影信息 user = User(name='Test', username='******') user.set_password('123456') movie = Movie(title='测试电影名称', year='2020') db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建测试客户端(模拟客户端请求) self.runner = app.test_cli_runner() # 创建测试命令运行器(触发自定义命令)
def forge(): db.create_all() name = "Bruce" movies = [ { 'title': '湖南农业大学', 'author': 'Yyjingqi', 'content': '博文内容' }, ] user = User(name=name) db.session.add(user) for m in movies: Ariticle = Ariticles(title=m['title'], author=m['author'], content=m['content']) db.session.add(Ariticle) db.session.commit() click.echo('数据导入完成')
def forge(): db.create_all() name = "Bruce" ariticles = [ {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'}, {'title':'如何防治蝗灾','content':'xxxxxxxxx','author':'晓红'} ] user = User(name=name) db.session.add(user) for m in ariticles: ariticle = Ariticles(title=m['title'],content=m['content'],author=m['author']) db.session.add(ariticle) db.session.commit() click.echo('数据导入完成')
def setUp(self): # 更新配置 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据 user = User(name='Test1', username='******') user.set_password('123456') movie = Movie(title='Test Movie Title', year='2019') # 使用add_all()方法一次添加多个模型类实例,传入列表 db.session.add_all([user, movie]) db.session.commit() # 创建测试客户端 self.client = app.test_client() # 创建测试命令运行器 self.runner = app.test_cli_runner()
def setUp(self): app.config['TESTING']=True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') self.app = app.test_client() db.create_all()
def setUp(self): self.app = create_app(TEST_URI) self.client = self.app.test_client() db.create_all()
def init_database(): from blog import db from blog import models db.create_all()
def setUpClass(cls): cls.app = app.test_client() db.create_all() test_category = Category("Test Category") db.session.add(test_category) db.session.commit()
def setUp(self): app.config.from_object('config.TestConfig') self.client = app.test_client() db.create_all()
def tables(): '初始化数据库结构' db.create_all() print '初始化数据库结构...[确定]'
def initdb(): """Creates all database tables.""" db.create_all()
def setUpClass(cls): cls.app = app.test_client() db.create_all()
def setUp(self): blog.config['TESTING'] = True blog.config['CSRF_ENABLED'] = False blog.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(LOG_DIR, 'test.db') self.app = blog.test_client() db.create_all()
from blog import db from blog.models import User #Create DB and tables db.drop_all() db.create_all() kyle = User("Kyle","*****@*****.**", "hong0322") #INSERT User db.session.add(kyle) db.session.add(User("admin","*****@*****.**", "admin")) db.session.commit()