codejump

Scopes, Cleanup & Testing

Preview — 3 of 10 questions

A dependency opens a database session, yields it, and closes it in finally. What ensures closure after a normal request?

AThe browser sends a cleanup request
BFastAPI resumes the generator after use and runs finally
CThe route calls close() on every returned value automatically
DPydantic closes the session during response validation

get_report depends on get_db, and both use yield. Which teardown order preserves the provider for its consumer?

Aget_report exits before get_db
Bget_db exits before get_report
CThey exit before the handler starts
DThe order is nondeterministic

A yield dependency catches an exception from the route, logs it, and needs normal error handling to continue. What should it do?

AReturn a successful response from finally
BSilently suppress the exception
CCall the route function a second time
DRe-raise the exception after logging

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.