CLI allow generate entity with url without password (#436)

* fix: cli allow generate entity with url without password

* test: no password test case should not panic

* test: fix test cases, assert panic message
This commit is contained in:
Billy Chan 2022-01-14 00:46:32 +08:00 committed by GitHub
parent 82bad4a376
commit a9fc52009e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 // Missing scheme will have been caught by the Url::parse() call
// above // above
let url_username = url.username(); let url_username = url.username();
let url_password = url.password();
let url_host = url.host_str(); let url_host = url.host_str();
let is_sqlite = url.scheme() == "sqlite"; let is_sqlite = url.scheme() == "sqlite";
@ -61,9 +60,6 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Er
if url_username.is_empty() { if url_username.is_empty() {
panic!("No username was found in the database url"); 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() { if url_host.is_none() {
panic!("No host was found in the database url"); panic!("No host was found in the database url");
} }
@ -202,12 +198,11 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use clap::AppSettings; use clap::AppSettings;
use url::ParseError;
use super::*; use super::*;
#[async_std::test] #[test]
async fn test_generate_entity_no_protocol() { #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: RelativeUrlWithoutBase")]
fn test_generate_entity_no_protocol() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
.get_matches_from(vec![ .get_matches_from(vec![
@ -217,22 +212,11 @@ mod tests {
"://root:root@localhost:3306/database", "://root:root@localhost:3306/database",
]); ]);
let result = std::panic::catch_unwind(|| { smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
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"),
}
} }
#[test] #[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() { fn test_generate_entity_no_database_section() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
@ -243,12 +227,11 @@ mod tests {
"postgresql://root:root@localhost:3306", "postgresql://root:root@localhost:3306",
]); ]);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())) smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
.unwrap_or_else(handle_error);
} }
#[test] #[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() { fn test_generate_entity_no_database_path() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
@ -259,12 +242,11 @@ mod tests {
"mysql://root:root@localhost:3306/", "mysql://root:root@localhost:3306/",
]); ]);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())) smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
.unwrap_or_else(handle_error);
} }
#[test] #[test]
#[should_panic] #[should_panic(expected = "No username was found in the database url")]
fn test_generate_entity_no_username() { fn test_generate_entity_no_username() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
@ -275,12 +257,11 @@ mod tests {
"mysql://:root@localhost:3306/database", "mysql://:root@localhost:3306/database",
]); ]);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())) smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
.unwrap_or_else(handle_error);
} }
#[test] #[test]
#[should_panic] #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: PoolTimedOut")]
fn test_generate_entity_no_password() { fn test_generate_entity_no_password() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
@ -291,12 +272,12 @@ mod tests {
"mysql://root:@localhost:3306/database", "mysql://root:@localhost:3306/database",
]); ]);
smol::block_on(run_generate_command(matches.subcommand().1.unwrap())) smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
.unwrap_or_else(handle_error);
} }
#[async_std::test] #[test]
async fn test_generate_entity_no_host() { #[should_panic(expected = "called `Result::unwrap()` on an `Err` value: EmptyHost")]
fn test_generate_entity_no_host() {
let matches = cli::build_cli() let matches = cli::build_cli()
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
.get_matches_from(vec![ .get_matches_from(vec![
@ -306,17 +287,6 @@ mod tests {
"postgres://root:root@/database", "postgres://root:root@/database",
]); ]);
let result = std::panic::catch_unwind(|| { smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap();
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"),
}
} }
} }