def db(test_client, non_admin_user, admin_user, unconfirmed_user): """ creates and returns the initial testing database """ # Create the database and the database table _db.app = test_client _db.create_all() # Insert admin, non admin, and unconfirmed _db.session.add(non_admin_user) _db.session.add(admin_user) _db.session.add(unconfirmed_user) # add the default settings with open("config.json", "r") as config: config = json.load(config) for name, value in config["settings"].items(): s = Settings(setting=name, value=value) _db.session.add(s) # Commit the changes for the users _db.session.commit() yield _db # this is where the testing happens! _db.drop_all()
def setUp(self): db.create_all() self.user = User(login='******', password=generate_password_hash('password')) db.session.add(self.user) db.session.commit() self.new_user = User.query.filter_by(login='******').first()
def __init__(self, data): """ constructor :param data: init data """ for field in data: setattr(self, field, data[field]) db.create_all()
def init_db(): print("INIT DB") if os.path.exists("main.old.db"): os.remove("main.old.db") if os.path.exists("main.db"): os.rename("main.db", "main.old.db") # with app.app_context(): db.create_all()
def create_db_models(): """ This function translates the defined database schemas into SQL commands. Creates the database tables. :return: void """ db.create_all() db.engine.execute("DROP TABLE IF EXISTS usersfts") db.engine.execute( "CREATE VIRTUAL TABLE usersfts USING FTS5(username, first_name, last_name, email, school, state)" ) db.session.commit()
def setUp(self): db_path = os.path.join(BASE_DIR, 'test.db') try: os.remove(db_path) except FileNotFoundError: pass app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path self.app = app.test_client() db.create_all() self.db = db
def setUp(self): """Define test variables and initialize app.""" self.app = create_app(config_name="testing") self.client = self.app.test_client self.driver = webdriver.Chrome("chromedriver") # self.driver.get(self.get_server_url())driver.current_url with self.app.app_context(): # create all tables db.create_all() setting1 = Settings(setting="APP_NAME", value="Testing") setting2 = Settings(setting="SECTION_NAME", value="Category") setting3 = Settings(setting="SECTION_ITEMS", value="Products") db.session.add_all([setting1, setting2, setting3]) db.session.commit()
def test_db(): """ The procedure to run the database testing functions and write to file :return: void """ i = 1 app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') test_app = app.test_client() db.create_all() db.session.commit() for test in db_tests: clear_all_tables() write_output("Database Schema Test " + str(i) + "\n----------") tester(test) i += 1
def test_funcs(): """ The procedure to run the search query function tests and write to file :return: void """ i = 1 app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db') test_app = app.test_client() db.create_all() db.engine.execute("DROP TABLE IF EXISTS usersfts") db.engine.execute("CREATE VIRTUAL TABLE usersfts USING FTS5(username, first_name, last_name, email, school, state)") db.session.commit() from create_test_users import run run() for test in func_tests: write_output("Search Query Test " + str(i) + "\n----------") tester(test) i += 1
def resetDb(): db.drop_all() db.create_all()
from init import db, app class Blog(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50)) entry = db.Column(db.String(10000)) db.create_all(app=app)
def init_db(): """Creates the database tables.""" with app.app_context(): db = get_db() db.create_all()
def setUp(self): # http://kronosapiens.github.io/blog/2014/07/29/setting-up-unit-tests-with-flask.html app.config.from_pyfile('settings.py') db.session.close() db.drop_all() db.create_all()
def setUp(self): # http://kronosapiens.github.io/blog/2014/07/29/setting-up-unit-tests-with-flask.html app.config.from_pyfile('settings.py') db.session.close() db.drop_all() db.create_all()
def setUp(self): db.create_all() db.session.add(User("admin", "admin")) db.session.commit()
from init import db,app class User(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) login = db.Column(db.String(20)) pw_hash = db.Column(db.String(64)) class Animal(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50)) location = db.Column(db.String(50)) # creator_id holds the ID of a valid user # reference table names by lowercasing the class name creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) # a *relationship* lets us resolve the creator id # this will also create an 'animals' on User to access their # animals. creator = db.relationship('User', backref='animals') db.create_all(app=app)
from init import db db.create_all()
email = db.Column(db.String(100)) location = db.Column(db.String(100)) bio = db.Column(db.String(200)) photo = db.Column(db.BLOB) photo_type = db.Column(db.String(50)) @property def grav_hash(self): hash = hashlib.md5() hash.update(self.email.strip().lower().encode('utf8')) return hash.hexdigest() class Posts(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50)) content = db.Column(db.String(256)) time = db.Column(db.DateTime) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref='posts') location = db.Column(db.String(256)) image = db.Column(db.BLOB) image_type = db.Column(db.String(50)) class Follows(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) follower_id = db.Column(db.Integer, db.ForeignKey('user.id')) followee_id = db.Column(db.Integer, db.ForeignKey('user.id')) db.create_all()