def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('tutu' + str(i + 1), 'pw' + str(i))) for j in range(0, 10): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add(Comment('这是一条评论' + str(k), 1 + 3 * i + j, i + 1)) db.session.commit() print(1, User.query.all()) print(2, User.query.get(1)) print(3, User.query.filter_by(id=5).first()) print(4, User.query.order_by(User.id.desc()).offset(1).limit(2).all()) print(5, User.query.filter(User.username.endswith('0')).limit(3).all()) print(6, User.query.filter(or_(User.id == 99, User.id == 88)).all()) print(7, User.query.filter(and_(User.id > 88, User.id < 99)).all()) print(8, User.query.filter( and_(User.id > 88, User.id < 90)).first_or_404()) # 如果结果存在的话打印第一个,如果不存在的话,抛出异常 print(9, User.query.order_by(User.id.desc()).paginate( page=2, per_page=10).items) # 分页--显示第二页的10个数据 user = User.query.get(1) print(10, user) image = Image.query.get(1) print(11, image, image.user)
def client_loaded(app): with app.app_context(): client_loaded = app.test_client() db.create_all() password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='Test', last_name='User', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='John', last_name='Smith', email='*****@*****.**', password=password)) db.session.commit() db.session.add( Character(id=1, name='Paladin', is_dead=False, user_id=1)) db.session.add(Character(id=2, name='Rogue', is_dead=False, user_id=1)) db.session.add( Character(id=3, name='Fighter', is_dead=False, user_id=2)) db.session.commit() data = dict(email='*****@*****.**', password='******', remember_me=False) client_loaded.post('/login', data=data) yield client_loaded db.drop_all()
def empty_client(app): with app.app_context(): empty_client = app.test_client() db.create_all() yield empty_client db.drop_all()
def client(app): with app.app_context(): client = app.test_client() db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() yield client db.drop_all()
def client(app): with app.app_context(): client = app.test_client() db.create_all() db.session.add(Player( first_name='Payton', last_name='Young', email='*****@*****.**', is_active=True) ) db.session.add(Party( party_name='Adventure Inc', is_active=True ) ) db.session.add(Character( character_name='Milo Thorngage', character_class='Investigator', is_active=True, is_dead=False, player_id=1, party_id=1 )) db.session.commit() yield client db.drop_all()
def create_database(): from web import db try: db.drop_all() db.create_all() print('[DATABASE] Baza danych została pomyślnie stworzona!') except: print('[DATABASE] Nie udało się stworzyć bazy danych!')
def entry_client(app): """ Fixture with more test data in it """ with app.app_context(): entry_client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=2)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=2)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=2)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=3)) db.session.add( Entry(id=5, game_session=2, description='Spear', amount=50.00, character_id=3)) db.session.add( Entry(id=6, game_session=3, description='Backpack', amount=60.00, character_id=3)) db.session.commit() yield entry_client db.drop_all()
def setUp(self): """ Creates a new database for the unit test to use """ self.app = Flask(__name__) db.init_app(self.app) with self.app.app_context(): db.create_all() self.populate_db() # Your function that adds test data.
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(app.config['BASEDIR'], TEST_DB) self.app = app.test_client() db.drop_all() db.create_all() self.assertEquals(app.debug, False)
def app(): app = create_app('test') app.config.from_object('web.config.TestingConfig') with app.app_context(): # Initialize the db tables db.create_all() yield app # Remove only the users crated here. Dummy users will be left intact User.query.filter(User.email == "*****@*****.**").delete() db.session.commit() return app
def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( BASE_DIR, TEST_DB) self.app = app.test_client() db.create_all() self.user = User('TestUser') self.user.save('testpassword') self.video = Video('TestVideo') self.video.save(hash='Teststring', user=self.user)
def client(app): with app.app_context(): client = app.test_client() db.create_all() db.session.add( Player(first_name='Payton', last_name='Young', email='*****@*****.**', is_active=True)) db.session.commit() yield client db.drop_all()
def init_database(test_client): db.create_all() user1 = models.User(name='user001') user1.password = '******' user2 = models.User(name='*****@*****.**') user2.password = '******' db.session.add(user1) db.session.add(user2) db.session.commit() yield db # this is where the testing happens! db.drop_all()
def client(app): """ Fixture with some basic data """ with app.app_context(): client = app.test_client() db.create_all() # Add an existing user db.session.add( User(id=1, first_name='Adam', last_name='Alpha', email='*****@*****.**', password=generate_password_hash('Monday1', method='sha256'))) db.session.commit() yield client db.drop_all()
def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + TEST_DB_PATH self.client = app.test_client() db.create_all() self.user = User('TestUser') self.user.save('testpassword') self.video = Video('TestVideo') self.video.save(hash='Teststring', user=self.user) self.video_id = self.video.id self.comment = Comment('Text', self.video.id, self.user.id) self.comment.save() self.anonuser = Device() self.anonuser2 = Device() self.room = Room('roomname', self.anonuser.id) self.room.save(self.video.id)
def client(app): """ Fixture with basic data in it """ with app.app_context(): client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() yield client db.drop_all()
def client(): ''' This is an extremely simplified configuration for testing. I spent some hours trying to learn how this can be improved: 1. Most configurations first rely on having a create_app function for the main app while we don't currently have one. 2. After applying that, it seems useful to separate the client creation from the db setup, and then define a session-level setting that allows each test to run with a separate db whereas it is not the case with this configuration. We basically use our app but change its config to use a temporary db. 3. Also, using a temp file as it was before did not work for me, it kept using our original db or throw errors. 4. I am not sure the teardown is clean here, since os.unlink(app.config['SQL..']) did not work when this is set to theh in-memory DB. So that might be another point for importvement. For now I proceeded with creating some tests until someone else gets to have a look or I manage to learn a bit more. ''' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['TESTING'] = True app.config['SECRET_KEY'] = 'test_key' with app.test_client() as client: db.create_all() yield client db.drop_all()
def client(app): with app.app_context(): client = app.test_client() db.create_all() password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='Test', last_name='User', email='*****@*****.**', password=password)) db.session.commit() data = dict(email='*****@*****.**', password='******', remember_me=False) client.post('/login', data=data) yield client db.drop_all()
def empty_client(app): with app.app_context(): empty_client = app.test_client() db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() yield empty_client db.drop_all()
def __init_test_db(): db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() db.session.add(Character(id=1, user_id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, user_id=1, name='Rogue', is_dead=False)) db.session.add(Character(id=3, user_id=2, name='Fighter', is_dead=False)) db.session.commit()
def init(delete_users_db, delete_admin_config_db): """Recreate the db tables except the one specified with cli option""" all_db_except_users_and_config = ['logs'] db.drop_all(all_db_except_users_and_config) db.create_all(all_db_except_users_and_config) if delete_users_db == 'y': db.drop_all('users') db.create_all('users') if delete_admin_config_db == 'y': db.drop_all('admin_config') db.create_all('admin_config') print("Done")
def createDbSchema(): db.create_all()
def client(): db.drop_all() db.create_all() client = app.test_client() yield client
def create_db(): from web import db db.create_all()
#!/usr/bin/python # -*- coding: utf-8 -*- from web import User, Role, Product, Vend, Payment from web import db from datetime import datetime db.drop_all() db.create_all() def add_roles(): user = Role("user") treasurer = Role("treasurer") admin = Role("admin") db.session.add_all([user,treasurer,admin]) db.session.commit() def add_root(): root = User("root", "*****@*****.**", "password", 0, Role.query.all()) db.session.add(root) db.session.commit() def add_users(): bouni = User("Bouni", "*****@*****.**", "password", 0, Role.query.filter(Role.role==u"user").all()) sannny = User("Samuel", "*****@*****.**", "password", 0, Role.query.filter(Role.role==u"user").all()) sniser = User("Sniser", "*****@*****.**", "password", 0, Role.query.filter(Role.role==u"user").all()) manuel = User("Manuel", "*****@*****.**", "password", 0, Role.query.filter(Role.role==u"user").all()) zeno = User("Zeno", "*****@*****.**", "password", 0, Role.query.filter(Role.role==u"user").all()) db.session.add_all([bouni,sannny,sniser,manuel,zeno]) db.session.commit() def add_products():
def __init_test_db(): ''' Add mock db data ''' db.create_all() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add sessions db.session.add(Setting(id=1, user_id=1, key='test_session', value='42')) db.session.add(Setting(id=2, user_id=2, key='test_session', value='99')) # Add some Characters db.session.add(Character(id=1, user_id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, user_id=1, name='Rogue', is_dead=False)) db.session.add(Character(id=3, user_id=2, name='Fighter', is_dead=False)) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=1)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=1)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=1)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=2)) db.session.add( Entry(id=5, game_session=1, description='Spear', amount=50.00, character_id=2)) db.session.add( Entry(id=6, game_session=2, description='Backpack', amount=60.00, character_id=2)) db.session.commit()
from web import db, create_app db.create_all(app=create_app())
def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db/', 'test.db') self.app = app.test_client() db.create_all()
def setUp(self): self.app = create_app() self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def entry_client(app): with app.app_context(): entry_client = app.test_client() db.create_all() # Add some Characters db.session.add(Character(id=1, name='Paladin', is_dead=False)) db.session.add(Character(id=2, name='Rogue', is_dead=False)) db.session.add(Character(id=3, name='Fighter', is_dead=False)) db.session.commit() # Set the current Character db.session.add(Setting(key='current_character', value='2')) db.session.commit() # Add Users password = generate_password_hash('Monday1') db.session.add( User(id=1, first_name='TestA', last_name='UserOne', email='*****@*****.**', password=password)) db.session.add( User(id=2, first_name='TestB', last_name='UserTwo', email='*****@*****.**', password=password)) db.session.commit() # Add some entries db.session.add( Entry(id=1, game_session=1, description='Wand', amount=10.00, character_id=2)) db.session.add( Entry(id=2, game_session=1, description='Sword', amount=20.00, character_id=2)) db.session.add( Entry(id=3, game_session=2, description='Potion', amount=30.00, character_id=2)) db.session.add( Entry(id=4, game_session=1, description='Crossbow', amount=40.00, character_id=3)) db.session.add( Entry(id=5, game_session=1, description='Spear', amount=50.00, character_id=3)) db.session.add( Entry(id=6, game_session=2, description='Backpack', amount=60.00, character_id=3)) db.session.commit() yield entry_client db.drop_all()