refactor: decouple UpstashDriver from env loading (Item 15)#262
Conversation
|
Hi @koladefaj sorry for the delay on your answers. I'm really busy so i'll have @matheusmra assist you now on. @matheusmra could you review, please? |
matheusmra
left a comment
There was a problem hiding this comment.
Well done, @koladefaj.
Once you’ve completed the requested changes, this PR will be ready to merge.
There was a problem hiding this comment.
This file doesn’t need to be committed.
Could you please remove it?
autograder/steps/step_registry.py
Outdated
| exporter = self.config.get("exporter") or UpstashDriver() | ||
| # Update the fallback to include the required credentials | ||
| exporter = self.config.get("exporter") or UpstashDriver( | ||
| redis_url=os.getenv("UPSTASH_REDIS_URL", ""), |
There was a problem hiding this comment.
Suggestion: The fallback in os.getenv is unnecessary here.
UpstashDriver.__init__ already validates the credentials and raises a descriptive ValueError if redis_url or redis_token are missing or empty.
| mock_driver_cls.assert_called_once() | ||
|
|
||
| # Verify that the driver was initialized with strings (even if empty) | ||
| # This matches the os.getenv("...", "") logic |
There was a problem hiding this comment.
Can you update this comment to match the changes that I asked on autograder/steps/step_registry.py?
|
One thing worth noting: Maybe we could move the user/quota methods into a separate |
That’s a great catch, @matheusmra. It definitely feels cleaner to separate the pipeline's export logic from the user management stuff. Keeping the driver focused on being an Exporter while moving the quota methods to a dedicated UpstashUserService makes the code much more intuitive. I'll handle that split in my next push, along with the other minor fixes you pointed out. Thanks for the guidance |
Actually @koladefaj, we can’t split this logic right now. Even though they’re not referenced anywhere in the code, we’ll be using them in the future in our github action. |
No problem, @matheusmra I'll keep the logic in UpstashDriver then to ensure it's ready for the GitHub Action integration. |
|
I've pushed the requested changes, @matheusmra |
Thanks! |
This PR resolves Item 15 of the Technical Debt Roadmap. It decouples the UpstashDriver from global environment loading, moving configuration management to the application entry points.
Changes
Decoupled Service Layer: Removed module-level load_dotenv() from autograder/services/upstash_driver.py to eliminate side effects at import time.
Constructor Injection: Refactored UpstashDriver to accept redis_url and redis_token as explicit parameters in init.
Startup Configuration:
Integrated load_dotenv() into the FastAPI lifespan in web/core/lifespan.py.
Verified existing load_dotenv() usage in the GitHub Action entry point (github_action/main.py).
Robustness: Updated StepRegistry and UpstashDriver to handle missing environment variables gracefully by providing fallbacks and raising descriptive ValueError exceptions.
Test Suite Maintenance: Updated unit tests for UpstashDriver and StepRegistry to use mocks and ANY matchers, ensuring tests pass without requiring a local .env file.
Impact
This change makes the codebase more predictable, significantly easier to test, and aligns with the project's move toward a more decoupled, pipeline-based architecture.
Testing Performed
Ran pytest on updated driver and registry tests.
Verified that importing the driver module no longer triggers environment loading side effects.