Avoiding SQL Connection Pool in FastAPI Code

A common pattern in FastAPI code bases, one that is used in all example code, is to have a global variable which represents the SQL connection pool. The only problem with this pattern is making sure no class or function pulls it in directly, inline, where it can'tbe mocked. It's hard because so much of your code will depend on it. You'll quickly see why Java and C# used dependency injection as a standard design pattern. The question, then, is "Why?" And the answer is simply all your "unit" tests will now require a DB instance. Nothing says "fun" like watching all your test code blow up because someone forgot this (sometimes you're the someone.) Then you get to unravel layer upon layer of code. Claude can't do it all for you. #code #python

I agree with what you wrote in principle but the mechanical solution for your specific example is unittest.mock.patch.

To view or add a comment, sign in

Explore content categories