0
0
Flaskframework~10 mins

Testing with database in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a test client for the Flask app.

Flask
with app.test_client() as [1]:
    response = client.get('/')
Drag options to blanks, or click blank then click option'
Aclient
Bapp
Crequest
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using app instead of client as the variable name.
Using request which is for incoming requests, not test client.
2fill in blank
medium

Complete the code to set up an in-memory SQLite database for testing.

Flask
app.config['SQLALCHEMY_DATABASE_URI'] = '[1]'
Drag options to blanks, or click blank then click option'
Asqlite:///:memory:
Bsqlite:///test.db
Cpostgresql://localhost/test
Dmysql://localhost/test
Attempts:
3 left
💡 Hint
Common Mistakes
Using a file-based SQLite URI instead of in-memory.
Using URIs for other databases not set up for tests.
3fill in blank
hard

Fix the error in the test setup by completing the code to create all tables before tests.

Flask
with app.app_context():
    [1]
Drag options to blanks, or click blank then click option'
Adb.init_app(app)
Bdb.drop_all()
Cdb.session.commit()
Ddb.create_all()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling drop_all() which deletes tables.
Calling commit() without creating tables.
4fill in blank
hard

Fill both blanks to add a new user and commit the change in the test database.

Flask
new_user = User(username='testuser')
db.session.[1](new_user)
db.session.[2]()
Drag options to blanks, or click blank then click option'
Aadd
Bcommit
Cremove
Drollback
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove() or rollback() which undo changes.
Forgetting to commit after adding.
5fill in blank
hard

Fill all three blanks to query a user by username and check the result in a test.

Flask
user = User.query.filter_by([1]=[2]).[3]()
Drag options to blanks, or click blank then click option'
Ausername
B'testuser'
Cfirst
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using all() which returns a list instead of one user.
Using wrong field names or missing quotes around the username.