Skip to content

Commit

Permalink
Make most fields of models::Author optional
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Jun 20, 2024
1 parent 06f4ca6 commit 0195de3
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/list_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> octocrab::Result<()> {
.await?;

for f in forks {
println!("fork: {}", f.owner.unwrap().login);
println!("fork: {}", f.owner.unwrap().login.unwrap());
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/list_gists_for_token_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> octocrab::Result<()> {

println!(
"User '{username}' has {count} gists:",
username = current_user_name,
username = current_user_name.unwrap(),
count = gists.len()
);
println!("id | url | [files...] | description");
Expand Down
54 changes: 36 additions & 18 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,42 @@ pub struct IssuePullRequest {
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Author {
pub login: String,
pub id: UserId,
pub node_id: String,
pub avatar_url: Url,
pub gravatar_id: String,
pub url: Url,
pub html_url: Url,
pub followers_url: Url,
pub following_url: Url,
pub gists_url: Url,
pub starred_url: Url,
pub subscriptions_url: Url,
pub organizations_url: Url,
pub repos_url: Url,
pub events_url: Url,
pub received_events_url: Url,
pub r#type: String,
pub site_admin: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub login: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<UserId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub node_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gravatar_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub html_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub followers_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub following_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gists_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub starred_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subscriptions_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub organizations_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repos_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub events_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub received_events_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub site_admin: Option<bool>,
pub patch_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/models/events/payload/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod test {
event.payload.as_ref().unwrap().specific
{
assert_eq!(payload.action, MemberEventAction::Added);
assert_eq!(payload.member.id.0, 58522265);
assert_eq!(payload.member.id.unwrap().0, 58522265);
} else {
panic!("unexpected event payload encountered: {:#?}", event.payload);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/current_user_orgs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn should_return_page_with_invitations() {
{
assert_eq!(items.len(), 2);
assert_eq!(items[0].role, "admin");
assert_eq!(items[0].user.login, "davidmhewitt");
assert_eq!(items[0].user.login.as_ref().unwrap(), "davidmhewitt");
assert_eq!(items[0].organization.login, "elementary");
assert_eq!(items[1].organization.login, "EpicGames");
}
Expand Down
7 changes: 5 additions & 2 deletions tests/follow_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const REPO: &str = "repo";
async fn should_return_page_with_users() {
let star_gazers: Vec<StarGazer> =
serde_json::from_str(include_str!("resources/stargazers.json")).unwrap();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone().unwrap();
let page_response = FakePage { items: star_gazers };
let template = ResponseTemplate::new(200).set_body_json(&page_response);
let mock_server = setup_api(template).await;
Expand All @@ -71,6 +71,9 @@ async fn should_return_page_with_users() {
let Page { items, .. } = result.unwrap();
{
assert_eq!(items.len(), 3);
assert_eq!(items[0].user.as_ref().unwrap().login, login1);
assert_eq!(
items[0].user.as_ref().unwrap().login.as_ref().unwrap(),
&login1
);
}
}
2 changes: 1 addition & 1 deletion tests/org_installations_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ async fn should_return_org_installation() {
} = result.unwrap();
{
assert_eq!(installation_id, InstallationId(1));
assert_eq!(login, "github");
assert_eq!(login.unwrap(), "github");
}
}
4 changes: 2 additions & 2 deletions tests/org_members_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const USERNAME: &str = "mona";
async fn should_return_page_with_users() {
let org_members: Author =
serde_json::from_str(include_str!("resources/org_members.json")).unwrap();
let login: String = org_members.login.clone();
let login: String = org_members.login.clone().unwrap();
let page_response = FakePage {
items: vec![org_members],
};
Expand All @@ -78,7 +78,7 @@ async fn should_return_page_with_users() {
let Page { items, .. } = result.unwrap();
{
assert_eq!(items.len(), 1);
assert_eq!(items[0].login, login);
assert_eq!(items[0].login.clone().unwrap(), login);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/projects_project_get_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ async fn should_get_projects_by_its_id() {
let result = project.unwrap();

assert_eq!(result.name, "Organization Roadmap");
assert_eq!(result.creator.login, "octocat");
assert_eq!(result.creator.login.unwrap(), "octocat");
}
2 changes: 1 addition & 1 deletion tests/projects_user_list_projects_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ async fn should_list_user_projects() {
let Page { items, .. } = result.unwrap();

assert_eq!(items[0].name, "My Projects");
assert_eq!(items[1].creator.login, "octocat");
assert_eq!(items[1].creator.login.as_ref().unwrap(), "octocat");
}
2 changes: 1 addition & 1 deletion tests/repo_contributors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn should_return_repo_contributors() {
} = contributors.items.first().unwrap();

{
assert_eq!(login, "XAMPPRocky");
assert_eq!(login.as_ref().unwrap(), "XAMPPRocky");
assert!(*contributions > 0);
}
}
12 changes: 6 additions & 6 deletions tests/repos_stargazers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const REPO: &str = "repo";
async fn should_return_page_with_users() {
let star_gazers: Vec<StarGazer> =
serde_json::from_str(include_str!("resources/stargazers.json")).unwrap();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone().unwrap();
let page_response = FakePage { items: star_gazers };
let template = ResponseTemplate::new(200).set_body_json(&page_response);
let mock_server = setup_api(template).await;
Expand All @@ -57,16 +57,16 @@ async fn should_return_page_with_users() {
let Page { items, .. } = result.unwrap();
{
assert_eq!(items.len(), 3);
assert_eq!(items[0].user.as_ref().unwrap().login, login1);
assert_eq!(items[0].user.as_ref().unwrap().login, Some(login1));
}
}

#[tokio::test]
async fn should_return_page_with_all_users() {
let star_gazers: Vec<StarGazer> =
serde_json::from_str(include_str!("resources/stargazers.json")).unwrap();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone();
let login2: String = star_gazers[1].user.as_ref().unwrap().login.clone();
let login1: String = star_gazers[0].user.as_ref().unwrap().login.clone().unwrap();
let login2: String = star_gazers[1].user.as_ref().unwrap().login.clone().unwrap();
let page_response = FakePage { items: star_gazers };
let template = ResponseTemplate::new(200).set_body_json(&page_response);
let mock_server = setup_api(template).await;
Expand All @@ -82,7 +82,7 @@ async fn should_return_page_with_all_users() {

let result = client.all_pages(page).await.unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result[0].user.as_ref().unwrap().login, login1);
assert_eq!(result[1].user.as_ref().unwrap().login, login2);
assert_eq!(result[0].user.as_ref().unwrap().login, Some(login1));
assert_eq!(result[1].user.as_ref().unwrap().login, Some(login2));
assert_eq!(result[2].user, None);
}
2 changes: 1 addition & 1 deletion tests/team_invitations_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn should_return_page_with_invitations() {
{
assert_eq!(items.len(), 1);
assert_eq!(items[0].login.clone().unwrap(), String::from("monalisa"));
assert_eq!(items[0].inviter.r#type, String::from("User"));
assert_eq!(items[0].inviter.r#type, Some(String::from("User")));
assert_eq!(items[0].role, String::from("direct_member"));
}
}
4 changes: 2 additions & 2 deletions tests/team_members_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn should_return_page_with_users() {
let Page { items, .. } = result.unwrap();
{
assert_eq!(items.len(), 1);
assert_eq!(items[0].login, String::from("octocat"));
assert_eq!(items[0].r#type, String::from("User"));
assert_eq!(items[0].login.clone().unwrap(), String::from("octocat"));
assert_eq!(items[0].r#type.clone().unwrap(), String::from("User"));
}
}

0 comments on commit 0195de3

Please sign in to comment.