extern crate reqwest; extern crate jplaceholder; extern crate serde_json; use jplaceholder::*; fn generate_user(id: i32) -> User { let mut response = reqwest::get(&format!("https://jsonplaceholder.typicode.com/users/{}", id)).expect("failed"); let text = response.text().expect("failed"); let json: User = serde_json::from_str(&text).expect("Failed to parse the json"); json } #[test] fn find_user_id() { let user = User::find(2); let json = generate_user(2); assert_eq!(user.is_none(), false); assert_eq!(user.expect("User not found"), json); } #[test] fn not_found_user_id() { let user = User::find(2000); assert_eq!(user.is_none(), true); } #[test] fn create_user() { let user = User{ id: 0, name: String::from("Hey"), username: String::from("Hey"), email: String::from("hey@gmail.com"), address: user::address::Address { street: String::from("lorem ipsum"), suite: String::from("ff"), city: String::from("New york"), zipcode: String::from("zip"), geo: user::geo::Geo { lat: String::from("ff"), lng: String::from("ff") } }, phone: String::from("ff"), website: String::from("ff"), company: user::company::Company { name: String::from("ff"), catch_phrase: String::from("ff"), bs: String::from("ff") } }; assert_eq!(User::create(user).is_ok(), true); } #[test] fn get_all_user() { let user = User::all(); assert_eq!(user[0].id, 1); } #[test] fn get_user_posts() { let user: User = User::find(1).expect("User not found"); let posts: Vec = Post::all().into_iter().filter(|p| p.user_id == user.id).collect(); assert_eq!(user.posts().expect("This user has posted no articles"), posts); }