ClearcodeHQ / pytest-redis

Redis fixtures and fixture factories for Pytest.This is a pytest plugin, that enables you to test your code that relies on a running Redis database. It allows you to specify additional fixtures for Redis process and client.
https://pypi.python.org/pypi/pytest-redis/
GNU Lesser General Public License v3.0
98 stars 16 forks source link

load DB from existing db file #658

Open TomW1605 opened 2 months ago

TomW1605 commented 2 months ago

hi, is there a way to have the factory load data from an existing DB file? that would be a much easier way to populate it with test data than having to use a bunch of set commands

mguijarr commented 2 months ago

You have --dbfilename argument, if you specify a .rdb file you already have I would assume Redis loads it first... Did you try?

TomW1605 commented 2 months ago

You have --dbfilename argument, if you specify a .rdb file you already have I would assume Redis loads it first... Did you try?

i cant see that in the docs. how do i pass that to the factory?

mguijarr commented 2 months ago
from pytest_redis import factories

redis_server_fixture = factories.proc.redis_proc(dbfilename=... , pass options here, ...)

redis_db_fixture = factories.redisdb("redis_server_fixture")

Then in your tests, use your configured redis_db_fixture and not the default one (redisdb).

TomW1605 commented 2 months ago

that wont work. if you look at the code redis_proc() dosnt have a dbfilename argument or *args/**kwargs to handle it seprately

https://github.com/ClearcodeHQ/pytest-redis/blob/main/pytest_redis/factories/proc.py#L15

mguijarr commented 2 months ago

Oh my bad... Really sorry ! I thought it was available I didn't check too much the code. Wanted to help... I apologize.

Well, your question is related to mine somehow (see #656 ) or #297 ...

mguijarr commented 2 months ago

Maybe a solution for you is to copy your rdb file for the test, and to rename it to match dbfilename as defined in pytest_redis/executor/process.py

TomW1605 commented 2 months ago

Oh my bad... Really sorry ! I thought it was available I didn't check too much the code. Wanted to help... I apologize.

No problem, thanks for trying

Maybe a solution for you is to copy your rdb file for the test, and to rename it to match dbfilename as defined in pytest_redis/executor/process.py

ill give that a try. im not sure it will work because i will need to start the fixture to get the filename and i dont know how well redis handles having its db file swapped out from under it but worth a try

fizyk commented 2 months ago

No, that's not supported at the moment. I found this in documentation https://redis.io/learn/guides/import#restore-an-rdb-file It could be a good solution on a per-process basis... but.... there's a caveat. client fixture clears the the database after each test. So the service would have to be restarted.

There's also a https://redis.io/learn/guides/import#import-using-redscli-script which could be triggered each time by the client fixture.... Not sure at the moment, which solution would be best.

TomW1605 commented 2 months ago

thanks ill look into these options

client fixture clears the the database after each test

i didnt realise that. the library also has the abilaty to connect to an existing redis server, will that be wiped each test? (this was my backup plan although at that point im not sure this test fixture adds much over just using the standard library)

fizyk commented 2 months ago

yes, that's applied as well. I don't see any other means to provide same starting points between tests and tests not affecting other tests than that. For pytest-postgresql there's a template database functionality utilised, maybe there's something similar for redis, but I'm not aware of that.

TomW1605 commented 2 months ago

in this case im testing database reads so it should be fine to not wipe it but i get why that is the sensible thing to do in most cases. i might just have to suck it up and stick a bunch of writes in the setup function