Skip to content

Commit

Permalink
Fix cargo tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ffakenz committed May 11, 2024
1 parent eb9f893 commit 562e075
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
55 changes: 30 additions & 25 deletions backend/src/bounty/api/accept.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
use super::state::{Contributor, PullRequest, BOUNTY_STATE};
use super::state::{Contributor, IssueId, PullRequest, PullRequestId, Time, BOUNTY_STATE};

use ic_cdk::api::time;
use candid::CandidType;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, CandidType)]
pub enum AcceptError {
IssueNotFound { github_issue_id: String },
CantAcceptedTwice,
}

pub type AcceptReceipt = Option<AcceptError>;

pub fn accept_impl(
contributor: Contributor,
github_issue_id: String,
github_pr_id: String,
github_issue_id: IssueId,
github_pr_id: PullRequestId,
now: Time
) -> AcceptReceipt {
return BOUNTY_STATE.with(|state| {
if let Some(ref mut bounty_canister) = *state.borrow_mut() {
let mut issue_exists = false;
let mut pr_exists = false;

if let Some(ref mut issue) = bounty_canister.github_issues.get_mut(&github_issue_id) {
issue_exists = true;
if !issue.bounty.accepted_prs.contains_key(&github_pr_id) {
let now = time();
let pr = PullRequest {
id: github_pr_id.clone(),
contributor,
accepted_at: now,
updated_at: now
};
// TODO: Check contributor it's registered and github_issue_id exists on github
issue.bounty.accepted_prs.insert(github_pr_id.clone(), pr);
pr_exists = true;
}
}


let issue_exists = bounty_canister.github_issues.contains_key(&github_issue_id);
if !issue_exists {
Some(AcceptError::IssueNotFound { github_issue_id });
}

if !pr_exists {
Some(AcceptError::CantAcceptedTwice);
return Some(AcceptError::IssueNotFound { github_issue_id });
}

// TODO check the issue is not closed and has no winner already
None
} else {
panic!("Bounty canister state not initialized")
Expand All @@ -55,16 +47,29 @@ pub fn accept_impl(
#[cfg(test)]
mod test_accept {
use super::*;
use crate::bounty::api::init::init_impl;
use candid::Principal;
use crate::bounty::api::{init::init_impl, register_issue::{register_issue_impl, RegisterIssueError}};
use candid::{Nat, Principal};
use num_bigint::BigUint;

#[test]
fn test_accept() {
let authority =
Principal::from_text("t2y5w-qp34w-qixaj-s67wp-syrei-5yqse-xbed6-z5nsd-fszmf-izgt2-lqe")
.unwrap();
let authority = Principal::anonymous();
init_impl(authority);
let github_issue_id = "input-output-hk/hydra/issues/1370".to_string();

let contributor = Contributor {
address: Principal::anonymous(),
crypto_address: "0x1234".to_string(),
};

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let now = 100u64;
let r: Option<RegisterIssueError> =
register_issue_impl(contributor, github_issue_id.clone(), bounty_amount, now);

assert!(r.is_none());

BOUNTY_STATE.with(|state| {
let bounty_canister = state.borrow();
if let Some(ref bounty_canister) = *bounty_canister {
Expand All @@ -83,17 +88,17 @@ mod test_accept {
}
});

let contributor =
Principal::from_text("t2y5w-qp34w-qixaj-s67wp-syrei-5yqse-xbed6-z5nsd-fszmf-izgt2-lqe")
.unwrap();
let contributor = Principal::anonymous();
let github_pr_id = "input-output-hk/hydra/pull/1266".to_string();
let now = 100u64;
accept_impl(
Contributor {
address: contributor,
crypto_address: "contributor_address".to_string(),
},
github_issue_id.clone(),
github_pr_id.clone(),
now
);
BOUNTY_STATE.with(|state| {
let bounty_canister = state.borrow();
Expand Down
3 changes: 3 additions & 0 deletions backend/src/bounty/api/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ mod test_claim {
use crate::bounty::api::state::{Contributor, BOUNTY_STATE};
use candid::Principal;
use futures::executor::block_on;
use ic_cdk::api::time;

use super::{claim_impl, ClaimError, GithubClientMock};

Expand All @@ -106,13 +107,15 @@ mod test_claim {
github_issue_id: &str,
github_pr_id: &str,
) {
let now = 100u64;
accept_impl(
Contributor {
address: Principal::from_text(principal).unwrap(),
crypto_address: crypto_address.to_string(),
},
github_issue_id.to_string(),
github_pr_id.to_string(),
now
);
}

Expand Down
10 changes: 6 additions & 4 deletions backend/src/bounty/api/register_issue.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use ic_cdk::api::time;
use super::state::IssueId;
use super::state::{IssueId, Time};
use super::state::{Bounty, Contributor, Issue, BOUNTY_STATE};

use candid::Nat;
Expand All @@ -14,12 +13,12 @@ pub fn register_issue_impl(
contributor: Contributor,
github_issue_id: IssueId,
amount: Nat,
now: Time
) -> RegisterIssueReceipt {
return BOUNTY_STATE.with(|state| {
if let Some(ref mut bounty_canister) = *state.borrow_mut() {
let issue_exists = bounty_canister.github_issues.contains_key(&github_issue_id);
if !issue_exists {
let now = time();
let github_issue = Issue {
id: github_issue_id.clone(),
maintainer: contributor,
Expand Down Expand Up @@ -67,7 +66,7 @@ mod test_register_issue {
let bounty_amount: Nat = Nat(BigUint::from(100u32));

let r: Option<RegisterIssueError> =
register_issue_impl(contributor, github_issue_id.clone(), bounty_amount);
register_issue_impl(contributor, github_issue_id.clone(), bounty_amount, 100u64);

assert!(r.is_none());

Expand Down Expand Up @@ -98,10 +97,12 @@ mod test_register_issue {

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let now = 100u64;
let r: Option<RegisterIssueError> = register_issue_impl(
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
now
);

assert!(r.is_none());
Expand All @@ -110,6 +111,7 @@ mod test_register_issue {
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
now
);

assert!(r2.is_none());
Expand Down
4 changes: 4 additions & 0 deletions backend/src/bounty/api/unregister_issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ mod test_unregister_issue {

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let now = 100u64;
let r: Option<RegisterIssueError> = register_issue_impl(
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
now
);

assert!(r.is_none());
Expand Down Expand Up @@ -83,10 +85,12 @@ mod test_unregister_issue {

let bounty_amount: Nat = Nat(BigUint::from(100u32));

let now = 100u64;
let r: Option<RegisterIssueError> = register_issue_impl(
contributor.clone(),
github_issue_id.clone(),
bounty_amount.clone(),
now
);

assert!(r.is_none());
Expand Down
5 changes: 3 additions & 2 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod provider {
pub mod utils;
}
}
use ic_cdk::api::time;
use provider::github::api::get_fixed_by::FixedByErr;
use provider::github::api::get_is_merged::IsMergedErr;
use provider::github::api::get_issue::{IssueErr, IssueResponse};
Expand Down Expand Up @@ -106,7 +107,7 @@ fn accept(
github_issue_id: IssueId,
github_pr_id: PullRequestId,
) -> AcceptReceipt {
return accept_impl(contributor, github_issue_id, github_pr_id);
return accept_impl(contributor, github_issue_id, github_pr_id, time());
}

#[ic_cdk::update]
Expand All @@ -125,7 +126,7 @@ fn register_issue(
github_issue_id: IssueId,
amount: Nat,
) -> RegisterIssueReceipt {
return register_issue_impl(contributor, github_issue_id, amount);
return register_issue_impl(contributor, github_issue_id, amount, time());
}

#[ic_cdk::update]
Expand Down

0 comments on commit 562e075

Please sign in to comment.