-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added mock + test (as usage example)
- Loading branch information
Dmytro Horskyi
committed
Dec 18, 2023
1 parent
eef5d8e
commit 284051a
Showing
2 changed files
with
703 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.