|
1 | 1 | mod test_helpers; |
2 | | -#[cfg(test)] |
3 | | -pub mod tests { |
4 | | - use actix_session::storage::SessionStore; |
5 | | - use actix_session_sqlx_postgres::SqlxPostgresqlSessionStore; |
6 | | - use sqlx::postgres::PgPoolOptions; |
7 | | - use std::collections::HashMap; |
8 | | - use testcontainers::clients; |
9 | | - use testcontainers::core::WaitFor; |
10 | | - use testcontainers::images::generic; |
| 2 | +use actix_session::storage::SessionStore; |
| 3 | +use actix_session_sqlx_postgres::SqlxPostgresqlSessionStore; |
| 4 | +use sqlx::postgres::PgPoolOptions; |
| 5 | +use std::collections::HashMap; |
| 6 | +use testcontainers::clients; |
| 7 | +use testcontainers::core::WaitFor; |
| 8 | +use testcontainers::images::generic; |
11 | 9 |
|
12 | | - async fn postgres_store(url: String) -> SqlxPostgresqlSessionStore { |
13 | | - SqlxPostgresqlSessionStore::new(url).await.expect("") |
14 | | - } |
| 10 | +async fn postgres_store(url: String) -> SqlxPostgresqlSessionStore { |
| 11 | + SqlxPostgresqlSessionStore::new(url).await.expect("") |
| 12 | +} |
15 | 13 |
|
16 | | - #[actix_web::test] |
17 | | - async fn test_session_workflow() { |
18 | | - let docker = clients::Cli::default(); |
19 | | - let postgres = docker.run( |
20 | | - generic::GenericImage::new("postgres", "14-alpine") |
21 | | - .with_wait_for(WaitFor::message_on_stderr( |
22 | | - "database system is ready to accept connections", |
23 | | - )) |
24 | | - .with_env_var("POSTGRES_DB", "sessions") |
25 | | - .with_env_var("POSTGRES_PASSWORD", "example") |
26 | | - .with_env_var("POSTGRES_HOST_AUTH_METHOD", "trust") |
27 | | - .with_env_var("POSTGRES_USER", "tests"), |
28 | | - ); |
29 | | - let url = format!( |
30 | | - "postgres://tests:example@localhost:{}/sessions", |
31 | | - postgres.get_host_port_ipv4(5432) |
32 | | - ); |
| 14 | +#[actix_web::test] |
| 15 | +async fn test_session_workflow() { |
| 16 | + let docker = clients::Cli::default(); |
| 17 | + let postgres = docker.run( |
| 18 | + generic::GenericImage::new("postgres", "15-alpine") |
| 19 | + .with_wait_for(WaitFor::message_on_stderr( |
| 20 | + "database system is ready to accept connections", |
| 21 | + )) |
| 22 | + .with_env_var("POSTGRES_DB", "sessions") |
| 23 | + .with_env_var("POSTGRES_PASSWORD", "example") |
| 24 | + .with_env_var("POSTGRES_HOST_AUTH_METHOD", "trust") |
| 25 | + .with_env_var("POSTGRES_USER", "tests"), |
| 26 | + ); |
| 27 | + let url = format!( |
| 28 | + "postgres://tests:example@localhost:{}/sessions", |
| 29 | + postgres.get_host_port_ipv4(5432) |
| 30 | + ); |
33 | 31 |
|
34 | | - let postgres_store = postgres_store(url.clone()).await; |
35 | | - let pool = PgPoolOptions::new() |
36 | | - .max_connections(1) |
37 | | - .connect(url.as_str()) |
38 | | - .await |
39 | | - .expect("Could not connect to database"); |
40 | | - sqlx::query( |
41 | | - r#"CREATE TABLE sessions( |
| 32 | + let postgres_store = postgres_store(url.clone()).await; |
| 33 | + let pool = PgPoolOptions::new() |
| 34 | + .max_connections(1) |
| 35 | + .connect(url.as_str()) |
| 36 | + .await |
| 37 | + .expect("Could not connect to database"); |
| 38 | + sqlx::query( |
| 39 | + r#"CREATE TABLE sessions( |
42 | 40 | key TEXT PRIMARY KEY NOT NULL, |
43 | 41 | session_state JSONB, |
44 | 42 | expires TIMESTAMP WITH TIME ZONE NOT NULL |
45 | 43 | );"#, |
46 | | - ) |
47 | | - .execute(&pool) |
48 | | - .await |
49 | | - .expect("Could not create table"); |
50 | | - let mut session = HashMap::new(); |
51 | | - session.insert("key".to_string(), "value".to_string()); |
52 | | - let data = postgres_store.save(session, &time::Duration::days(1)).await; |
53 | | - println!("{:#?}", data); |
54 | | - assert!(data.is_ok()); |
55 | | - super::test_helpers::acceptance_test_suite(move || postgres_store.clone(), true).await; |
56 | | - } |
| 44 | + ) |
| 45 | + .execute(&pool) |
| 46 | + .await |
| 47 | + .expect("Could not create table"); |
| 48 | + let mut session = HashMap::new(); |
| 49 | + session.insert("key".to_string(), "value".to_string()); |
| 50 | + let data = postgres_store.save(session, &time::Duration::days(1)).await; |
| 51 | + println!("{:#?}", data); |
| 52 | + assert!(data.is_ok()); |
| 53 | + test_helpers::acceptance_test_suite(move || postgres_store.clone(), true).await; |
57 | 54 | } |
0 commit comments