def setUp(self): self.databaseFile, PC.app.config['DATABASE'] = tempfile.mkstemp(dir="tests") PC.app.testing = True self.app = PC.app.test_client() with PC.app.app_context(): PC.init_db() mockUUID = uuid mockUUID.uuid4 = Mock(side_effect=['id_1', 'code_1', 'id_2', 'code_2']) mockEmailHandler = EmailHandler mockEmailHandler.send_email = Mock() mockImageHandler = ImageHandler mockImageHandler.save_image = Mock(side_effect=['image1.jpg', 'image2.jpg', 'image3.jpg'])
def db(create_test_app): _db = init_db(create_test_app) with create_test_app.app_context(): _db.create_all() yield _db with create_test_app.app_context(): _db.drop_all()
from run import init_db init_db()
def init(): if __name__ != "__main__": return "ERROR: Must start from terminal." print("Initializes the server config and database.") reset = False conf = config_file_ok() if conf: inp = input("Config file looks ok. Do you want to reset it (y/n)?") reset = inp == "y" if reset: print("Will create a new config file.") if reset or not conf: # db = input("Database name: ") # if not db: # print("No database name. Aborting...") # return None secret_key = input("Super secret key (empty for random key): ") if not secret_key: import uuid print("Random key chosen.") secret_key = str(uuid4()) with open("config.py", "w") as f: f.write("from os import path\n") f.write("DEBUG = True\n") f.write("SECRET_KEY = '{}'\n".format(secret_key)) f.write("DATABASE = path.join('db', 'database.db')") if path.isdir("db") and path.exists(path.join("db", "database.db")): print("A database already exists.") print("1. Reset current database.") print("2. Transfer data to new database.") print("3. Keep old database.") inp = input("Action (1,2,3): ") # inp = input("A database already exists. Do you want to reset current database? (y/n) ") # if inp != "y": # print("Database was not reseted.") # return None if inp == "2": pass elif inp != "1": print("Old database keeped.") return None print("Will initialize a new database.") print("You will need an admin account.") username = input("Username: "******"Password: "******"Retype password: "******"Error! Username or password is empty. Aborting...") else: if not path.isdir("db"): makedirs("db") with open(path.join("db", "database.db"), "w") as f: pass from run import init_db, hash_password, connect_db init_db() db = connect_db() try: r = db.execute( "insert into User(username, password, admin) values (?,?,?)", [username, hash_password(password), True]) # db.execute("insert into Beer_type values (?)", ["Ingen"]) db.commit() print("Database was successfully created.") except Exception as e: print("Database not created, error.") print(e) pass finally: if db: db.close()
def setUp(self): app.config.from_object('config.TestConfig') init_db() self.client = app.test_client()