增加测试
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),
|
_ => return rouille::Response::from(Status::BadRequest),
|
||||||
};
|
};
|
||||||
// rouille::Response::from(Status::InternalServerError)
|
|
||||||
match create_pokemon::execute(repo, req) {
|
match create_pokemon::execute(repo, req) {
|
||||||
Ok(create_pokemon::Response {
|
Ok(create_pokemon::Response {
|
||||||
number,
|
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),
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::repo::pokemon::InMemoryRepository;
|
use crate::repo::pokemon::InMemoryRepository;
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn it_should_return_an_unknown_error_when_an_unexpected_error_happens(){
|
fn it_should_return_an_unknown_error_when_an_unexpected_error_happens(){
|
||||||
let mut repo = Arc::new(InMemoryRepository::new());
|
let repo = Arc::new(InMemoryRepository::new());
|
||||||
let number = 25;
|
let req = Request::new(
|
||||||
let req = Request {
|
PokemonNumber::pikachu(),
|
||||||
number: number,
|
PokemonName::pikachu(),
|
||||||
name: "Pikachu".to_string(),
|
PokemonTypes::pikachu()
|
||||||
types: vec!["Electric".to_string(), ],
|
);
|
||||||
};
|
|
||||||
let res = execute(repo, req);
|
let res = execute(repo, req);
|
||||||
match res {
|
match res {
|
||||||
Err(Error::Unknown) => {},
|
Err(Error::Unknown) => {},
|
||||||
|
@ -68,32 +82,31 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn it_should_return_the_pokemon_number_otherwise() {
|
fn it_should_return_the_pokemon_number_otherwise() {
|
||||||
let repo = Arc::new(InMemoryRepository::new());
|
let repo = Arc::new(InMemoryRepository::new());
|
||||||
let num = 25;
|
let req = Request::new(
|
||||||
let req = Request {
|
PokemonNumber::pikachu(),
|
||||||
number: num,
|
PokemonName::pikachu(),
|
||||||
name: "Pikachu".to_string(),
|
PokemonTypes::pikachu().clone(),
|
||||||
types: vec!["Electric".to_string(), ],
|
);
|
||||||
};
|
|
||||||
let res = execute(repo, req);
|
let res = execute(repo, req);
|
||||||
match res {
|
match res {
|
||||||
Ok(Response {
|
Ok(res) => {
|
||||||
number,
|
assert_eq!(res.number, u16::from(PokemonNumber::pikachu()));
|
||||||
name,
|
assert_eq!(res.name, String::from(PokemonName::pikachu()));
|
||||||
types,
|
assert_eq!(res.types, Vec::<String>::from(PokemonTypes::pikachu()));
|
||||||
}) => assert_eq!(number, num),
|
},
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_should_return_a_bad_request_error_when_request_is_invalid() {
|
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 num = 25;
|
||||||
let req = Request {
|
let req = Request::new(
|
||||||
number: num,
|
PokemonNumber::pikachu(),
|
||||||
name: "".to_string(),
|
PokemonName::bad(),
|
||||||
types: vec!["Electric".to_string(), ],
|
PokemonTypes::pikachu()
|
||||||
};
|
);
|
||||||
let res = execute(repo, req);
|
let res = execute(repo, req);
|
||||||
match res {
|
match res {
|
||||||
Ok(Response{
|
Ok(Response{
|
||||||
|
@ -108,16 +121,18 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_should_return_a_conflict_error_when_pokemon_number_already_exists() {
|
fn it_should_return_a_conflict_error_when_pokemon_number_already_exists() {
|
||||||
let number = PokemonNumber::try_from(25).unwrap();
|
let repo = Arc::new(InMemoryRepository::new());
|
||||||
let name = PokemonName::try_from("Pikachu".to_string()).unwrap();
|
let req = Request::new(
|
||||||
let types = PokemonTypes::try_from(vec!["Electric".to_string(),]).unwrap();
|
PokemonNumber::pikachu(),
|
||||||
let mut repo = Arc::new(InMemoryRepository::new());
|
PokemonName::pikachu(),
|
||||||
repo.insert(number, name, types);
|
PokemonTypes::pikachu()
|
||||||
let req = Request {
|
);
|
||||||
number: 25,
|
let res = execute(repo.clone(), req);
|
||||||
name: "Charmander".to_string(),
|
let req = Request::new(
|
||||||
types: vec!["File".to_string()],
|
PokemonNumber::pikachu(),
|
||||||
};
|
PokemonName::charmander(),
|
||||||
|
PokemonTypes::charmander()
|
||||||
|
);
|
||||||
let res = execute(repo, req);
|
let res = execute(repo, req);
|
||||||
match res {
|
match res {
|
||||||
Err(Error::Conflict) => {},
|
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