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

CLI allow generate entity with url without password #436

Merged
merged 3 commits into from
Jan 13, 2022
Merged
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
62 changes: 16 additions & 46 deletions sea-orm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
// Missing scheme will have been caught by the Url::parse() call
// above
let url_username = url.username();
let url_password = url.password();
let url_host = url.host_str();

let is_sqlite = url.scheme() == "sqlite";
Expand All @@ -61,9 +60,6 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
if url_username.is_empty() {
panic!("No username was found in the database url");
}
if url_password.is_none() {
panic!("No password was found in the database url");
}
if url_host.is_none() {
panic!("No host was found in the database url");
}
Expand Down Expand Up @@ -202,12 +198,11 @@ where
#[cfg(test)]
mod tests {
use clap::AppSettings;
use url::ParseError;

use super::*;

#[async_std::test]
async fn test_generate_entity_no_protocol() {
#[test]
#[should_panic(expected = "called `Result::unwrap()` on an `Err` value: RelativeUrlWithoutBase")]
fn test_generate_entity_no_protocol() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
.get_matches_from(vec![
Expand All @@ -217,22 +212,11 @@ mod tests {
"://root:root@localhost:3306/database",
]);

let result = std::panic::catch_unwind(|| {
smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
});

// Make sure result is a ParseError
match result {
Ok(Err(e)) => match e.downcast::<ParseError>() {
Ok(_) => (),
Err(e) => panic!("Expected ParseError but got: {:?}", e),
},
_ => panic!("Should have panicked"),
}
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}

#[test]
#[should_panic]
#[should_panic(expected = "There is no database name as part of the url path: postgresql://root:root@localhost:3306")]
fn test_generate_entity_no_database_section() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
Expand All @@ -243,12 +227,11 @@ mod tests {
"postgresql://root:root@localhost:3306",
]);

smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
.unwrap_or_else(handle_error);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}

#[test]
#[should_panic]
#[should_panic(expected = "There is no database name as part of the url path: mysql://root:root@localhost:3306/")]
fn test_generate_entity_no_database_path() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
Expand All @@ -259,12 +242,11 @@ mod tests {
"mysql://root:root@localhost:3306/",
]);

smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
.unwrap_or_else(handle_error);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}

#[test]
#[should_panic]
#[should_panic(expected = "No username was found in the database url")]
fn test_generate_entity_no_username() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
Expand All @@ -275,12 +257,11 @@ mod tests {
"mysql://:root@localhost:3306/database",
]);

smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
.unwrap_or_else(handle_error);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}

#[test]
#[should_panic]
#[should_panic(expected = "called `Result::unwrap()` on an `Err` value: PoolTimedOut")]
fn test_generate_entity_no_password() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
Expand All @@ -291,12 +272,12 @@ mod tests {
"mysql://root:@localhost:3306/database",
]);

smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
.unwrap_or_else(handle_error);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}

#[async_std::test]
async fn test_generate_entity_no_host() {
#[test]
#[should_panic(expected = "called `Result::unwrap()` on an `Err` value: EmptyHost")]
fn test_generate_entity_no_host() {
let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName)
.get_matches_from(vec![
Expand All @@ -306,17 +287,6 @@ mod tests {
"postgres://root:root@/database",
]);

let result = std::panic::catch_unwind(|| {
smol::block_on(run_generate_command(matches.subcommand().1.unwrap()))
});

// Make sure result is a ParseError
match result {
Ok(Err(e)) => match e.downcast::<ParseError>() {
Ok(_) => (),
Err(e) => panic!("Expected ParseError but got: {:?}", e),
},
_ => panic!("Should have panicked"),
}
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
}
}