Flask-RQ2 icon indicating copy to clipboard operation
Flask-RQ2 copied to clipboard

Add a testing option to use fakeredis

Open moritz89 opened this issue 6 years ago • 7 comments

When writing unit tests it would be useful to have an option to use fakeredis instead of a real redis server. Are there any accepted methods that I am missing or would it make sense to integrate it directly into this library?

One approach is described by python-rq but I am not sure how to best integrate it.

For context, I am using Flask-base and would like to test the registration aspect, which creates a queue event but fails since there is no running redis server.

moritz89 avatar Mar 25 '18 13:03 moritz89

There is a class attribute called connection_class that is used in the RQ._connect method to create the Redis client. Since fakeredis supports the from_url API it should be enough to subclass the RQ class and overwrite connection_class with the dotted path to the fakeredis client class.

jezdez avatar Mar 26 '18 07:03 jezdez

Would it make sense to add example code somewhere? Or is there a blog or repo that can be used as an example?

On 26. Mar 2018, at 09:11, Jannis Leidel [email protected] wrote:

There is a class attribute called connection_class that is used in the RQ._connect method to create the Redis client. Since fakeredis supports the from_url API it should be enough to subclass the RQ class and overwrite connection_class with the dotted path to the fakeredis client class.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

moritz89 avatar Mar 26 '18 08:03 moritz89

Would make sense to add a testing section to the docs. Mind opening a PR?

jezdez avatar Mar 26 '18 18:03 jezdez

Check that 👍🏼

moritz89 avatar Mar 27 '18 19:03 moritz89

It was actually much simpler than that, just set RQ_CONNECTION_CLASS = 'fakeredis.FakeStrictRedis' when setting the config variables. The relevant sections are:

class Config:
    APP_NAME = os.environ.get('APP_NAME')
    RQ_CONNECTION_CLASS = 'fakeredis.FakeStrictRedis'
rq = RQ()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    rq.init_app(app)

Where and how would it make sense to add the documentation? I would add a section in https://github.com/rq/Flask-RQ2/blob/master/docs/index.rst

moritz89 avatar Mar 29 '18 18:03 moritz89

@moritz89 Yeah, adding something to docs/index.rst makes sense to me, as a "Testing" section?

jezdez avatar Sep 19 '18 08:09 jezdez

Totally forgot about it. I’ll send you an MR by the end of next week.

On 19. Sep 2018, at 10:25, Jannis Leidel [email protected] wrote:

@moritz89 Yeah, adding something to docs/index.rst makes sense to me, as a "Testing" section?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

moritz89 avatar Sep 19 '18 14:09 moritz89