Skip to content

Commit

Permalink
fix template generat when when there is a DB and not worker
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Dec 16, 2024
1 parent 1f6dea1 commit 9e96097
Show file tree
Hide file tree
Showing 28 changed files with 351 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
#![allow(clippy::wildcard_imports)]
pub use sea_orm_migration::prelude::*;

{%- if settings.auth %}
mod m20220101_000001_users;
{%- endif %}

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
{%- if settings.auth %}
Box::new(m20220101_000001_users::Migration),
{%- endif %}
// inject-above (do not remove this comment)
]
}
Expand Down
22 changes: 17 additions & 5 deletions loco-new/base_template/src/app.rs.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use loco_rs::{
Queue},
boot::{create_app, BootResult, StartMode},
controller::AppRoutes,
{%- if settings.db %}
{%- if settings.auth %}
db::{self, truncate_table},
{%- endif %}
environment::Environment,
Expand All @@ -25,17 +25,16 @@ use sea_orm::DatabaseConnection;

#[allow(unused_imports)]
use crate::{
controllers
controllers ,tasks
{%- if settings.initializers -%}
, initializers
{%- endif %}
{%- if settings.db %}
,tasks
{%- if settings.auth %}
, models::_entities::users
{%- endif %}
{%- if settings.background %}
, workers::downloader::DownloadWorker
{%- endif %},
{%- endif %}
};

pub struct App;
Expand Down Expand Up @@ -97,13 +96,26 @@ impl Hooks for App {
}
{%- if settings.db %}
{%- if settings.auth %}
async fn truncate(db: &DatabaseConnection) -> Result<()> {
{%- else %}
async fn truncate(_db: &DatabaseConnection) -> Result<()> {
{%- endif %}
{%- if settings.auth %}
truncate_table(db, users::Entity).await?;
{%- endif %}
Ok(())
}
{%- if settings.auth %}
async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> {
{%- else %}
async fn seed(_db: &DatabaseConnection, _base: &Path) -> Result<()> {
{%- endif %}
{%- if settings.auth %}
db::seed::<users::ActiveModel>(db, &base.join("users.yaml").display().to_string()).await?;
{%- endif %}
Ok(())
}
{%- endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0

pub mod prelude;

{%- if settings.auth %}
pub mod users;
{%- endif %}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0

{%- if settings.auth %}
pub use super::users::Entity as Users;
{%- endif %}
2 changes: 0 additions & 2 deletions loco-new/base_template/src/models/mod.rs

This file was deleted.

4 changes: 4 additions & 0 deletions loco-new/base_template/src/models/mod.rs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod _entities;
{%- if settings.auth %}
pub mod users;
{%- endif %}
1 change: 0 additions & 1 deletion loco-new/base_template/tests/models/mod.rs

This file was deleted.

3 changes: 3 additions & 0 deletions loco-new/base_template/tests/models/mod.rs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%- if settings.auth %}
mod users;
{%- endif %}
38 changes: 28 additions & 10 deletions loco-new/setup.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,34 @@ gen.copy_file("config/production.yaml"); // Production config
// Database-Related Files
// =====================
if db {
gen.copy_template_dir("migration"); // Database migrations directory
gen.copy_dir("src/models"); // Models directory, copied if background enabled
gen.copy_dir("src/fixtures"); // Database fixtures directory
gen.copy_template("examples/playground.rs.t"); // Example playground template with DB setup

// Test modules related to database models
gen.copy_file("tests/models/mod.rs"); // Models tests root
gen.copy_dir("tests/models/snapshots"); // Test snapshots for models
gen.copy_template("tests/models/users.rs.t"); // User model test template
gen.copy_template("tests/requests/prepare_data.rs.t"); // Data preparation template
// Database migrations configuration and setup
gen.copy_template("migration/Cargo.toml.t"); // Database migrations Cargo configuration
gen.copy_template("migration/src/lib.rs.t"); // Database migrations library

// Entity modules for database models
gen.copy_template("src/models/_entities/mod.rs.t"); // Root module for database entities
gen.copy_template("src/models/_entities/prelude.rs.t"); // Root module for database entities
gen.copy_template("src/models/mod.rs.t"); // Root module for database entities

// Test modules related to database models
gen.copy_template("tests/models/mod.rs.t"); // Models tests root module

if (settings.auth) {
// Authentication-related models and migrations
gen.copy_file("migration/src/m20220101_000001_users.rs"); // Users migration file
gen.copy_file("src/models/_entities/users.rs"); // Users entity definition
gen.copy_file("src/models/users.rs"); // Users model logic

// Fixtures and test setup for authentication
gen.copy_dir("src/fixtures"); // Database fixtures directory

// Test modules related to user models
gen.copy_dir("tests/models/snapshots"); // Test snapshots for models
gen.copy_template("tests/models/users.rs.t"); // User model test template
gen.copy_template("tests/requests/prepare_data.rs.t"); // Data preparation template for tests
}

gen.copy_template("examples/playground.rs.t"); // Example playground template with DB setup
}

// =====================
Expand Down
4 changes: 2 additions & 2 deletions loco-new/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Settings {
Self {
package_name: package_name.to_string(),
module_name: package_name.to_snake_case(),
auth: prompt_selection.db.enable(),
mailer: prompt_selection.db.enable(),
auth: prompt_selection.db.enable() && prompt_selection.background.enable(),
mailer: prompt_selection.db.enable() && prompt_selection.background.enable(),
db: prompt_selection.db.clone().into(),
background: prompt_selection.background.clone().into(),
asset: prompt_selection.asset.clone().into(),
Expand Down
37 changes: 24 additions & 13 deletions loco-new/src/wizard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,31 @@ pub fn start(args: &ArgsPlaceholder) -> crate::Result<Selections> {
}),
Template::RestApi => Ok(Selections {
db: select_db(args)?,
background: select_background(args)?,
background: select_background(args, None)?,
asset: AssetsOption::None,
}),
Template::SaasServerSideRendering => Ok(Selections {
db: select_db(args)?,
background: select_background(args)?,
background: select_background(args, None)?,
asset: AssetsOption::Serverside,
}),
Template::SaasClientSideRendering => Ok(Selections {
db: select_db(args)?,
background: select_background(args)?,
background: select_background(args, None)?,
asset: AssetsOption::Clientside,
}),
Template::Advanced => Ok(Selections {
db: select_db(args)?,
background: select_background(args)?,
asset: select_asset(args)?,
}),
Template::Advanced => {
let db = select_db(args)?;
let background_options = match db {
DBOption::Sqlite | DBOption::Postgres => Some(vec![BackgroundOption::None]),
DBOption::None => None,
};
Ok(Selections {
db,
background: select_background(args, background_options.as_ref())?,
asset: select_asset(args)?,
})
}
}
}

Expand All @@ -355,14 +362,18 @@ fn select_db(args: &ArgsPlaceholder) -> crate::Result<DBOption> {

/// Prompts the user to select a background worker option if none is provided in
/// the arguments.
fn select_background(args: &ArgsPlaceholder) -> crate::Result<BackgroundOption> {
fn select_background(
args: &ArgsPlaceholder,
filters: Option<&Vec<BackgroundOption>>,
) -> crate::Result<BackgroundOption> {
let bgopt = if let Some(bgopt) = args.bg.clone() {
bgopt
} else {
select_option(
"❯ Select your background worker type",
&BackgroundOption::iter().collect::<Vec<_>>(),
)?
let available_options = BackgroundOption::iter()
.filter(|opt| filters.as_ref().map_or(true, |f| !f.contains(opt)))
.collect::<Vec<_>>();

select_option("❯ Select your background worker type", &available_options)?
};
Ok(bgopt)
}
Expand Down
93 changes: 81 additions & 12 deletions loco-new/tests/templates/auth.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use loco::settings;
use loco::{settings, wizard::DBOption};
use rstest::rstest;

use super::*;
use crate::assertion;

pub fn run_generator(enable_auth: bool) -> TestGenerator {
pub fn run_generator(enable_auth: bool, db: DBOption) -> TestGenerator {
let settings = settings::Settings {
package_name: "loco-app-test".to_string(),
module_name: "loco_app_test".to_string(),
auth: enable_auth,
db: db.into(),
..Default::default()
};

Expand All @@ -19,7 +20,7 @@ pub fn run_generator(enable_auth: bool) -> TestGenerator {
fn test_config_file_without_auth(
#[values("config/development.yaml", "config/test.yaml")] config_file: &str,
) {
let generator = run_generator(false);
let generator = run_generator(false, DBOption::None);
let content = assertion::yaml::load(generator.path(config_file));
assertion::yaml::assert_path_is_empty(&content, &["auth"]);
}
Expand All @@ -28,7 +29,7 @@ fn test_config_file_without_auth(
fn test_config_file_with_auth(
#[values("config/development.yaml", "config/test.yaml")] config_file: &str,
) {
let generator = run_generator(true);
let generator = run_generator(true, DBOption::None);
let content = assertion::yaml::load(generator.path(config_file));
assertion::yaml::assert_path_key_count(&content, &["auth"], 1);

Expand All @@ -37,7 +38,7 @@ fn test_config_file_with_auth(

#[test]
fn test_config_file_development_rand_secret() {
let generator = run_generator(true);
let generator = run_generator(true, DBOption::None);
let content = assertion::yaml::load(generator.path("config/development.yaml"));
assertion::yaml::assert_path_value_eq_string(
&content,
Expand All @@ -48,7 +49,7 @@ fn test_config_file_development_rand_secret() {

#[test]
fn test_config_file_test_rand_secret() {
let generator = run_generator(true);
let generator = run_generator(true, DBOption::None);
let content = assertion::yaml::load(generator.path("config/test.yaml"));
assertion::yaml::assert_path_value_eq_string(
&content,
Expand All @@ -58,17 +59,20 @@ fn test_config_file_test_rand_secret() {
}

#[rstest]
fn test_app_rs(#[values(true, false)] auth: bool) {
let generator = run_generator(auth);
fn test_app_rs(
#[values(true, false)] auth: bool,
#[values(DBOption::None, DBOption::Sqlite)] db: DBOption,
) {
let generator = run_generator(auth, db.clone());
insta::assert_snapshot!(
format!("src_app_rs_auth_{:?}", auth),
format!("src_app_rs_auth_{:?}_{:?}", auth, db),
std::fs::read_to_string(generator.path("src/app.rs")).expect("could not open file")
);
}

#[rstest]
fn test_src_controllers_mod_rs(#[values(true, false)] auth: bool) {
let generator = run_generator(auth);
let generator = run_generator(auth, DBOption::None);
let content = std::fs::read_to_string(generator.path("src/controllers/mod.rs"))
.expect("could not open file");

Expand All @@ -81,7 +85,7 @@ fn test_src_controllers_mod_rs(#[values(true, false)] auth: bool) {

#[rstest]
fn test_src_views_mod_rs(#[values(true, false)] auth: bool) {
let generator = run_generator(auth);
let generator = run_generator(auth, DBOption::None);
let content =
std::fs::read_to_string(generator.path("src/views/mod.rs")).expect("could not open file");

Expand All @@ -91,9 +95,10 @@ fn test_src_views_mod_rs(#[values(true, false)] auth: bool) {
assertion::string::assert_line_regex(&content, "(?m)^pub mod home;$");
}
}

#[rstest]
fn test_tests_requests_mod_rs(#[values(true, false)] auth: bool) {
let generator = run_generator(auth);
let generator = run_generator(auth, DBOption::None);
let content = std::fs::read_to_string(generator.path("tests/requests/mod.rs"))
.expect("could not open file");

Expand All @@ -104,3 +109,67 @@ fn test_tests_requests_mod_rs(#[values(true, false)] auth: bool) {
assertion::string::assert_line_regex(&content, "(?m)^mod home;$");
}
}

#[rstest]
fn test_migration_src_lib(#[values(true)] auth: bool) {
let generator = run_generator(auth, DBOption::Sqlite);
let content = std::fs::read_to_string(generator.path("migration/src/lib.rs"))
.expect("could not open file");

if auth {
assertion::string::assert_line_regex(&content, "(?m)^mod m20220101_000001_users;$");
assertion::string::assert_line_regex(
&content,
r"(?m)Box::new\(m20220101_000001_users::Migration\),$",
);
}
}

#[rstest]
fn test_models_mod_rs(#[values(true)] auth: bool) {
let generator = run_generator(auth, DBOption::Sqlite);
let content =
std::fs::read_to_string(generator.path("src/models/mod.rs")).expect("could not open file");

if auth {
assertion::string::assert_line_regex(&content, "(?m)^pub mod users;$");
}
}

#[rstest]
fn test_models_entities_mod_rs(#[values(true)] auth: bool) {
let generator = run_generator(auth, DBOption::Sqlite);
let content = std::fs::read_to_string(generator.path("src/models/_entities/mod.rs"))
.expect("could not open file");

if auth {
assertion::string::assert_line_regex(&content, "(?m)^pub mod users;$");
}
}

#[rstest]
fn test_models_entities_prelude_rs(#[values(true)] auth: bool) {
let generator = run_generator(auth, DBOption::Sqlite);
let content = std::fs::read_to_string(generator.path("src/models/_entities/prelude.rs"))
.expect("could not open file");

if auth {
assertion::string::assert_line_regex(
&content,
"(?m)^pub use super::users::Entity as Users;$",
);
}
}

#[rstest]
fn test_tests_models_mod_rs(#[values(true, false)] auth: bool) {
let generator = run_generator(auth, DBOption::Sqlite);
let content = std::fs::read_to_string(generator.path("tests/models/mod.rs"))
.expect("could not open file");

if auth {
assertion::string::assert_line_regex(&content, "(?m)^mod users;$");
} else {
assert!(content.is_empty());
}
}
Loading

0 comments on commit 9e96097

Please sign in to comment.