Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load DB from existing db file #658

Open
TomW1605 opened this issue May 3, 2024 · 11 comments
Open

load DB from existing db file #658

TomW1605 opened this issue May 3, 2024 · 11 comments

Comments

@TomW1605
Copy link

TomW1605 commented May 3, 2024

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
Copy link

mguijarr commented May 3, 2024

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

@TomW1605
Copy link
Author

TomW1605 commented May 3, 2024

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
Copy link

mguijarr commented May 3, 2024

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
Copy link
Author

TomW1605 commented May 3, 2024

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
Copy link

mguijarr commented May 3, 2024

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
Copy link

mguijarr commented May 3, 2024

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
Copy link
Author

TomW1605 commented May 3, 2024

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
Copy link
Member

fizyk commented May 6, 2024

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
Copy link
Author

TomW1605 commented May 6, 2024

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
Copy link
Member

fizyk commented May 6, 2024

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
Copy link
Author

TomW1605 commented May 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants