extern crate reqwest; extern crate jplaceholder; extern crate serde_json; use jplaceholder::*; fn generate_post(id: i32) -> Post { let mut response = reqwest::get(&format!("https://jsonplaceholder.typicode.com/posts/{}", id)).expect("failed"); let text = response.text().expect("failed"); let json: Post = serde_json::from_str(&text).expect("Failed to parse the json"); json } // posts #[test] fn find_post_id() { let post = Post::find(2); let json = generate_post(2); assert_eq!(post.is_none(), false); assert_eq!(post.expect("Post not found"), json); } #[test] fn not_found_post_id() { let post = Post::find(2000); assert_eq!(post.is_none(), true); } #[test] fn create_post() { let post = Post{id: 5, title: String::from("Hey"), body: String::from("hehe"), user_id: 5}; assert_eq!(Post::create(post).is_ok(), true); } #[test] fn get_all_post() { let posts = Post::all(); assert_eq!(posts[0].id, 1); } #[test] fn get_post_user() { let post: Post = Post::find(1).expect("Post not found"); let user = User::all().into_iter().filter(|u| u.id == post.user_id).nth(0); assert_eq!(user.expect("User not found"), post.user().expect("User not found")); }