Skip to content

Commit

Permalink
Seed use AppContext
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Dec 7, 2024
1 parent a074624 commit 618e325
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 66 deletions.
10 changes: 5 additions & 5 deletions docs-site/content/docs/the-app/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,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 @@ -579,7 +579,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 @@ -703,7 +703,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 @@ -759,7 +759,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
8 changes: 5 additions & 3 deletions examples/demo/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ impl Hooks for App {
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/controllers/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use axum_extra::extract::cookie::Cookie;
use loco_rs::prelude::*;
use serde::Serialize;
use utoipa::{openapi, OpenApi, ToSchema};
use utoipa::{OpenApi, ToSchema};

#[derive(Serialize)]
pub struct Health {
Expand Down
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
2 changes: 1 addition & 1 deletion loco-gen/src/templates/seeder/default.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Seeder for {{struct_name}}Seeder {
"{{struct_name}}Seeder".to_string()
}

async fn seed(&self, conn: &DatabaseConnection) -> AppResult<()> {
async fn seed(&self, ctx: &AppContext) -> AppResult<()> {
todo!()
}
}
4 changes: 2 additions & 2 deletions loco-new/base_template/src/app.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl Hooks for App {
Ok(())
}
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(())
}
{%- endif %}
Expand Down
14 changes: 7 additions & 7 deletions loco-new/base_template/tests/models/users.rs.t
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, "user1@example.com").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
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Hooks for App {
Ok(())
}

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(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Hooks for App {
Ok(())
}

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(())
}
}
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub trait Hooks: Send {

/// Seeds the database with initial data.
#[cfg(feature = "with-db")]
async fn seed(db: &DatabaseConnection, path: &Path) -> Result<()>;
async fn seed(db: &AppContext, path: &Path) -> Result<()>;

/// Called when the application is shutting down.
/// This function allows users to perform any necessary cleanup or final
Expand Down
2 changes: 1 addition & 1 deletion src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub async fn run_db<H: Hooks, M: MigratorTrait>(
if reset {
db::reset::<M>(&app_context.db).await?;
}
db::run_app_seed::<H>(&app_context.db, &from).await?;
db::run_app_seed::<H>(app_context, &from).await?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
//! Ok(())
//! }
//!
//! async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
//! async fn seed(db: &AppContext, base: &Path) -> Result<()> {
//! Ok(())
//! }
//! }
Expand Down
8 changes: 4 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait Seeder: Send + Sync {
fn name(&self) -> String;

/// Seeds the database with initial data.
fn seed(&self, db: &DatabaseConnection) -> impl Future<Output = AppResult<()>> + Send + Sync;
fn seed(&self, db: &AppContext) -> impl Future<Output = AppResult<()>> + Send + Sync;
}

#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -292,8 +292,8 @@ where
/// # Errors
///
/// Returns an error if the seeder fails.
pub async fn seed_via_seeder(db: &DatabaseConnection, seeder: &impl Seeder) -> crate::Result<()> {
seeder.seed(db).await
pub async fn seed_via_seeder(ctx: &AppContext, seeder: &impl Seeder) -> crate::Result<()> {
seeder.seed(ctx).await
}

/// Function to reset auto-increment
Expand Down Expand Up @@ -471,7 +471,7 @@ where
/// # Errors
///
/// when seed process is fails
pub async fn run_app_seed<H: Hooks>(db: &DatabaseConnection, path: &Path) -> AppResult<()> {
pub async fn run_app_seed<H: Hooks>(db: &AppContext, path: &Path) -> AppResult<()> {
H::seed(db, path).await
}

Expand Down
9 changes: 4 additions & 5 deletions src/testing/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use sea_orm::DatabaseConnection;

use crate::prelude::AppContext;
use crate::{app::Hooks, Result};

/// Seeds data into the database.
Expand All @@ -21,13 +20,13 @@ use crate::{app::Hooks, Result};
/// #[tokio::test]
/// async fn test_create_user() {
/// let boot = boot_test::<App, Migrator>().await;
/// seed::<App>(&boot.app_context.db).await.unwrap();
/// seed::<App>(&boot.app_context).await.unwrap();
///
/// /// .....
/// assert!(false)
/// }
/// ```
pub async fn seed<H: Hooks>(db: &DatabaseConnection) -> Result<()> {
pub async fn seed<H: Hooks>(ctx: &AppContext) -> Result<()> {
let path = std::path::Path::new("src/fixtures");
H::seed(db, path).await
H::seed(ctx, path).await
}
2 changes: 1 addition & 1 deletion src/tests_cfg/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Hooks for AppHook {
Ok(())
}

async fn seed(_db: &DatabaseConnection, _base: &Path) -> Result<()> {
async fn seed(_ctx: &AppContext, _base: &Path) -> Result<()> {
Ok(())
}
}
4 changes: 4 additions & 0 deletions starters/rest-api/Cargo.lock

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

Loading

0 comments on commit 618e325

Please sign in to comment.