test /read API for empty project

This commit is contained in:
Lucien Greathouse 2018-06-06 00:07:20 -07:00
parent 565bed2b9d
commit 9af6c9cfd9
3 changed files with 23 additions and 5 deletions

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use id::Id; use id::Id;
@ -23,6 +24,12 @@ pub struct RbxInstance {
pub parent: Option<Id>, pub parent: Option<Id>,
} }
impl<'a> From<&'a RbxInstance> for Cow<'a, RbxInstance> {
fn from(instance: &'a RbxInstance) -> Cow<'a, RbxInstance> {
Cow::Borrowed(instance)
}
}
pub struct RbxTree { pub struct RbxTree {
instances: HashMap<Id, RbxInstance>, instances: HashMap<Id, RbxInstance>,
} }
@ -81,7 +88,9 @@ impl RbxTree {
ids_deleted ids_deleted
} }
pub fn get_instance<'a, 'b>(&'a self, id: Id, output: &'b mut HashMap<Id, &'a RbxInstance>) { pub fn get_instance<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap<Id, T>)
where T: From<&'a RbxInstance>
{
let mut ids_to_visit = vec![id]; let mut ids_to_visit = vec![id];
loop { loop {
@ -92,7 +101,7 @@ impl RbxTree {
match self.instances.get(&id) { match self.instances.get(&id) {
Some(instance) => { Some(instance) => {
output.insert(id, instance); output.insert(id, instance.into());
for child_id in &instance.children { for child_id in &instance.children {
ids_to_visit.push(*child_id); ids_to_visit.push(*child_id);

View file

@ -49,12 +49,12 @@ pub struct ReadAllResponse<'a> {
pub instances: Cow<'a, HashMap<Id, RbxInstance>>, pub instances: Cow<'a, HashMap<Id, RbxInstance>>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ReadResponse<'a> { pub struct ReadResponse<'a> {
pub server_id: &'a str, pub server_id: &'a str,
pub message_cursor: i32, pub message_cursor: i32,
pub instances: HashMap<Id, &'a RbxInstance>, pub instances: HashMap<Id, Cow<'a, RbxInstance>>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]

View file

@ -13,7 +13,7 @@ use std::path::PathBuf;
use librojo::{ use librojo::{
session::Session, session::Session,
project::Project, project::Project,
web::{Server, WebConfig, ServerInfoResponse, ReadAllResponse}, web::{Server, WebConfig, ServerInfoResponse, ReadResponse, ReadAllResponse},
}; };
#[test] #[test]
@ -48,4 +48,13 @@ fn empty() {
assert_eq!(response.message_cursor, -1); assert_eq!(response.message_cursor, -1);
assert_eq!(response.instances.as_ref(), &HashMap::new()); assert_eq!(response.instances.as_ref(), &HashMap::new());
} }
{
let body = server.get_string("/api/read/0");
let response = serde_json::from_str::<ReadResponse>(&body).unwrap();
assert_eq!(response.server_id, "0");
assert_eq!(response.message_cursor, -1);
assert_eq!(response.instances, HashMap::new());
}
} }