Advertisement
Guest User

Untitled

a guest
Feb 9th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. """
  2. my_project.connections.redis.py
  3. """
  4. from redis import Redis
  5. redis_client = Redis()
  6.  
  7. """
  8. my_project.connections.__init__.py
  9. """
  10. from my_project.connections.redis import redis_client
  11.  
  12. """
  13. my_project.[all_other_modules].py
  14. """
  15. from my_project.connections import redis_client
  16.  
  17. redis_client.set( ... )
  18.  
  19.  
  20. """
  21. In connections.[some_connection].py I set up connections to external stuff, like redis. Then I expose them to all the other modules through `connections`. I guess the reason I do this is that it looks clean and I can easily maintain whatever is inside the [some_connection].py modules.
  22.  
  23. However - when I now write integration tests and want to replace Redis with FakeRedis, I can't for the life of me figure out how to patch it....
  24.  
  25. Is this even possible?
  26.  
  27. I've looked at so many examples of using unittest.mock.patch and/or monkeypatch, but however I try to patch using different namespace combinations, I can't get i working correctly.
  28.  
  29. Preliminary conclusion is that the way I'm sharing the redis instance between modules is wrong and should be changed, but I don't know the best way to change it...
  30. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement