diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..73ef42a --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,3 @@ +[development] +address = "0.0.0.0" +port = 3000 diff --git a/src/handlers/list.rs b/src/handlers/list.rs index 75479cf..1788952 100644 --- a/src/handlers/list.rs +++ b/src/handlers/list.rs @@ -112,13 +112,11 @@ mod tests { let mut res = client.get("/lists/updatable").header(Accept::JSON).dispatch(); let body = res.body_string().unwrap(); let list = serde_json::from_str::(body.as_str()).unwrap(); - // Switch contentA with contentB and vice versa - let new_data = if list.data == "contentA" { - "contentB" - } else { - "contentA" - }; - let new_json = json!({ + assert_eq!(list.data, "Some content"); + + // Try to change the list data, then verify it was changed + let new_data = "New content"; + let mut new_json: serde_json::Value = json!({ "id": "updatable", "index": 1, "data": new_data, @@ -129,8 +127,6 @@ mod tests { "name": "Updatable", "path": "test/lists/updatablelist" }); - - // Try to change the list data, then verify it was changed let res = client.put("/lists/updatable") .header(ContentType::JSON) .body(new_json.to_string()) @@ -144,6 +140,14 @@ mod tests { let list = serde_json::from_str::(body.as_str()).unwrap(); assert_eq!(list.data, new_data); + // ... and change it back + *new_json.get_mut("data").unwrap() = json!("Some content"); + let res = client.put("/lists/updatable") + .header(ContentType::JSON) + .body(new_json.to_string()) + .dispatch(); + assert_eq!(res.status(), Status::Ok); + // Try to change a list that doesn't exist let res = client.put("/lists/doesntexit") .header(ContentType::JSON) diff --git a/src/models/list.rs b/src/models/list.rs index 74ae8b6..91fc5b9 100644 --- a/src/models/list.rs +++ b/src/models/list.rs @@ -116,20 +116,22 @@ mod tests { let list = lists.iter_mut() .find(|list| list.id == "updatable") .unwrap(); + assert_eq!(list.data, "Some content"); - let new_data = match list.data.as_ref() { - "contentA" => "contentB", - _ => "contentA" - }; + // Update the data van verify it has changed + let new_data = "New content"; list.update_data(&String::from(new_data)); assert_eq!(list.data, new_data); // Verify that the data is written to the file of the list by // loading them again - let lists = List::load_all(Some("test")); - let list = lists.iter() + let mut lists = List::load_all(Some("test")); + let list = lists.iter_mut() .find(|list| list.id == "updatable") .unwrap(); assert_eq!(list.data, new_data); + + // ... and change it back again + list.update_data(&String::from("Some content")); } } diff --git a/test/lists/updatable.list b/test/lists/updatable.list index f53431b..e041e7d 100644 --- a/test/lists/updatable.list +++ b/test/lists/updatable.list @@ -1 +1 @@ -contentB \ No newline at end of file +Some content \ No newline at end of file