def initialize_db(app): """ Fixture for creating tables in the database. Drops tables after each function. Args: app (Flask): a flask application instance. """ # Create tables db.create_all() yield app # Close connection. Postgres needs it to release locks db.session.close() # Drop all tables db.drop_all()
def cli_app(): """ Fixture for creating a flask app and app context specifically for testing the custom flask cli. Note: We can't use the existing `app` fixture first because we need a per function scope. Secondly, we need to pass yield a factory function instead of the app itself """ # Declaring app context so that it can be popped outside the app closure. flask_app_context = None # Closure for creating flask app and context. def app(info): # Use the variable the defined outside the closure. nonlocal flask_app_context # Create flask application. flask_app = create_app(test_env=True) # Create application context flask_app_context = flask_app.app_context() # Push it onto the context stack. flask_app_context.push() # Create the tables. db.create_all() return flask_app yield app # Close connection. Postgres needs it to release locks. db.session.close() # Drop all the tables. db.drop_all() # Pop application context flask_app_context.pop()
def tearDown(self): db.session.remove() db.drop_all()
#test_addr1= AddressModel(username='******', address='456') #add_list = [{"username": '******', "address": "123"}, {"username": '******', "address": "456"}] #guest = UserModel(username='******', email='*****@*****.**') db.session.add(test_admin) db.session.add(test_addr) #db.session.bulk_insert_mappings(AddressModel, add_list) # db.session.execute( # AddressModel.__table__.insert(), # add_list # ) db.session.commit() print(test_addr.id) elif command == 'clean': #db.metadata.clear() db.drop_all() elif command == 'query': #user = UserModel.query.filter_by(username='******').first() #print(user.email) # Option 1 joined_table = db.session.query(PeopleModel, AddressModel).filter(PeopleModel.username==AddressModel.username) \ .all() print(type(joined_table[0])) #test1 anmo_rest = Combined(PeopleModel, AddressModel).exclude(['id'], ['id']).to_dict(joined_table) print(anmo_rest) #test2 #user = UserModel.query.filter_by(username='******').first() #Combined(UserModel, anmo_rest).to_dict(joined_table) else: