增加测试
This commit is contained in:
parent
499bdfa9fc
commit
ede5b7074e
|
@ -26,7 +26,6 @@ pub fn serve(repo: Arc<dyn Repository>, req: &rouille::Request) -> rouille::Resp
|
|||
},
|
||||
_ => return rouille::Response::from(Status::BadRequest),
|
||||
};
|
||||
// rouille::Response::from(Status::InternalServerError)
|
||||
match create_pokemon::execute(repo, req) {
|
||||
Ok(create_pokemon::Response {
|
||||
number,
|
||||
|
@ -42,3 +41,4 @@ pub fn serve(repo: Arc<dyn Repository>, req: &rouille::Request) -> rouille::Resp
|
|||
Err(create_pokemon::Error::Unknown) => rouille::Response::from(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,20 +44,34 @@ pub fn execute(repo: Arc<dyn Repository>, req: Request) -> Result<Response, Erro
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::repo::pokemon::InMemoryRepository;
|
||||
use super::*;
|
||||
|
||||
use crate::domain::create_pokemon::Request;
|
||||
use crate::domain::entity::{PokemonName, PokemonNumber, PokemonTypes};
|
||||
|
||||
impl Request {
|
||||
fn new(number: PokemonNumber, name: PokemonName, types: PokemonTypes) -> Self {
|
||||
Self {
|
||||
number: u16::from(number),
|
||||
name: String::from(name),
|
||||
types: Vec::<String>::from(types),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_should_return_an_unknown_error_when_an_unexpected_error_happens(){
|
||||
let mut repo = Arc::new(InMemoryRepository::new());
|
||||
let number = 25;
|
||||
let req = Request {
|
||||
number: number,
|
||||
name: "Pikachu".to_string(),
|
||||
types: vec!["Electric".to_string(), ],
|
||||
};
|
||||
let repo = Arc::new(InMemoryRepository::new());
|
||||
let req = Request::new(
|
||||
PokemonNumber::pikachu(),
|
||||
PokemonName::pikachu(),
|
||||
PokemonTypes::pikachu()
|
||||
);
|
||||
let res = execute(repo, req);
|
||||
match res {
|
||||
Err(Error::Unknown) => {},
|
||||
|
@ -68,32 +82,31 @@ mod tests {
|
|||
#[test]
|
||||
fn it_should_return_the_pokemon_number_otherwise() {
|
||||
let repo = Arc::new(InMemoryRepository::new());
|
||||
let num = 25;
|
||||
let req = Request {
|
||||
number: num,
|
||||
name: "Pikachu".to_string(),
|
||||
types: vec!["Electric".to_string(), ],
|
||||
};
|
||||
let req = Request::new(
|
||||
PokemonNumber::pikachu(),
|
||||
PokemonName::pikachu(),
|
||||
PokemonTypes::pikachu().clone(),
|
||||
);
|
||||
let res = execute(repo, req);
|
||||
match res {
|
||||
Ok(Response {
|
||||
number,
|
||||
name,
|
||||
types,
|
||||
}) => assert_eq!(number, num),
|
||||
Ok(res) => {
|
||||
assert_eq!(res.number, u16::from(PokemonNumber::pikachu()));
|
||||
assert_eq!(res.name, String::from(PokemonName::pikachu()));
|
||||
assert_eq!(res.types, Vec::<String>::from(PokemonTypes::pikachu()));
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_should_return_a_bad_request_error_when_request_is_invalid() {
|
||||
let mut repo = Arc::new(InMemoryRepository::new());
|
||||
let repo = Arc::new(InMemoryRepository::new());
|
||||
let num = 25;
|
||||
let req = Request {
|
||||
number: num,
|
||||
name: "".to_string(),
|
||||
types: vec!["Electric".to_string(), ],
|
||||
};
|
||||
let req = Request::new(
|
||||
PokemonNumber::pikachu(),
|
||||
PokemonName::bad(),
|
||||
PokemonTypes::pikachu()
|
||||
);
|
||||
let res = execute(repo, req);
|
||||
match res {
|
||||
Ok(Response{
|
||||
|
@ -108,16 +121,18 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn it_should_return_a_conflict_error_when_pokemon_number_already_exists() {
|
||||
let number = PokemonNumber::try_from(25).unwrap();
|
||||
let name = PokemonName::try_from("Pikachu".to_string()).unwrap();
|
||||
let types = PokemonTypes::try_from(vec!["Electric".to_string(),]).unwrap();
|
||||
let mut repo = Arc::new(InMemoryRepository::new());
|
||||
repo.insert(number, name, types);
|
||||
let req = Request {
|
||||
number: 25,
|
||||
name: "Charmander".to_string(),
|
||||
types: vec!["File".to_string()],
|
||||
};
|
||||
let repo = Arc::new(InMemoryRepository::new());
|
||||
let req = Request::new(
|
||||
PokemonNumber::pikachu(),
|
||||
PokemonName::pikachu(),
|
||||
PokemonTypes::pikachu()
|
||||
);
|
||||
let res = execute(repo.clone(), req);
|
||||
let req = Request::new(
|
||||
PokemonNumber::pikachu(),
|
||||
PokemonName::charmander(),
|
||||
PokemonTypes::charmander()
|
||||
);
|
||||
let res = execute(repo, req);
|
||||
match res {
|
||||
Err(Error::Conflict) => {},
|
||||
|
|
|
@ -115,3 +115,37 @@ impl TryFrom<String> for PokemonType {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
impl PokemonNumber {
|
||||
pub fn pikachu() -> Self {
|
||||
Self(25)
|
||||
}
|
||||
pub fn charmander() -> Self {
|
||||
Self(4)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl PokemonName {
|
||||
pub fn pikachu() -> Self {
|
||||
Self(String::from("Pikachu"))
|
||||
}
|
||||
pub fn charmander() -> Self {
|
||||
Self(String::from("Charmander"))
|
||||
}
|
||||
pub fn bad() -> Self {
|
||||
Self(String::from(""))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl PokemonTypes {
|
||||
pub fn pikachu() -> Self {
|
||||
Self(vec![PokemonType::Electric])
|
||||
}
|
||||
pub fn charmander() -> Self {
|
||||
Self(vec![PokemonType::Fire])
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue