-
-
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 #13 from JakenHerman/jaken/update-and-complete-todos
Add functionality to update and complete todos w/ tests
- Loading branch information
Showing
5 changed files
with
144 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
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,50 @@ | ||
use rocket::http::Status; | ||
use dooly::{db::TodoItem, helpers::{cleanup_database, establish_connection, run_seed_script, setup_rocket}}; | ||
use serde_json::json; | ||
use rocket::http::ContentType; | ||
|
||
#[test] | ||
fn test_complete_todo() { | ||
cleanup_database(); // Clean up the database before starting the test | ||
let mut connection = establish_connection(); | ||
run_seed_script(&mut connection); // Seed the database with initial data | ||
|
||
let client = setup_rocket(); | ||
|
||
// Create a new todo item | ||
let new_todo = json!({ | ||
"title": "Test Incomplete Todo", | ||
"completed": false | ||
}); | ||
|
||
let response = client.post("/todos") | ||
.header(ContentType::JSON) | ||
.body(new_todo.to_string()) | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
assert_eq!(response.into_string().unwrap(), "Todo added successfully!"); | ||
|
||
// Fetch the todo items to get the ID of the newly added todo | ||
let response = client.get("/todos") | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
let todos: Vec<TodoItem> = serde_json::from_str(&response.into_string().unwrap()).unwrap(); | ||
let todo_id = todos[0].id; // Assuming this is the only todo item | ||
|
||
// Mark the todo item as completed | ||
let response = client.put(format!("/todos/{}/complete", todo_id)) | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
assert_eq!(response.into_string().unwrap(), "Todo marked as completed!"); | ||
|
||
// Fetch the updated todo to verify the changes | ||
let response = client.get("/todos") | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
let todos: Vec<TodoItem> = serde_json::from_str(&response.into_string().unwrap()).unwrap(); | ||
assert_eq!(todos[0].completed, true); | ||
} |
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,50 @@ | ||
use rocket::http::Status; | ||
use dooly::{db::TodoItem, helpers::{cleanup_database, establish_connection, run_seed_script, setup_rocket}}; | ||
use serde_json::json; | ||
use rocket::http::ContentType; | ||
|
||
#[test] | ||
fn test_update_todo() { | ||
cleanup_database(); // Clean up the database before starting the test | ||
let mut connection = establish_connection(); | ||
run_seed_script(&mut connection); // Seed the database with initial data | ||
|
||
let client = setup_rocket(); | ||
|
||
// Create a new todo item to update | ||
let new_todo = json!({ | ||
"title": "Initial Todo", | ||
"completed": false | ||
}); | ||
|
||
let response = client.post("/todos") | ||
.header(ContentType::JSON) | ||
.body(new_todo.to_string()) | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
assert_eq!(response.into_string().unwrap(), "Todo added successfully!"); | ||
|
||
// Update the existing todo item | ||
let updated_todo = json!({ | ||
"title": "Updated Todo Title", | ||
"completed": true | ||
}); | ||
|
||
let response = client.put("/todos/1") | ||
.header(ContentType::JSON) | ||
.body(updated_todo.to_string()) | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
assert_eq!(response.into_string().unwrap(), "Todo updated successfully!"); | ||
|
||
// Fetch the updated todo to verify the changes | ||
let response = client.get("/todos") | ||
.dispatch(); | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
let todos: Vec<TodoItem> = serde_json::from_str(&response.into_string().unwrap()).unwrap(); | ||
assert_eq!(todos[0].title, "Updated Todo Title"); | ||
assert_eq!(todos[0].completed, true); | ||
} |