From 562e0758f85579d4ec99b160f3e500cbbc57106b Mon Sep 17 00:00:00 2001 From: Franco Testagrossa Date: Sat, 11 May 2024 02:22:54 +0200 Subject: [PATCH] Fix cargo tests --- backend/src/bounty/api/accept.rs | 55 ++++++++++++---------- backend/src/bounty/api/claim.rs | 3 ++ backend/src/bounty/api/register_issue.rs | 10 ++-- backend/src/bounty/api/unregister_issue.rs | 4 ++ backend/src/lib.rs | 5 +- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/backend/src/bounty/api/accept.rs b/backend/src/bounty/api/accept.rs index 7f2caa3..c2d3a98 100644 --- a/backend/src/bounty/api/accept.rs +++ b/backend/src/bounty/api/accept.rs @@ -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; 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") @@ -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 = + 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 { @@ -83,10 +88,9 @@ 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, @@ -94,6 +98,7 @@ mod test_accept { }, github_issue_id.clone(), github_pr_id.clone(), + now ); BOUNTY_STATE.with(|state| { let bounty_canister = state.borrow(); diff --git a/backend/src/bounty/api/claim.rs b/backend/src/bounty/api/claim.rs index bd22682..740e06e 100644 --- a/backend/src/bounty/api/claim.rs +++ b/backend/src/bounty/api/claim.rs @@ -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}; @@ -106,6 +107,7 @@ mod test_claim { github_issue_id: &str, github_pr_id: &str, ) { + let now = 100u64; accept_impl( Contributor { address: Principal::from_text(principal).unwrap(), @@ -113,6 +115,7 @@ mod test_claim { }, github_issue_id.to_string(), github_pr_id.to_string(), + now ); } diff --git a/backend/src/bounty/api/register_issue.rs b/backend/src/bounty/api/register_issue.rs index 69134ca..b5a9e7a 100644 --- a/backend/src/bounty/api/register_issue.rs +++ b/backend/src/bounty/api/register_issue.rs @@ -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; @@ -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, @@ -67,7 +66,7 @@ mod test_register_issue { let bounty_amount: Nat = Nat(BigUint::from(100u32)); let r: Option = - 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()); @@ -98,10 +97,12 @@ mod test_register_issue { let bounty_amount: Nat = Nat(BigUint::from(100u32)); + let now = 100u64; let r: Option = register_issue_impl( contributor.clone(), github_issue_id.clone(), bounty_amount.clone(), + now ); assert!(r.is_none()); @@ -110,6 +111,7 @@ mod test_register_issue { contributor.clone(), github_issue_id.clone(), bounty_amount.clone(), + now ); assert!(r2.is_none()); diff --git a/backend/src/bounty/api/unregister_issue.rs b/backend/src/bounty/api/unregister_issue.rs index 0bd621c..6cf71e9 100644 --- a/backend/src/bounty/api/unregister_issue.rs +++ b/backend/src/bounty/api/unregister_issue.rs @@ -45,10 +45,12 @@ mod test_unregister_issue { let bounty_amount: Nat = Nat(BigUint::from(100u32)); + let now = 100u64; let r: Option = register_issue_impl( contributor.clone(), github_issue_id.clone(), bounty_amount.clone(), + now ); assert!(r.is_none()); @@ -83,10 +85,12 @@ mod test_unregister_issue { let bounty_amount: Nat = Nat(BigUint::from(100u32)); + let now = 100u64; let r: Option = register_issue_impl( contributor.clone(), github_issue_id.clone(), bounty_amount.clone(), + now ); assert!(r.is_none()); diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 1108586..5dab88e 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -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}; @@ -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] @@ -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]