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

Looking for a way to mock tokio_postgres::Client ? #1119

Open
capveg-netdebug opened this issue Mar 14, 2024 · 2 comments
Open

Looking for a way to mock tokio_postgres::Client ? #1119

capveg-netdebug opened this issue Mar 14, 2024 · 2 comments

Comments

@capveg-netdebug
Copy link

Hi - thanks for the great project.

Per the title, I'm trying to create a Mock Client so I can test my code without having to setup a real DB. How have other people done this?

Looking at the code, it seems like implementing GenericClient would be a great path, but it extends private::Sealed explicitly preventing that. Another avenue would be to implement an InnerClient and pass that to a new client, but both the InnerClient trait and the Client::new() functions are not accessible outside of the crate.

If there aren't any easy paths for this, could I offer to create a Mock function that looks like:

impl Client {

#[cfg(test)]
pub fn make_mock(sender: mpsc::UnboundedSender<Request>) -> Client {
      Client::new(sender, SSLMode::..., 0, 0)
   } 
}

https://docs.rs/tokio-postgres/latest/src/tokio_postgres/client.rs.html#189

Let me know and thank you in advance!

@sfackler
Copy link
Owner

You could make your own trait and implement it for Client.

@capveg-netdebug
Copy link
Author

capveg-netdebug commented Mar 15, 2024

Thanks for the reply. You're right that I could duplicate all of the code in GenericClient and thus have my own trait that was the same without the private::Sealed, but I guess I don't understand why the private::Sealed is there.

[edit for clarification] It seems like you've intentionally/explicitly made this code hard to test; why is that? Would you be open to a PR to remove that private::Sealed dependency?

Thanks in advance.

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

No branches or pull requests

2 participants