Skip to content

Commit

Permalink
added mock + test (as usage example)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Horskyi committed Dec 18, 2023
1 parent eef5d8e commit 284051a
Show file tree
Hide file tree
Showing 2 changed files with 703 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/repo_contributors_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

use mock_error::setup_error_handler;
use octocrab::models::{Author, Contributor};
use octocrab::Octocrab;

/// Unit test for calls to the `/repos/OWNER/REPO/contributors` endpoint
mod mock_error;

const OWNER: &str = "XAMPPRocky";
const REPO: &str = "octocrab";

async fn setup_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path(format!("/repos/{OWNER}/{REPO}/contributors")))
.respond_with(template)
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
"GET on /repos/OWNER/REPO/contributors not called",
)
.await;
mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}

#[tokio::test]
async fn should_return_repo_contributors() {
let repo_contributors_response: Vec<Contributor> =
serde_json::from_str(include_str!("resources/repo_contributors.json")).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&repo_contributors_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client
.repos(OWNER, REPO)
.list_contributors()
.anon(true)
.send()
.await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);

let contributors = result.unwrap();

assert!(contributors.items.len() > 0);

let Contributor {
author: Author { login, .. },
contributions,
..
} = contributors.items.first().unwrap();

{
assert_eq!(login, "XAMPPRocky");
assert!(*contributions > 0);
}
}
Loading

0 comments on commit 284051a

Please sign in to comment.