def app(): """ This function is a pytest fixture, meaning that it can be used as an object in pytest function. This function returns the flask app with testing configurations and a temporary fake database """ # We make a temporary file for the database instance db_fd, db_path = tempfile.mkstemp() # We use the application factory to create the flask app and # give it a new database path and set TESTING mode to true # which disables error catching. app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) # I don't really understand this part yet, # I just will say this is how to initialize the fake db # connect to it, and close the connection after the tests with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): # Creates and opens a temporary file, returning the file object # and the path to it. The DATABASE path is overridden so it points # to this temporary path instead of the instance folder. After # setting the path, the database tables are created and the test # data is inserted. After the test is over, the temporary file is # closed and removed. db_fd, db_path = tempfile.mkstemp() # TESTING tells Flask that the app is in test mode. Flask changes # some internal behavior so it’s easier to test, and other # extensions can also use the flag to make testing them easier. app = create_app({ 'TESTING': True, 'DATABASE': db_path }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def db_insert_test_data(app): with app.app_context(): init_db() db = get_db() test_user = User( username='******', password_hash= 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f', first_name='TestUserFirstName', last_name='TestUserLastName', ) other_user = User( username='******', password_hash= 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79', first_name='OtherUserFirstName', last_name='OtherUserLastName', ) test_post = Post( author_id=1, created=datetime.fromisoformat('2020-01-01 00:00:00'), title='test title', body='test\nbody', ) other_post = Post( author_id=2, created=datetime.fromisoformat('2020-02-01 00:00:00'), title='other title', body='other\nbody', ) db.session.add_all((test_user, other_user, test_post, other_post)) db.session.commit()
def app(): # tempfile.mkstemp() creates and opens a temporary file, returning the # file object and the path to it db_fd, db_path = tempfile.mkstemp() # The DATABASE path is overridden so it points to this temporary path # instead of the instance folder app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) # Turn off caching app.config["CACHE_TYPE"] = "null" app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # Initiate the database and insert the data.sql data with app.app_context(): init_db() get_db().executescript(_data_sql) # Return iterator of the app object. yield app # Close the file and delete the database. os.close(db_fd) os.unlink(db_path)
def app(): '''Create and configure a new app instance for each test.''' # create a temporary file to isolate the databse for each test. # tempfile.mkstemp(): creates and opens a temporary file, returning # the file object and the path to it. db_fd, db_path = tempfile.mkstemp() # /DATABASE/ path: is overridden so it points to this temporary path # instead of the instance folder. # /TESTING/ tells Flask that the app is in test mode. # create the app with common test config app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) # create the database and load test data with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # close and remove the temporary databse os.close(db_fd) os.unlink(db_path)
def app(): # db_fb -- temp file object # db_path -- temp file path db_fd, db_path = tempfile.mkstemp() # get app object app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): # initializing database init_db() # initializing the data sql get_db().executescript(_data_sql) # app generator yield app # close the file path os.close(db_fd) # unlink the path os.unlink(db_path)
def app(): ''' tempfile.mkstemp()는 임시 파일을 생성하고 파일을 연다. 반환값은 파일 설명자(file dexcriptor)와 파일 경로(path)이다. db_path는 instacne folder 대신 이 임시 폴더의 경로를 가진다. 경로를 설정한 이후, 데이터베이스 테이블이 생성되고, 테스트 데이터가 삽입된다. * 테스트가 끝나면 임시 파일이 닫히고 제거된다. ''' db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, # flask 앱에게 테스트임을 알려줌 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): ''' 2. tempfile.mkstemp() - temp file 생성, 열기 - data path 덮어쓰기 - instance 대신 임시 경로 사용 - test 종료 후 임시 파일 삭제됨 ''' db_fd, db_path = tempfile.mkstemp() ''' 3. TESTING - Flask에게 application이 test 모드로 실행됨을 알린다. ''' app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): # create the app app = create_app({'TESTING': True, 'API_KEY': 'fake key'}) # initialize the DB with app.app_context(): init_db() yield app
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({'TESTING': True, 'DATABASE': db_path}) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): app = create_app({ 'TESTING': True, 'DB_NAME': 'test_fyr_app' }) with app.app_context(): db.init_db() yield app
def client(): db_fd, db_path = tempfile.mkstemp() app = create_app({'TESTING': True, 'DATABASE': db_path}) with app.test_client() as client: with app.app_context(): init_db() yield client os.close(db_fd) os.unlink(db_path)
def app(): """ 为每一个测试用例,创建,配置一个新的 app 接口 """ # 为每个test创建临时独立的数据文件 db_fd, db_path = tempfile.mkstemp() app = create_app({"TESTING": True, "DATABASE": db_path}) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # Terminal: 关闭并删除临时文件 os.close(db_fd) os.unlink(db_path)
def create_app(self): # db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) return app
def app(): db_fd, db_path = tempfile.mkdtemp() # 创建和返回一个独一无二的临时文件,返回的值为文件对象和路径 print(db_fd, db_path) app = create_app({ 'TESTING': True, # 告诉flask这个app是testing模式 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): app = create_app({ 'TESTING': True, 'SECRET_KEY': 'dev', 'CONNECTION_STRING': 'mongodb://*****:*****@py1.documents.azure.com:10255/?ssl=true&replicaSet=globaldb', 'DATABASE': 'dev' }) with app.app_context(): init_db() db = get_db() db.insert_posts(_data_json['posts']) db.insert_users(_data_json['users']) yield app
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() yield app os.close(db_fd) os.unlink(db_path)
def comment(user_ID, appointment_id): if request.method == 'POST': db = init_db() error = None import re evaluation_record_id = 'e' + re.findall(r'([A-Z]+)(\d+)', appointment_id)[0][1] doctor_ID = db.execute( 'SELECT doctor_id from appoint where appointment_id = (%s)', (appointment_id, )).fetchone()['doctor_id'] patient_ID = db.execute( 'SELECT patient_id from patient where user_id = (%s)', (user_ID, )).fetchone()['patient_id'] rate = request.form['Rate'] comment = request.form['Comment'] if not rate: error = 'Please give your score' if error is None: db.execute( 'INSERT INTO evaluation (evaluation_record_id, doctor_id, rate, comment) ' 'VALUES (%s, %s, %s, %s)', (evaluation_record_id, doctor_ID, rate, comment)) time = db.execute( 'SELECT appointmenttime FROM appointment WHERE appointment_id=(%s)', (appointment_id, )).fetchone()['appointmenttime'] db.execute( 'INSERT INTO evaluate (evaluation_record_id, doctor_id, patient_id, time) ' 'VALUES (%s, %s, %s, %s)', (evaluation_record_id, doctor_ID, patient_ID, time)) return redirect(url_for('index.record_view', user_ID=user_ID)) return render_template('index/comment.html', appointment_id=appointment_id)
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, # tell to Flask app that the mode is testing. 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): # mkstemp() opens tmp file and returns file object and path db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): db_fd, db_path = tempfile.mkstemp() # 임시 파일 생성, 오픈 후 파일 정의와 경로를 반환 app = create_app({ 'TESTING': True, # test mode임을 알려줌, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() # create the app with common test config app = create_app({"TESTING": True, "DATABASE": db_path}) # create the database and load test data with app.app_context(): init_db() yield app # close and remove the temporary database os.close(db_fd) os.unlink(db_path)
def comment_view(user_ID): db = init_db() evaluation = db.execute( 'SELECT * FROM evaluation, doctor WHERE evaluation.doctor_id = doctor.doctor_id ' 'AND doctor.user_id = (%s)', (user_ID, )).fetchall() return render_template('index/comment_view.html', evaluation=evaluation)
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def register_patient(user_ID): if request.method == 'POST': patient_ID = request.form['Patient_ID'] name = request.form['Name'] gender = request.form['Gender'] height = request.form['Height'] weight = request.form['Weight'] age = request.form['Age'] bloodtype = request.form['BloodType'] db = init_db() error = None if not patient_ID: error = 'Patient ID is required.' elif not name: error = 'Name is required.' elif db.execute( 'SELECT patient_id FROM Patient WHERE patient_id = (%s)', (patient_ID, )).fetchone() is not None: error = 'Patient ID {} is already registered.'.format(patient_ID) if error is None: db.execute( 'INSERT INTO Patient (patient_id, name, user_id) ' 'VALUES (%s, %s, %s)', (patient_ID, name, user_ID)) db.execute( 'INSERT INTO Personal_information (name, gender, height, weight, age, bloodtype, patient_id) ' 'VALUES (%s, %s, %s, %s, %s, %s, %s)', (name, gender, height, weight, age, bloodtype, patient_ID)) return render_template('auth/login.html') flash(error) return render_template('auth/register_doctor.html', user_ID=user_ID)
def register_doctor(user_ID): if request.method == 'POST': doctor_ID = request.form['Doctor_ID'] name = request.form['Name'] title = request.form['Title'] specialization = request.form['Specialization'] availabletime = request.form['AvailableTime'] db = init_db() error = None if not doctor_ID: error = 'Doctor ID is required.' elif not name: error = 'Name is required.' elif not specialization: error = 'Specialization is required.' elif not availabletime: error = 'Available Time is required.' elif db.execute('SELECT doctor_id FROM Doctor WHERE doctor_id = (%s)', (doctor_ID, )).fetchone() is not None: error = 'Doctor ID {} is already registered.'.format(doctor_ID) if error is None: db.execute( 'INSERT INTO Doctor (doctor_id, name, title, specialization, availabletime, user_id) ' 'VALUES (%s, %s, %s, %s, %s, %s)', (doctor_ID, name, title, specialization, availabletime, user_ID)) return render_template('auth/login.html') flash(error) return render_template('auth/register_doctor.html', user_ID=user_ID, days=days)
def make(user_ID, doctor_ID): db = init_db() error = None if_made = 0 day = db.execute('SELECT availabletime FROM doctor WHERE doctor_id = (%s)', (doctor_ID,)).fetchone()['availabletime'] appointmenttime = get_next_byday(day) appointment_id = 'a' + str(db.execute('SELECT count(*) FROM appoint').fetchone()['count']+1) patient_ID = db.execute('SELECT patient_id FROM patient WHERE user_id=(%s)', (user_ID,)).fetchone()['patient_id'] doctor_name = db.execute('SELECT name FROM doctor WHERE doctor_id=(%s)', (doctor_ID,)).fetchone()['name'] if db.execute('SELECT appoint.* FROM appoint, appointment WHERE appointment.appointmenttime = (%s) AND appoint.patient_id = (%s) AND ' 'appoint.doctor_id = (%s) AND appointment.appointment_id = appoint.appointment_id', (appointmenttime, patient_ID, doctor_ID)).fetchone() is not None: error = 'You have already made an appointment with Dr.' + doctor_name + ' on ' + str(appointmenttime) print(error) if_made = 0 else: if db.execute('INSERT INTO appointment (appointment_id, appointmenttime) VALUES (%s, %s)' , (appointment_id, appointmenttime)) and \ db.execute('INSERT INTO appoint (appointment_id, doctor_id, patient_id) VALUES (%s, %s, %s)' , (appointment_id, doctor_ID, patient_ID)): if_made = 1 return render_template('appoint/make.html', user_ID=user_ID, doctor_name=doctor_name, time=appointmenttime, if_made=if_made)
def register_nurse(user_ID): if request.method == 'POST': nurse_ID = request.form['Nurse_ID'] name = request.form['Name'] specialization = request.form['Specialization'] db = init_db() error = None if not nurse_ID: error = 'Doctor ID is required.' elif not name: error = 'Name is required.' elif not specialization: error = 'Specialization is required.' elif db.execute('SELECT nurse_id FROM Nurse WHERE nurse_id = (%s)', (nurse_ID, )).fetchone() is not None: error = 'Nurse ID {} is already registered.'.format(nurse_ID) if error is None: db.execute( 'INSERT INTO Nurse (nurse_id, name, specialization, user_id) ' 'VALUES (%s, %s, %s, %s)', (nurse_ID, name, specialization, user_ID)) return render_template('auth/login.html') flash(error) return render_template('auth/register_nurse.html', user_ID=user_ID)
def load_logged_in_user(): user_ID = session.get('user_id') if user_ID is None: g.user = None else: g.user = init_db().execute('SELECT * FROM Login WHERE user_id = (%s)', (user_ID, )).fetchone()
def app(): # create and open a temporary file, returning the file object and the file path to it db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, # set flask in testing mode 'DATABASE': db_path, # points to temporary file }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # After the test is over, the temporary file is closed and removed os.close(db_fd) os.unlink(db_path)
def app(): """Create and configure a new app instance for each test.""" # create a temporary file to isolate the database for each test db_fd, db_path = tempfile.mkstemp() # create the app with common test config app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) # create the database and load test data with app.app_context(): init_db() get_db().executescript(_data_sql) yield app # close and remove the temporary database os.close(db_fd) os.unlink(db_path)