From a9fc52009e1376b0141995ab4a57287d40f63fd4 Mon Sep 17 00:00:00 2001 From: Billy Chan <30400950+billy1624@users.noreply.github.com> Date: Fri, 14 Jan 2022 00:46:32 +0800 Subject: [PATCH] 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 --- sea-orm-cli/src/main.rs | 62 +++++++++++------------------------------ 1 file changed, 16 insertions(+), 46 deletions(-) diff --git a/sea-orm-cli/src/main.rs b/sea-orm-cli/src/main.rs index f689a52e..8cf9134a 100644 --- a/sea-orm-cli/src/main.rs +++ b/sea-orm-cli/src/main.rs @@ -50,7 +50,6 @@ async fn run_generate_command(matches: &ArgMatches<'_>) -> Result<(), Box) -> Result<(), Box match e.downcast::() { - 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) @@ -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) @@ -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) @@ -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) @@ -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![ @@ -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::() { - Ok(_) => (), - Err(e) => panic!("Expected ParseError but got: {:?}", e), - }, - _ => panic!("Should have panicked"), - } + smol::block_on(run_generate_command(matches.subcommand().1.unwrap())).unwrap(); } }