You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. extern crate actix_demo;
  2. use actix_demo::{AppConfig, AppData};
  3. use actix_web::test;
  4. use actix_web::App;
  5. use diesel::SqliteConnection;
  6. use diesel::r2d2::{self, ConnectionManager};
  7. use env_logger::Env;
  8. use std::io;
  9. use std::io::ErrorKind;
  10. use actix_demo::configure_app;
  11. use actix_http::Request;
  12. use actix_web::{dev as ax_dev, Error as AxError};
  13. pub async fn test_app() -> impl ax_dev::Service<
  14. Request = Request,
  15. Response = ax_dev::ServiceResponse<impl ax_dev::MessageBody>,
  16. Error = AxError,
  17. > {
  18. let _ = dotenv::dotenv()
  19. .map_err(|err| {
  20. io::Error::new(
  21. ErrorKind::Other,
  22. format!("Failed to set up env: {:?}", err),
  23. )
  24. })
  25. .unwrap();
  26. let _ = env_logger::builder()
  27. .is_test(true)
  28. .parse_env(Env::default().filter("ACTIX_DEMO_TEST_RUST_LOG"))
  29. .try_init();
  30. let connspec = ":memory:";
  31. let manager = ConnectionManager::<SqliteConnection>::new(connspec);
  32. let pool = r2d2::Pool::builder()
  33. .build(manager)
  34. .map_err(|err| {
  35. io::Error::new(
  36. ErrorKind::Other,
  37. format!("Failed to create pool: {:?}", err),
  38. )
  39. })
  40. .unwrap();
  41. let _ = {
  42. let conn = &pool
  43. .get()
  44. .map_err(|err| {
  45. io::Error::new(
  46. ErrorKind::Other,
  47. format!("Failed to get connection: {:?}", err),
  48. )
  49. })
  50. .unwrap();
  51. let migrations_dir = diesel_migrations::find_migrations_directory()
  52. .map_err(|err| {
  53. io::Error::new(
  54. ErrorKind::Other,
  55. format!("Error finding migrations dir: {:?}", err),
  56. )
  57. })
  58. .unwrap();
  59. let _ = diesel_migrations::run_pending_migrations_in_directory(
  60. conn,
  61. &migrations_dir,
  62. &mut io::sink(),
  63. )
  64. .map_err(|err| {
  65. io::Error::new(
  66. ErrorKind::Other,
  67. format!("Error running migrations: {:?}", err),
  68. )
  69. })
  70. .unwrap();
  71. };
  72. test::init_service(
  73. App::new()
  74. .configure(configure_app(AppData {
  75. config: AppConfig { hash_cost: 8 },
  76. pool,
  77. }))
  78. .wrap(actix_web::middleware::Logger::default()),
  79. )
  80. .await
  81. }