Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixtures use AppContext instead of DatabaseConnection #1063

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -566,7 +566,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 @@ -690,7 +690,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 @@ -746,7 +746,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
12 changes: 12 additions & 0 deletions loco-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ mod controller;
mod model;
#[cfg(feature = "with-db")]
mod scaffold;
#[cfg(feature = "with-db")]
mod seeder;
#[cfg(test)]
mod testutil;

use std::{str::FromStr, sync::OnceLock};

const MAILER_T: &str = include_str!("templates/mailer/mailer.t");
Expand Down Expand Up @@ -171,6 +174,11 @@ pub enum Component {
// k
kind: ScaffoldKind,
},
#[cfg(feature = "with-db")]
Seeder {
/// Name of the thing to generate
name: String,
},
Controller {
/// Name of the thing to generate
name: String,
Expand Down Expand Up @@ -246,6 +254,10 @@ pub fn generate(component: Component, appinfo: &AppInfo) -> Result<()> {
json!({ "name": name, "ts": chrono::Utc::now(), "pkg_name": appinfo.app_name});
rrgen.generate(MIGRATION_T, &vars)?;
}
#[cfg(feature = "with-db")]
Component::Seeder { name } => {
seeder::generate(&rrgen, name.as_str())?;
}
Component::Controller {
name,
actions,
Expand Down
12 changes: 12 additions & 0 deletions loco-gen/src/seeder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::{collect_messages, Result};
use rrgen::RRgen;
use serde_json::json;

const DEFAULT_SEEDER_T: &str = include_str!("templates/seeder/default.t");

pub fn generate(rrgen: &RRgen, name: &str) -> Result<String> {
let vars = json!({"name": name});
let res = rrgen.generate(DEFAULT_SEEDER_T, &vars)?;

Ok(collect_messages(vec![res]))
}
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
15 changes: 15 additions & 0 deletions loco-gen/src/templates/seeder/default.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% set module_name = name | snake_case -%}
{% set struct_name = module_name | pascal_case -%}
use loco_rs::prelude::*;

pub struct {{struct_name}}Seeder;

impl Seeder for {{struct_name}}Seeder {
fn name(&self) -> String {
"{{struct_name}}Seeder".to_string()
}

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, "[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
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
Loading
Loading