-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from abhi3700/todos
Add todos module & its tests
- Loading branch information
Showing
5 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
@host=https://dummyjson.com/todos | ||
|
||
### | ||
# @name GetAllTodos | ||
GET {{host}} | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetTodoById | ||
GET {{host}}/1 | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetRandomTodo | ||
GET {{host}}/random | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetRandomTodos | ||
GET {{host}}/random/3 | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name LimitSkipTodos | ||
GET {{host}}?limit=3&skip=10 | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name GetAllTodosOfUser | ||
GET {{host}}/user/1 | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
### | ||
# @name AddTodo | ||
POST {{host}}/add | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"todo": "Use DummyJSON in the project", | ||
"completed": false, | ||
"userId": 5 | ||
} | ||
|
||
### | ||
# @name UpdateTodo | ||
PUT {{host}}/1 | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"completed": true | ||
} | ||
|
||
### | ||
# @name DeleteTodo | ||
DELETE {{host}}/1 | ||
Accept: application/json | ||
Content-Type: application/json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
//! Todos module | ||
//! https://dummyjson.com/docs/todos | ||
use crate::{DummyJsonClient, API_BASE_URL}; | ||
use once_cell::sync::Lazy; | ||
use reqwest::Error; | ||
use serde::Deserialize; | ||
use serde_json::json; | ||
|
||
static TODOS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/todos", API_BASE_URL)); | ||
|
||
/// Todo | ||
#[derive(Deserialize, Debug)] | ||
pub struct Todo { | ||
pub id: u32, | ||
pub todo: String, | ||
pub completed: bool, | ||
#[serde(rename = "userId")] | ||
pub user_id: u32, | ||
} | ||
|
||
/// All Todos response | ||
#[derive(Deserialize, Debug)] | ||
pub struct AllTodos { | ||
pub todos: Vec<Todo>, | ||
pub total: u32, | ||
pub skip: u32, | ||
pub limit: u32, | ||
} | ||
|
||
/// Delete todo response | ||
#[derive(Deserialize, Debug)] | ||
pub struct DeleteTodoResponse { | ||
#[serde(flatten)] | ||
pub todo: Todo, | ||
#[serde(rename = "isDeleted")] | ||
pub is_deleted: bool, | ||
#[serde(rename = "deletedOn")] | ||
pub deleted_on: String, | ||
} | ||
|
||
impl DummyJsonClient { | ||
/// Get all todos | ||
pub async fn get_all_todos(&self) -> Result<AllTodos, Error> { | ||
let url = &*TODOS_BASE_URL; | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Get todo by id | ||
pub async fn get_todo_by_id(&self, id: u32) -> Result<Todo, Error> { | ||
let url = format!("{}/{}", *TODOS_BASE_URL, id); | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Get random todo | ||
pub async fn get_random_todo(&self) -> Result<Todo, Error> { | ||
let url = format!("{}/random", *TODOS_BASE_URL); | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Get random todos | ||
pub async fn get_random_todos(&self, count: u32) -> Result<Vec<Todo>, Error> { | ||
let url = format!("{}/random/{}", *TODOS_BASE_URL, count); | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Limit and skip todos | ||
pub async fn limit_skip_todos(&self, limit: u32, skip: u32) -> Result<AllTodos, Error> { | ||
let url = format!("{}/?limit={}&skip={}", *TODOS_BASE_URL, limit, skip); | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Get all todos of user | ||
pub async fn get_all_todos_of_user(&self, user_id: u32) -> Result<AllTodos, Error> { | ||
let url = format!("{}/user/{}", *TODOS_BASE_URL, user_id); | ||
self.client.get(url).send().await?.json().await | ||
} | ||
|
||
/// Add todo | ||
pub async fn add_todo(&self, todo: &str, completed: bool, user_id: u32) -> Result<Todo, Error> { | ||
let url = format!("{}/add", *TODOS_BASE_URL); | ||
let payload = json!({ "todo": todo, "completed": completed, "userId": user_id }); | ||
self.client.post(url).json(&payload).send().await?.json().await | ||
} | ||
|
||
/// Update todo | ||
pub async fn update_todo(&self, id: u32, completed: bool) -> Result<Todo, Error> { | ||
let url = format!("{}/{}", *TODOS_BASE_URL, id); | ||
let payload = json!({ "completed": completed }); | ||
self.client.put(url).json(&payload).send().await?.json().await | ||
} | ||
|
||
/// Delete todo | ||
pub async fn delete_todo(&self, id: u32) -> Result<DeleteTodoResponse, Error> { | ||
let url = format!("{}/{}", *TODOS_BASE_URL, id); | ||
self.client.delete(url).send().await?.json().await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
mod todos { | ||
use dummy_json_rs::DummyJsonClient; | ||
|
||
#[tokio::test] | ||
async fn get_all_todos() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_all_todos().await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_todo_by_id() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_todo_by_id(1).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_todo_by_id_fails_with_invalid_id() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_todo_by_id(24332).await; | ||
assert!(response.is_err()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_random_todo() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_random_todo().await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_random_todos() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_random_todos(3).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn limit_skip_todos() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.limit_skip_todos(3, 10).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn limit_skip_todos_get_all_todos() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.limit_skip_todos(0, 0).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn get_all_todos_of_user() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.get_all_todos_of_user(1).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn add_todo() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.add_todo("Use DummyJSON in the project", false, 5).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn update_todo() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.update_todo(1, true).await; | ||
assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn delete_todo() { | ||
let client = DummyJsonClient::default(); | ||
let response = client.delete_todo(1).await; | ||
// assert!(response.is_ok()); | ||
println!("{:#?}", response.unwrap()); | ||
} | ||
} |