Skip to content

Commit

Permalink
Merge branch 'master' into bump-loco-version-0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad authored Jan 8, 2025
2 parents 7e0eeff + f72a6bd commit 976acd6
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 97 deletions.
14 changes: 7 additions & 7 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ Integrate your seed into the app's Hook implementations by following these steps
impl Hooks for App {
// Other implementations...
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
Ok(())
}
}
Expand Down Expand Up @@ -694,7 +694,7 @@ use loco_rs::testing::prelude::*;
async fn handle_create_with_password_with_duplicate() {
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
assert!(get_user_by_id(1).ok());
}
```
Expand Down Expand Up @@ -818,7 +818,7 @@ async fn can_find_by_pid() {
configure_insta!();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand Down Expand Up @@ -855,8 +855,8 @@ pub struct App;
#[async_trait]
impl Hooks for App {
//...
async fn truncate(db: &DatabaseConnection) -> Result<()> {
// truncate_table(db, users::Entity).await?;
async fn truncate(ctx: &AppContext) -> Result<()> {
// truncate_table(&ctx.db, users::Entity).await?;
Ok(())
}
Expand All @@ -874,7 +874,7 @@ async fn is_user_exists() {
configure_insta!();
let boot = boot_test::<App, Migrator>().await;
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();
assert!(get_user_by_id(1).ok());
}
Expand Down
5 changes: 3 additions & 2 deletions examples/demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions examples/demo/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use loco_rs::{
Result,
};
use migration::Migrator;
use sea_orm::DatabaseConnection;

use crate::{
controllers::{self, middlewares},
Expand Down Expand Up @@ -106,17 +105,19 @@ impl Hooks for App {
// tasks-inject (do not remove)
}

async fn truncate(db: &DatabaseConnection) -> Result<()> {
truncate_table(db, users_roles::Entity).await?;
truncate_table(db, roles::Entity).await?;
truncate_table(db, users::Entity).await?;
truncate_table(db, notes::Entity).await?;
async fn truncate(ctx: &AppContext) -> Result<()> {
truncate_table(&ctx.db, users_roles::Entity).await?;
truncate_table(&ctx.db, roles::Entity).await?;
truncate_table(&ctx.db, users::Entity).await?;
truncate_table(&ctx.db, notes::Entity).await?;
Ok(())
}

async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<notes::ActiveModel>(db, &base.join("notes.yaml").display().to_string()).await?;
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string())
.await?;
db::seed::<notes::ActiveModel>(&ctx.db, &base.join("notes.yaml").display().to_string())
.await?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion examples/demo/src/tasks/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Task for SeedData {
db::reset::<Migrator>(&app_context.db).await?;
}
let path = std::path::Path::new("src/fixtures");
db::run_app_seed::<App>(&app_context.db, path).await?;
db::run_app_seed::<App>(&app_context, path).await?;
Ok(())
}
}
14 changes: 7 additions & 7 deletions examples/demo/tests/models/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn handle_create_with_password_with_duplicate() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let new_user: Result<Model, ModelError> = Model::create_with_password(
&boot.app_context.db,
Expand All @@ -81,7 +81,7 @@ async fn can_find_by_email() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user = Model::find_by_email(&boot.app_context.db, "[email protected]").await;
let non_existing_user_results =
Expand All @@ -97,7 +97,7 @@ async fn can_find_by_pid() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let existing_user =
Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
Expand All @@ -114,7 +114,7 @@ async fn can_verification_token() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -143,7 +143,7 @@ async fn can_set_forgot_password_sent() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -172,7 +172,7 @@ async fn can_verified() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand All @@ -199,7 +199,7 @@ async fn can_reset_password() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/tests/requests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn can_get_or_insert() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();
let response = request.get("/cache/get_or_insert").await;
assert_eq!(response.text(), "user1");

Expand Down
6 changes: 3 additions & 3 deletions examples/demo/tests/requests/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn can_get_notes(#[case] test_name: &str, #[case] params: serde_json::Valu
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let notes = request.get("notes").add_query_params(params).await;

Expand Down Expand Up @@ -80,7 +80,7 @@ async fn can_get_note() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let add_note_request = request.get("/notes/1").await;

Expand All @@ -105,7 +105,7 @@ async fn can_delete_note() {
configure_insta!();

request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let count_before_delete = Entity::find().all(&ctx.db).await.unwrap().len();
let delete_note_request = request.delete("/notes/1").await;
Expand Down
2 changes: 1 addition & 1 deletion loco-gen/src/templates/model/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn test_model() {
configure_insta!();

let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

// query your model, e.g.:
//
Expand Down
13 changes: 6 additions & 7 deletions loco-new/base_template/src/app.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use loco_rs::{
};
{%- if settings.db %}
use migration::Migrator;
use sea_orm::DatabaseConnection;
{%- endif %}

#[allow(unused_imports)]
Expand Down Expand Up @@ -99,23 +98,23 @@ impl Hooks for App {
{%- if settings.db %}
{%- if settings.auth %}
async fn truncate(db: &DatabaseConnection) -> Result<()> {
async fn truncate(ctx: &AppContext) -> Result<()> {
{%- else %}
async fn truncate(_db: &DatabaseConnection) -> Result<()> {
async fn truncate(_ctx: &AppContext) -> Result<()> {
{%- endif %}
{%- if settings.auth %}
truncate_table(db, users::Entity).await?;
truncate_table(&ctx.db, users::Entity).await?;
{%- endif %}
Ok(())
}
{%- if settings.auth %}
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
async fn seed(ctx: &AppContext, base: &Path) -> Result<()> {
{%- else %}
async fn seed(_db: &DatabaseConnection, _base: &Path) -> Result<()> {
async fn seed(_ctx: &AppContext, _base: &Path) -> Result<()> {
{%- endif %}
{%- if settings.auth %}
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
db::seed::<users::ActiveModel>(&ctx.db, &base.join("users.yaml").display().to_string()).await?;
{%- endif %}
Ok(())
}
Expand Down
16 changes: 8 additions & 8 deletions loco-new/base_template/tests/models/users.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn handle_create_with_password_with_duplicate() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let new_user = Model::create_with_password(
&boot.app_context.db,
Expand All @@ -83,7 +83,7 @@ async fn can_find_by_email() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let existing_user = Model::find_by_email(&boot.app_context.db, "user1@example.com").await;
let non_existing_user_results = Model::find_by_email(&boot.app_context.db, "un@existing-email.com").await;
Expand All @@ -98,7 +98,7 @@ async fn can_find_by_pid() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let existing_user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111").await;
let non_existing_user_results = Model::find_by_pid(&boot.app_context.db, "23232323-2323-2323-2323-232323232323").await;
Expand All @@ -113,7 +113,7 @@ async fn can_verification_token() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -144,7 +144,7 @@ async fn can_set_forgot_password_sent() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn can_verified() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -204,7 +204,7 @@ async fn can_reset_password() {
configure_insta!();

let boot = boot_test::<App>().await.expect("Failed to boot test application");
seed::<App>(&boot.app_context.db).await.expect("Failed to seed database");
seed::<App>(&boot.app_context).await.expect("Failed to seed database");

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down Expand Up @@ -232,7 +232,7 @@ async fn can_reset_password() {
#[serial]
async fn magic_link() {
let boot = boot_test::<App>().await.unwrap();
seed::<App>(&boot.app_context.db).await.unwrap();
seed::<App>(&boot.app_context).await.unwrap();

let user = Model::find_by_pid(&boot.app_context.db, "11111111-1111-1111-1111-111111111111")
.await
Expand Down
4 changes: 2 additions & 2 deletions loco-new/base_template/tests/requests/auth.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async fn can_get_current_user() {
async fn can_auth_with_magic_link() {
configure_insta!();
request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let payload = serde_json::json!({
"email": "user1@example.com",
Expand Down Expand Up @@ -313,7 +313,7 @@ async fn can_reject_invalid_email() {
async fn can_reject_invalid_magic_link_token() {
configure_insta!();
request::<App, _, _>(|request, ctx| async move {
seed::<App>(&ctx.db).await.unwrap();
seed::<App>(&ctx).await.unwrap();

let magic_link_response = request.get("/api/auth/magic-link/invalid-token").await;
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use loco_rs::{
Result,
};
use migration::Migrator;
use sea_orm::DatabaseConnection;

#[allow(unused_imports)]
use crate::{
Expand Down Expand Up @@ -61,10 +60,10 @@ impl Hooks for App {
fn register_tasks(tasks: &mut Tasks) {
// tasks-inject (do not remove)
}
async fn truncate(_db: &DatabaseConnection) -> Result<()> {
async fn truncate(_ctx: &AppContext) -> Result<()> {
Ok(())
}
async fn seed(_db: &DatabaseConnection, _base: &Path) -> Result<()> {
async fn seed(_ctx: &AppContext, _base: &Path) -> Result<()> {
Ok(())
}
}
Loading

0 comments on commit 976acd6

Please sign in to comment.