diff --git a/examples/playground.rs b/examples/playground.rs index 318acbf..0fe0dd7 100644 --- a/examples/playground.rs +++ b/examples/playground.rs @@ -1,6 +1,6 @@ +use launch_app::app::App; #[allow(unused_imports)] use loco_rs::{cli::playground, prelude::*}; -use launch_app::app::App; #[tokio::main] async fn main() -> loco_rs::Result<()> { diff --git a/migration/src/m20240917_065557_contacts.rs b/migration/src/m20240917_065557_contacts.rs index 8eaf564..76bb8e3 100644 --- a/migration/src/m20240917_065557_contacts.rs +++ b/migration/src/m20240917_065557_contacts.rs @@ -29,7 +29,4 @@ enum Contacts { Table, Id, Email, - } - - diff --git a/src/bin/main.rs b/src/bin/main.rs index 719fee9..0262bfd 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,5 +1,5 @@ -use loco_rs::cli; use launch_app::app::App; +use loco_rs::cli; use migration::Migrator; #[tokio::main] diff --git a/src/bin/tool.rs b/src/bin/tool.rs index 719fee9..0262bfd 100644 --- a/src/bin/tool.rs +++ b/src/bin/tool.rs @@ -1,5 +1,5 @@ -use loco_rs::cli; use launch_app::app::App; +use loco_rs::cli; use migration::Migrator; #[tokio::main] diff --git a/src/controllers/pages.rs b/src/controllers/pages.rs index 9b20d4f..2014a9a 100644 --- a/src/controllers/pages.rs +++ b/src/controllers/pages.rs @@ -1,13 +1,13 @@ #![allow(clippy::missing_errors_doc)] #![allow(clippy::unnecessary_struct_initialization)] #![allow(clippy::unused_async)] -use loco_rs::prelude::*; use axum::debug_handler; +use loco_rs::prelude::*; #[debug_handler] pub async fn about( ViewEngine(v): ViewEngine, - State(_ctx): State + State(_ctx): State, ) -> Result { format::render().view(&v, "pages/about.html", serde_json::json!({})) } @@ -15,7 +15,7 @@ pub async fn about( #[debug_handler] pub async fn terms( ViewEngine(v): ViewEngine, - State(_ctx): State + State(_ctx): State, ) -> Result { format::render().view(&v, "pages/terms.html", serde_json::json!({})) } diff --git a/src/models/contacts.rs b/src/models/contacts.rs index ee6ccb3..1f61e14 100644 --- a/src/models/contacts.rs +++ b/src/models/contacts.rs @@ -7,6 +7,10 @@ impl ActiveModelBehavior for ActiveModel { } impl Model { + /// Check if contact user exists by email + /// + /// # Errors + /// When could has query/connectivity error pub async fn exists(db: &DatabaseConnection, email: &str) -> ModelResult { let count = contacts::Entity::find() .filter( diff --git a/src/views/mod.rs b/src/views/mod.rs index 204bb64..cdd6a46 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -1,4 +1,4 @@ pub mod auth; pub mod user; -pub mod contacts; \ No newline at end of file +pub mod contacts; diff --git a/tests/models/mod.rs b/tests/models/mod.rs index 1c91f1d..18105c3 100644 --- a/tests/models/mod.rs +++ b/tests/models/mod.rs @@ -1,3 +1,3 @@ mod users; -mod contacts; \ No newline at end of file +mod contacts; diff --git a/tests/models/users.rs b/tests/models/users.rs index 9595cd6..6a4b029 100644 --- a/tests/models/users.rs +++ b/tests/models/users.rs @@ -1,9 +1,9 @@ use insta::assert_debug_snapshot; -use loco_rs::{model::ModelError, testing}; use launch_app::{ app::App, models::users::{self, Model, RegisterParams}, }; +use loco_rs::{model::ModelError, testing}; use sea_orm::{ActiveModelTrait, ActiveValue, IntoActiveModel}; use serial_test::serial; diff --git a/tests/requests/auth.rs b/tests/requests/auth.rs index 95948fe..78b7961 100644 --- a/tests/requests/auth.rs +++ b/tests/requests/auth.rs @@ -1,6 +1,6 @@ use insta::{assert_debug_snapshot, with_settings}; -use loco_rs::testing; use launch_app::{app::App, models::users}; +use loco_rs::testing; use rstest::rstest; use serial_test::serial; diff --git a/tests/requests/mod.rs b/tests/requests/mod.rs index 81ed68f..b26f447 100644 --- a/tests/requests/mod.rs +++ b/tests/requests/mod.rs @@ -1,4 +1,3 @@ mod auth; -mod notes; mod prepare_data; mod user; diff --git a/tests/requests/notes.rs b/tests/requests/notes.rs deleted file mode 100644 index 92d73f1..0000000 --- a/tests/requests/notes.rs +++ /dev/null @@ -1,123 +0,0 @@ -use insta::{assert_debug_snapshot, with_settings}; -use loco_rs::testing; -use launch_app::{app::App, models::_entities::notes::Entity}; -use sea_orm::entity::prelude::*; -use serial_test::serial; - -// TODO: see how to dedup / extract this to app-local test utils -// not to framework, because that would require a runtime dep on insta -macro_rules! configure_insta { - ($($expr:expr),*) => { - let mut settings = insta::Settings::clone_current(); - settings.set_prepend_module_to_snapshot(false); - settings.set_snapshot_suffix("notes_request"); - let _guard = settings.bind_to_scope(); - }; -} - -#[tokio::test] -#[serial] -async fn can_get_notes() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let notes = request.get("/api/notes").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (notes.status_code(), notes.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_add_note() { - configure_insta!(); - - testing::request::(|request, _ctx| async move { - let payload = serde_json::json!({ - "title": "loco", - "content": "loco note test", - }); - - let add_note_request = request.post("/api/notes").json(&payload).await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (add_note_request.status_code(), add_note_request.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_get_note() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let add_note_request = request.get("/api/notes/1").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (add_note_request.status_code(), add_note_request.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_delete_note() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let count_before_delete = Entity::find().all(&ctx.db).await.unwrap().len(); - let delete_note_request = request.delete("/api/notes/1").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (delete_note_request.status_code(), delete_note_request.text()) - ); - }); - - let count_after_delete = Entity::find().all(&ctx.db).await.unwrap().len(); - assert_eq!(count_after_delete, count_before_delete - 1); - }) - .await; -} diff --git a/tests/requests/prepare_data.rs b/tests/requests/prepare_data.rs index db1ce90..60e962a 100644 --- a/tests/requests/prepare_data.rs +++ b/tests/requests/prepare_data.rs @@ -1,6 +1,6 @@ use axum::http::{HeaderName, HeaderValue}; -use loco_rs::{app::AppContext, TestServer}; use launch_app::{models::users, views::auth::LoginResponse}; +use loco_rs::{app::AppContext, TestServer}; const USER_EMAIL: &str = "test@loco.com"; const USER_PASSWORD: &str = "1234"; diff --git a/tests/requests/snapshots/can_add_note@notes_request.snap b/tests/requests/snapshots/can_add_note@notes_request.snap deleted file mode 100644 index f8457d7..0000000 --- a/tests/requests/snapshots/can_add_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(add_note_request.status_code(), add_note_request.text())" ---- -( - 200, - "{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"loco\",\"content\":\"loco note test\"}", -) diff --git a/tests/requests/snapshots/can_delete_note@notes_request.snap b/tests/requests/snapshots/can_delete_note@notes_request.snap deleted file mode 100644 index 3481fc3..0000000 --- a/tests/requests/snapshots/can_delete_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(delete_note_request.status_code(), delete_note_request.text())" ---- -( - 200, - "", -) diff --git a/tests/requests/snapshots/can_get_note@notes_request.snap b/tests/requests/snapshots/can_get_note@notes_request.snap deleted file mode 100644 index 8af1604..0000000 --- a/tests/requests/snapshots/can_get_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(add_note_request.status_code(), add_note_request.text())" ---- -( - 200, - "{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"Loco note 1 content\"}", -) diff --git a/tests/requests/snapshots/can_get_notes@notes_request.snap b/tests/requests/snapshots/can_get_notes@notes_request.snap deleted file mode 100644 index 014b75c..0000000 --- a/tests/requests/snapshots/can_get_notes@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(notes.status_code(), notes.text())" ---- -( - 200, - "[{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"Loco note 1 content\"},{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 2\",\"content\":\"Loco note 2 content\"}]", -) diff --git a/tests/requests/user.rs b/tests/requests/user.rs index 34f98d2..c26afc1 100644 --- a/tests/requests/user.rs +++ b/tests/requests/user.rs @@ -1,6 +1,6 @@ use insta::{assert_debug_snapshot, with_settings}; -use loco_rs::testing; use launch_app::app::App; +use loco_rs::testing; use serial_test::serial; use super::prepare_data; diff --git a/tests/tasks/list_contacts.rs b/tests/tasks/list_contacts.rs index 97a875e..cc73278 100644 --- a/tests/tasks/list_contacts.rs +++ b/tests/tasks/list_contacts.rs @@ -9,9 +9,11 @@ use serial_test::serial; async fn test_can_run_list_users() { let boot = testing::boot_test::().await.unwrap(); - assert!( - run_task::(&boot.app_context, Some(&"list_users".to_string()), &task::Vars::default()) - .await - .is_ok() - ); + assert!(run_task::( + &boot.app_context, + Some(&"list_users".to_string()), + &task::Vars::default() + ) + .await + .is_ok()); } diff --git a/tests/tasks/seed.rs b/tests/tasks/seed.rs index a059883..f453061 100644 --- a/tests/tasks/seed.rs +++ b/tests/tasks/seed.rs @@ -1,5 +1,5 @@ -use loco_rs::{boot::run_task, task, testing}; use launch_app::app::App; +use loco_rs::{boot::run_task, task, testing}; use serial_test::serial; #[tokio::test]