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

Integration testing examples and documentation #2058

Closed
WillSquire opened this issue May 9, 2019 · 6 comments
Closed

Integration testing examples and documentation #2058

WillSquire opened this issue May 9, 2019 · 6 comments

Comments

@WillSquire
Copy link

I've been reading through the examples and docs but I can't find material on integration testing? Found a bit of information here regarding Connection::test_transaction. Is there material on this?

This is my code that contains the database connection pool:

use crate::error::Error;
use diesel::{pg::PgConnection, r2d2::ConnectionManager};
use r2d2::{Pool, PooledConnection};

pub type Connection = PooledConnection<ConnectionManager<PgConnection>>;
type ConnectionPool = Pool<ConnectionManager<PgConnection>>;

pub struct Db {
  connection_pool: ConnectionPool,
}

impl Db {
  pub fn new(
    db_user: &str,
    db_password: &str,
    db_name: &str,
    db_server: &str,
  ) -> Result<Db, Error> {
    Ok(Db {
      connection_pool: Pool::builder().max_size(15).build(
        ConnectionManager::<PgConnection>::new(format!(
          "postgres://{}:{}@{}/{}",
          db_user, db_password, db_server, db_name
        )),
      )?,
    })
  }

  pub fn connect(&self) -> Result<Connection, Error> {
    Ok(self.connection_pool.clone().get()?)
  }
}

For brevity the actual usage can be found here, which I might get away with passing a test connection if it exists.

Noticed the r2d2 feature uses TestConnection from test_helpers::*. Is the the recommended way?

@weiznich
Copy link
Member

The following code will call begin_test_transaction for each connection in a given pool:

struct TestTransaction;

impl CustomizeConnection<DbConnection, ::diesel::r2d2::Error> for TestTransaction {
    fn on_acquire(
        &self,
        conn: &mut DbConnection,
    ) -> ::std::result::Result<(), ::diesel::r2d2::Error> {
        conn.begin_test_transaction().unwrap();
        Ok(())
    }
}

let manager = ConnectionManager::<DbConnection>::new(db_url);
    let pool = Pool::builder()
        .connection_customizer(Box::new(TestTransaction))
        .build(manager)
        .expect("Failed to init pool")

@weiznich weiznich reopened this May 17, 2019
@WillSquire
Copy link
Author

Thanks @weiznich, I've tried implementing what you've put above but I'm getting no method named 'begin_test_transaction' found for type '&mut diesel::PgConnection' in the current scope. Is this implemented for postgres or am I using the wrong connection type?

@weiznich
Copy link
Member

Just to add the solution also to this issue:
You need to import the Connection trait into the current scope, otherwise begin_test_transaction is not found.

@Mastermindaxe
Copy link

Mastermindaxe commented Apr 30, 2020

I think a guide on testing would be very helpful.
Is there any work done on this since posting this issue? I guess this is also related to issue #1549 as it seems there is documentation on testing overall missing. Thanks in advance

@WillSquire
Copy link
Author

@Mastermindaxe I'm not sure about documentation, but I did implement it and have an example of the path I took here: https://github.com/WillSquire/Graphy/blob/master/api/src/db/mod.rs . Not worked on it for perhaps a year though.

@weiznich
Copy link
Member

Closed as this seems to be a duplicate of #777

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

3 participants