Challenge - 5 Problems
Database Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask test with database setup?
Consider this Flask test code snippet that uses a test database. What will be the output when running this test?
Flask
import unittest from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['TESTING'] = True db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) class UserTestCase(unittest.TestCase): def setUp(self): db.create_all() user = User(name='Alice') db.session.add(user) db.session.commit() def tearDown(self): db.session.remove() db.drop_all() def test_user_count(self): count = User.query.count() print(count) if __name__ == '__main__': unittest.main()
Attempts:
2 left
💡 Hint
Think about what happens in setUp before the test runs.
✗ Incorrect
The setUp method creates the database tables and adds one user named 'Alice'. So the count of users is 1.
🔧 Debug
intermediate2:00remaining
Why does this Flask test raise an error when accessing the database?
This Flask test raises an error when trying to query the database. What is the cause?
Flask
import unittest from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['TESTING'] = True db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) class UserTestCase(unittest.TestCase): def test_user_count(self): count = User.query.count() print(count) if __name__ == '__main__': unittest.main()
Attempts:
2 left
💡 Hint
Check if the database schema exists before querying.
✗ Incorrect
Without calling db.create_all(), the tables do not exist, so querying raises an error.
📝 Syntax
advanced2:00remaining
Which option correctly uses Flask's test client with database setup?
Select the code snippet that correctly sets up a Flask test client and database for testing a route that queries the database.
Attempts:
2 left
💡 Hint
Remember to call test_client as a function and create tables before adding data.
✗ Incorrect
Option A correctly calls app.test_client() and creates tables before adding data. Option A misses parentheses on test_client. Option A misses creating tables. Option A drops tables but does not create them before adding data.
❓ state_output
advanced2:00remaining
What is the value of 'user_count' after running this Flask test?
Given this test code, what will be the value of 'user_count' printed?
Flask
import unittest from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['TESTING'] = True db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) class UserTestCase(unittest.TestCase): def setUp(self): db.create_all() user1 = User(name='Alice') db.session.add(user1) db.session.commit() def test_add_user(self): user2 = User(name='Bob') db.session.add(user2) db.session.commit() user_count = User.query.count() print(user_count) def tearDown(self): db.session.remove() db.drop_all() if __name__ == '__main__': unittest.main()
Attempts:
2 left
💡 Hint
Consider the users added in setUp and test_add_user.
✗ Incorrect
setUp adds one user, test_add_user adds another, so total is 2.
🧠 Conceptual
expert2:00remaining
Which statement best explains why Flask tests use an in-memory SQLite database?
Why do Flask tests often configure the database URI as 'sqlite:///:memory:' during testing?
Attempts:
2 left
💡 Hint
Think about speed and isolation in tests.
✗ Incorrect
Using 'sqlite:///:memory:' creates a fast, temporary database in RAM that is fresh for each test, avoiding side effects.