From 2bd0e2ac4c9bd08ce8e4b9cc8a23a2467416688f Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 30 Oct 2019 22:52:00 -0700 Subject: [PATCH] update tests --- src/endpoints.rs | 14 ++++----- src/lib.rs | 39 +---------------------- tests/async_tests.rs | 13 ++++++-- tests/ids.rs | 5 --- tests/tests.rs.bak | 75 -------------------------------------------- tests/tests_euw.rs | 12 ++----- tests/tests_jp.rs | 14 ++------- tests/tests_na.rs | 25 +++++++-------- tests/tests_tr.rs | 13 ++------ tests/testutils.rs | 19 +++++++++++ 10 files changed, 55 insertions(+), 174 deletions(-) delete mode 100644 tests/ids.rs delete mode 100644 tests/tests.rs.bak create mode 100644 tests/testutils.rs diff --git a/src/endpoints.rs b/src/endpoints.rs index 9b318ca..472c3e7 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,9 +1,9 @@ -/////////////////////////////////////////////// -// // -// ! // -// This file is automatically generated! // -// Do not directly edit! // -// // +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // /////////////////////////////////////////////// // http://www.mingweisamuel.com/riotapi-schema/tool/ @@ -195,7 +195,7 @@ impl<'a> LeagueExpV4<'a> { let mut query_params = Serializer::new(String::new()); if let Some(page) = page { query_params.append_pair("page", &*page.to_string()); }; let query_string = query_params.finish(); - let path_string = format!("/lol/league-exp/v4/entries/{}/{}/{}", division, tier, queue); + let path_string = format!("/lol/league-exp/v4/entries/{}/{}/{}", queue, tier, division); self.base.get::>("league-exp-v4.getLeagueEntries", region, path_string, Some(query_string)) } diff --git a/src/lib.rs b/src/lib.rs index 4d04b10..b25d9c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ //! Module docs TODO. +#![feature(non_exhaustive)] mod riot_api_error; pub use riot_api_error::*; @@ -15,41 +16,3 @@ pub use riot_api::*; mod req; mod util; - - -#[cfg(test)] -mod tests { - use tokio::runtime::Runtime; - use super::*; - - use url::form_urlencoded::Serializer; - #[test] - fn checkme() { - let mut query = Serializer::new(String::new()); - query.append_pair("hello", "false"); - query.append_pair("hello", "world"); - let result = query.finish(); - println!("{}", result); - } - - #[test] - #[ignore] - fn it_works() { - env_logger::init(); - - let champ = crate::consts::Champion::Riven; - println!("{}", champ); - - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); // TODO don't use unwrap. - - let rt = Runtime::new().unwrap(); - let riot_api = RiotApi::with_key(api_key.trim()); - - for i in 0..2 { - let my_future = riot_api.champion_mastery_v4().get_all_champion_masteries( - consts::Region::NA, "SBM8Ubipo4ge2yj7bhEzL7yvV0C9Oc1XA2l6v5okGMA_nCw"); - let val = rt.block_on(my_future).unwrap(); - //println!("VAL {}: {:#?}", i, val.unwrap()); - } - } -} diff --git a/tests/async_tests.rs b/tests/async_tests.rs index 901d220..f2fa722 100644 --- a/tests/async_tests.rs +++ b/tests/async_tests.rs @@ -8,7 +8,8 @@ macro_rules! async_tests { const TUPLE_ERR: (u32, u32) = (0, 1); std::process::exit({ - let mut rt = Runtime::new().expect("Failed to create runtime."); + let mut rt = tokio::runtime::current_thread::Runtime::new() + .expect("Failed to create runtime."); let (_, errs) = rt.block_on(async { println!(); @@ -70,6 +71,14 @@ macro_rules! rassert { macro_rules! rassert_eq { ( $a:expr, $b:expr ) => { rassert!($a == $b) }; ( $a:expr, $b:expr, $format:expr $(, $arg:expr)* ) => { - rassert!($a == $b, $format, $( $arg )* ) + rassert!($a == $b, $format $(, $arg )* ) + }; +} + +#[macro_export] +macro_rules! rassert_ne { + ( $a:expr, $b:expr ) => { rassert!($a != $b) }; + ( $a:expr, $b:expr, $format:expr $(, $arg:expr)* ) => { + rassert!($a != $b, $format $(, $arg )* ) }; } diff --git a/tests/ids.rs b/tests/ids.rs deleted file mode 100644 index 94f262a..0000000 --- a/tests/ids.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub const SUMMONER_ID_LUGNUTSK: &'static str = "SBM8Ubipo4ge2yj7bhEzL7yvV0C9Oc1XA2l6v5okGMA_nCw"; -pub const SUMMONER_ID_MA5TERY: &'static str = "IbC4uyFEEW3ZkZw6FZF4bViw3P1EynclAcI6-p-vCpI99Ec"; -pub const SUMMONER_ID_C9SNEAKY: &'static str = "ghHSdADqgxKwcRl_vWndx6wKiyZx0xKQv-LOhOcU5LU"; -pub const ACCOUNT_ID_C9SNEAKY: &'static str = "ML_CcLT94UUHp1iDvXOXCidfmzzPrk_Jbub1f_INhw"; -pub const ACCOUNT_ID_LUGNUTSK: &'static str = "iheZF2uJ50S84Hfq6Ob8GNlJAUmBmac-EsEEWBJjD01q1jQ"; diff --git a/tests/tests.rs.bak b/tests/tests.rs.bak deleted file mode 100644 index 42da5a9..0000000 --- a/tests/tests.rs.bak +++ /dev/null @@ -1,75 +0,0 @@ -#![feature(custom_test_frameworks)] -#![test_runner(my_runner)] -// use std::future::Future; - -use colored::*; -use lazy_static::lazy_static; -use riven::RiotApi; -use tokio::runtime::current_thread::Runtime; - - -lazy_static! { - static ref API_KEY: String = { - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); // TODO don't use unwrap. - api_key.trim().to_owned() - }; - static ref RIOT_API: RiotApi<'static> = { - RiotApi::with_key(&API_KEY) - }; -} - -fn my_runner(tests: &[()]) { - std::process::exit({ - let mut rt = Runtime::new().expect("Failed to create runtime."); - - let result = rt.block_on(async { - let a = async { - let result = plus_one(&RIOT_API).await; - print!("plus_one ... "); - match &result { - Ok(_) => println!("{}", "ok".green()), - Err(msg) => println!("{}: {}", "error".red(), msg), - }; - result - }; - let b = async { - let result = plus_two(&RIOT_API).await; - print!("plus_two ... "); - match &result { - Ok(_) => println!("{}", "ok".green()), - Err(msg) => println!("{}: {}", "error".bright_red(), msg), - }; - result - }; - let err = None; - let err = err.or(a.await.err()); - let err = err.or(b.await.err()); - err.map_or(Ok(()), |e| Err(e)) - }); - match &result { - Ok(_) => 0, - Err(_) => 2, - } - }); -} - -macro_rules! rassert { - ( $x:expr ) => { - { - if $x { Ok(()) } else { Err(stringify!($x)) }? - } - }; -} -macro_rules! rassert_eq { - ( $a:expr, $b:expr ) => { rassert!($a == $b) }; -} - -async fn plus_one(riot_api: &'static RiotApi<'static>) -> Result<(), String> { - rassert_eq!("world", "world"); - Ok(()) -} - -async fn plus_two(riot_api: &'static RiotApi<'static>) -> Result<(), String> { - rassert_eq!("hello", "world"); - Ok(()) -} diff --git a/tests/tests_euw.rs b/tests/tests_euw.rs index 0cfffae..32faff1 100644 --- a/tests/tests_euw.rs +++ b/tests/tests_euw.rs @@ -2,23 +2,15 @@ #![test_runner(my_runner)] mod async_tests; -mod ids; +mod testutils; +use testutils::*; use colored::*; -use lazy_static::lazy_static; -use tokio::runtime::current_thread::Runtime; use riven::RiotApi; use riven::consts::*; -lazy_static! { - static ref RIOT_API: RiotApi = { - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); - RiotApi::with_key(api_key.trim()) - }; -} - async_tests!{ my_runner { // Champion Mastery tests. diff --git a/tests/tests_jp.rs b/tests/tests_jp.rs index 4930a19..367e03b 100644 --- a/tests/tests_jp.rs +++ b/tests/tests_jp.rs @@ -2,23 +2,13 @@ #![test_runner(my_runner)] mod async_tests; -mod ids; +mod testutils; +use testutils::*; use colored::*; -use lazy_static::lazy_static; -use tokio::runtime::current_thread::Runtime; -use riven::RiotApi; use riven::consts::*; - -lazy_static! { - static ref RIOT_API: RiotApi = { - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); - RiotApi::with_key(api_key.trim()) - }; -} - async_tests!{ my_runner { // Summoner tests. diff --git a/tests/tests_na.rs b/tests/tests_na.rs index 7e11d90..5168613 100644 --- a/tests/tests_na.rs +++ b/tests/tests_na.rs @@ -2,24 +2,15 @@ #![test_runner(my_runner)] mod async_tests; -mod ids; +mod testutils; +use testutils::*; use colored::*; -use lazy_static::lazy_static; -use tokio::runtime::current_thread::Runtime; -use riven::RiotApi; use riven::consts::*; use riven::endpoints::summoner_v4::*; -lazy_static! { - static ref RIOT_API: RiotApi = { - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); - RiotApi::with_key(api_key.trim()) - }; -} - fn validate_lugnutsk(s: Summoner, tag: &str) -> Result<(), String> { rassert_eq!("LugnutsK", s.name, "LugnutsK name didn't match {}.", tag); @@ -53,10 +44,16 @@ async_tests!{ rassert_eq!(10, level, "New player level: {}", level); Ok(()) }, + leagueexp_get: async { + let p = RIOT_API.league_exp_v4().get_league_entries(Region::NA, Division::I, Tier::CHALLENGER, QueueType::RANKED_SOLO_5x5, None); + let d = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get challenger exp league entries.".to_owned())?; + rassert!(!d.is_empty(), "Challenger shouldn't be empty."); + Ok(()) + }, match_get: async { let p = RIOT_API.match_v4().get_match(Region::NA, 3190191338); let m = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get match.".to_owned())?; - // TODO. + rassert!(!m.participants.is_empty(), "Match should have participants."); Ok(()) }, // match_get_old: async { @@ -68,13 +65,13 @@ async_tests!{ match_get_aram: async { let p = RIOT_API.match_v4().get_match(Region::NA, 2961635718); let m = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get match.".to_owned())?; - // TODO. + rassert!(!m.participants.is_empty(), "Match should have participants."); Ok(()) }, match_get_urf900: async { let p = RIOT_API.match_v4().get_match(Region::NA, 2963663381); let m = p.await.map_err(|e| e.to_string())?.ok_or("Failed to get match.".to_owned())?; - // TODO. + rassert!(!m.participants.is_empty(), "Match should have participants."); Ok(()) }, } diff --git a/tests/tests_tr.rs b/tests/tests_tr.rs index 355c92f..d46e1ed 100644 --- a/tests/tests_tr.rs +++ b/tests/tests_tr.rs @@ -2,23 +2,14 @@ #![test_runner(my_runner)] mod async_tests; -mod ids; +mod testutils; +use testutils::*; use colored::*; -use lazy_static::lazy_static; -use tokio::runtime::current_thread::Runtime; -use riven::RiotApi; use riven::consts::*; -lazy_static! { - static ref RIOT_API: RiotApi = { - let api_key = std::fs::read_to_string("apikey.txt").unwrap(); - RiotApi::with_key(api_key.trim()) - }; -} - async_tests!{ my_runner { league_summoner_bulk_test: async { diff --git a/tests/testutils.rs b/tests/testutils.rs new file mode 100644 index 0000000..7ff1f9e --- /dev/null +++ b/tests/testutils.rs @@ -0,0 +1,19 @@ +use riven::RiotApi; +use lazy_static::lazy_static; + +lazy_static! { + pub static ref RIOT_API: RiotApi = { + let api_key = std::env::var("RGAPI_KEY").ok() + .or_else(|| std::fs::read_to_string("apikey.txt").ok()) + .expect("Failed to find RGAPI_KEY env var or apikey.txt."); + RiotApi::with_key(api_key.trim()) + }; +} + +pub mod ids { + pub const SUMMONER_ID_LUGNUTSK: &'static str = "SBM8Ubipo4ge2yj7bhEzL7yvV0C9Oc1XA2l6v5okGMA_nCw"; + pub const SUMMONER_ID_MA5TERY: &'static str = "IbC4uyFEEW3ZkZw6FZF4bViw3P1EynclAcI6-p-vCpI99Ec"; + pub const SUMMONER_ID_C9SNEAKY: &'static str = "ghHSdADqgxKwcRl_vWndx6wKiyZx0xKQv-LOhOcU5LU"; + pub const ACCOUNT_ID_C9SNEAKY: &'static str = "ML_CcLT94UUHp1iDvXOXCidfmzzPrk_Jbub1f_INhw"; + pub const ACCOUNT_ID_LUGNUTSK: &'static str = "iheZF2uJ50S84Hfq6Ob8GNlJAUmBmac-EsEEWBJjD01q1jQ"; +}