Add some tests
This commit is contained in:
		
							parent
							
								
									bc46df634d
								
							
						
					
					
						commit
						c525f63660
					
				
					 6 changed files with 889 additions and 27 deletions
				
			
		| 
						 | 
				
			
			@ -49,3 +49,111 @@ impl Action for UpdatePersonDateOfBirth {
 | 
			
		|||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
    use crate::tests::apimock::MockServerExt;
 | 
			
		||||
    use httpmock::MockServer;
 | 
			
		||||
    use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
    use crate::{types::PersonResponseDto, Client};
 | 
			
		||||
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_describe() {
 | 
			
		||||
        let person = Person {
 | 
			
		||||
            id: Uuid::parse_str("123e4567-e89b-12d3-a456-426655440000").unwrap(),
 | 
			
		||||
            name: String::from("John Doe"),
 | 
			
		||||
            date_of_birth: None,
 | 
			
		||||
        };
 | 
			
		||||
        let date_of_birth = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
 | 
			
		||||
        let args = UpdatePersonDateOfBirthArgs {
 | 
			
		||||
            person,
 | 
			
		||||
            date_of_birth,
 | 
			
		||||
        };
 | 
			
		||||
        let action = UpdatePersonDateOfBirth::new(args);
 | 
			
		||||
 | 
			
		||||
        let description = action.describe();
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            description,
 | 
			
		||||
            "Updating John Doe's date of birth to 2000-01-01"
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[tokio::test]
 | 
			
		||||
    async fn calls_server() {
 | 
			
		||||
        let id = "123e4567-e89b-12d3-a456-426655440000";
 | 
			
		||||
        let new_date_of_birth = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
 | 
			
		||||
        let server = MockServer::start();
 | 
			
		||||
        let response = PersonResponseDto {
 | 
			
		||||
            id: String::from(id),
 | 
			
		||||
            name: String::from("John Doe"),
 | 
			
		||||
            birth_date: Some(new_date_of_birth),
 | 
			
		||||
            is_hidden: false,
 | 
			
		||||
            thumbnail_path: String::from("/foo/bar.jpg"),
 | 
			
		||||
            updated_at: None,
 | 
			
		||||
        };
 | 
			
		||||
        let mock = server.update_person(|when, then| {
 | 
			
		||||
            when.id(&Uuid::from_str(id).unwrap())
 | 
			
		||||
                .body(&PersonUpdateDto {
 | 
			
		||||
                    birth_date: Some(new_date_of_birth),
 | 
			
		||||
                    feature_face_asset_id: None,
 | 
			
		||||
                    is_hidden: None,
 | 
			
		||||
                    name: None,
 | 
			
		||||
                });
 | 
			
		||||
            then.ok(&response);
 | 
			
		||||
        });
 | 
			
		||||
        let client = Client::new(&server.url(""));
 | 
			
		||||
        let ctx = Context::builder()
 | 
			
		||||
            .client(client)
 | 
			
		||||
            .dry_run(false)
 | 
			
		||||
            .no_confirm(true)
 | 
			
		||||
            .build();
 | 
			
		||||
 | 
			
		||||
        UpdatePersonDateOfBirth::new(UpdatePersonDateOfBirthArgs {
 | 
			
		||||
            person: Person {
 | 
			
		||||
                id: Uuid::from_str(id).unwrap(),
 | 
			
		||||
                name: String::from("John Doe"),
 | 
			
		||||
                date_of_birth: None,
 | 
			
		||||
            },
 | 
			
		||||
            date_of_birth: new_date_of_birth,
 | 
			
		||||
        })
 | 
			
		||||
        .execute(&ctx)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
        mock.assert_async().await;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[tokio::test]
 | 
			
		||||
    async fn does_not_call_server_in_dry_run() {
 | 
			
		||||
        let id = "123e4567-e89b-12d3-a456-426655440000";
 | 
			
		||||
        let new_date_of_birth = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
 | 
			
		||||
        let server = MockServer::start();
 | 
			
		||||
        let mock = server.update_person(|_when, _then| {});
 | 
			
		||||
        let client = Client::new(&server.url(""));
 | 
			
		||||
        let ctx = Context::builder()
 | 
			
		||||
            .client(client)
 | 
			
		||||
            .dry_run(true)
 | 
			
		||||
            .no_confirm(true)
 | 
			
		||||
            .build();
 | 
			
		||||
 | 
			
		||||
        UpdatePersonDateOfBirth::new(UpdatePersonDateOfBirthArgs {
 | 
			
		||||
            person: Person {
 | 
			
		||||
                id: Uuid::from_str(id).unwrap(),
 | 
			
		||||
                name: String::from("John Doe"),
 | 
			
		||||
                date_of_birth: None,
 | 
			
		||||
            },
 | 
			
		||||
            date_of_birth: new_date_of_birth,
 | 
			
		||||
        })
 | 
			
		||||
        .execute(&ctx)
 | 
			
		||||
        .await
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
        mock.assert_hits_async(0).await;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,4 +1,12 @@
 | 
			
		|||
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
 | 
			
		||||
include!(concat!(env!("OUT_DIR"), "/client.rs"));
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    #[allow(dead_code)]
 | 
			
		||||
    pub mod apimock {
 | 
			
		||||
        include!(concat!(env!("OUT_DIR"), "/mock.rs"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
use crate::args::Commands;
 | 
			
		||||
use args::{AssetsCommands, PeopleCommands, ServerCommands};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,3 +19,47 @@ impl From<PersonResponseDto> for Person {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn from_person_response_dto_success() {
 | 
			
		||||
        let dto = PersonResponseDto {
 | 
			
		||||
            id: String::from("123e4567-e89b-12d3-a456-426655440000"),
 | 
			
		||||
            name: String::from("John Doe"),
 | 
			
		||||
            birth_date: Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()),
 | 
			
		||||
            is_hidden: false,
 | 
			
		||||
            thumbnail_path: String::from("/foo/bar.jpg"),
 | 
			
		||||
            updated_at: None,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let person = Person::from(dto);
 | 
			
		||||
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            person.id,
 | 
			
		||||
            Uuid::parse_str("123e4567-e89b-12d3-a456-426655440000").unwrap()
 | 
			
		||||
        );
 | 
			
		||||
        assert_eq!(person.name, "John Doe".to_string());
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            person.date_of_birth,
 | 
			
		||||
            Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    #[should_panic]
 | 
			
		||||
    fn from_person_response_dto_invalid_uuid_panics() {
 | 
			
		||||
        let dto = PersonResponseDto {
 | 
			
		||||
            id: String::from("invalid_uuid"),
 | 
			
		||||
            name: String::from("John Doe"),
 | 
			
		||||
            birth_date: Some(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()),
 | 
			
		||||
            is_hidden: false,
 | 
			
		||||
            thumbnail_path: String::from("/foo/bar.jpg"),
 | 
			
		||||
            updated_at: None,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        let _ = Person::from(dto);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue