Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quotes with tests #9

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api-requests/quotes.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@host=https://dummyjson.com/quotes

###
# @name GetAllQuotes
GET {{host}}
Accept: application/json
Content-Type: application/json


###
# @name GetQuoteById
GET {{host}}/1
Accept: application/json
Content-Type: application/json

###
# @name GetRandomQuote
GET {{host}}/random
Accept: application/json
Content-Type: application/json

###
# @name GetRandomQuotes
GET {{host}}/random/3
Accept: application/json
Content-Type: application/json

###
# @name LimitSkipQuotes
GET {{host}}?limit=3&skip=10
Accept: application/json
Content-Type: application/json
2 changes: 1 addition & 1 deletion src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct GetAllComments {
#[derive(Deserialize, Debug)]
pub struct DeleteCommentResponse {
#[serde(flatten)]
other_fields: Comment,
pub other_fields: Comment,
#[serde(rename = "isDeleted")]
pub is_deleted: bool,
#[serde(rename = "deletedOn")]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod carts;
mod comments;
mod posts;
mod products;
mod quotes;
mod recipes;
mod todos;

Expand All @@ -11,6 +12,7 @@ pub use carts::*;
pub use comments::*;
pub use posts::*;
pub use products::*;
pub use quotes::*;
pub use recipes::*;
use reqwest::Client;
pub use todos::*;
Expand Down
76 changes: 76 additions & 0 deletions src/quotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::{DummyJsonClient, API_BASE_URL};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

static QUOTES_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/quotes", API_BASE_URL));

#[derive(Serialize, Deserialize, Debug)]
pub struct Quote {
pub id: u32,
pub quote: String,
pub author: String,
}

#[derive(Deserialize, Debug)]
pub struct GetAllQuotes {
pub quotes: Vec<Quote>,
pub total: u32,
pub skip: u32,
pub limit: u32,
}

impl DummyJsonClient {
/// Get all quotes
pub async fn get_all_quotes(&self) -> Result<GetAllQuotes, reqwest::Error> {
self.client
.get(QUOTES_BASE_URL.as_str())
.send()
.await?
.json::<GetAllQuotes>()
.await
}

/// Get quote by id
pub async fn get_quote_by_id(&self, id: u32) -> Result<Quote, reqwest::Error> {
self.client
.get(format!("{}/{}", QUOTES_BASE_URL.as_str(), id))
.send()
.await?
.json::<Quote>()
.await
}

/// Get random quote
pub async fn get_random_quote(&self) -> Result<Quote, reqwest::Error> {
self.client
.get(format!("{}/random", QUOTES_BASE_URL.as_str()))
.send()
.await?
.json::<Quote>()
.await
}

/// Get random quotes
pub async fn get_random_quotes(&self, count: u32) -> Result<Vec<Quote>, reqwest::Error> {
self.client
.get(format!("{}/random/{}", QUOTES_BASE_URL.as_str(), count))
.send()
.await?
.json::<Vec<Quote>>()
.await
}

/// Limit and skip quotes
pub async fn limit_skip_quotes(
&self,
limit: u32,
skip: u32,
) -> Result<GetAllQuotes, reqwest::Error> {
self.client
.get(format!("{}/?limit={}&skip={}", QUOTES_BASE_URL.as_str(), limit, skip))
.send()
.await?
.json::<GetAllQuotes>()
.await
}
}
43 changes: 43 additions & 0 deletions tests/quotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mod quotes {
use dummy_json_rs::DummyJsonClient;

#[tokio::test]
async fn get_all_quotes() {
let client = DummyJsonClient::default();
let response = client.get_all_quotes().await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_quote_by_id() {
let client = DummyJsonClient::default();
let response = client.get_quote_by_id(1).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_random_quote() {
let client = DummyJsonClient::default();
let response = client.get_random_quote().await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn get_random_quotes() {
let client = DummyJsonClient::default();
let response = client.get_random_quotes(3).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}

#[tokio::test]
async fn limit_skip_quotes() {
let client = DummyJsonClient::default();
let response = client.limit_skip_quotes(3, 10).await;
assert!(response.is_ok());
println!("{:#?}", response.unwrap());
}
}