From a28e4186b97b562e28bbbd95b8ae2051e9f9db7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Tue, 27 Aug 2024 17:30:32 +0200 Subject: [PATCH] Add some tests for collect_approved_reviews --- src/api_clients.rs | 1 + src/changes.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++ src/helm_config.rs | 1 + 3 files changed, 62 insertions(+) diff --git a/src/api_clients.rs b/src/api_clients.rs index 24750a9..91cb653 100644 --- a/src/api_clients.rs +++ b/src/api_clients.rs @@ -53,6 +53,7 @@ impl ClientSet { Ok(()) } + // TODO: add test fn get_client(&mut self, host: &str) -> Result, anyhow::Error> { if let Some(client) = self.clients.get(host) { return Ok(client.clone()); diff --git a/src/changes.rs b/src/changes.rs index b11bb54..b6f9e9b 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -103,6 +103,7 @@ pub struct Changeset { impl Changeset { // pr_reviews must be sorted by key submitted_at! + // TODO: add test pub fn collect_approved_reviews(&mut self, pr_reviews: &[Review], head_sha: &String) { let mut last_review_by: Vec = vec![]; @@ -156,3 +157,62 @@ impl CommitMetadata { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::github::Review; + + fn gen_change_review() -> (Changeset, Vec) { + ( + Changeset { + commits: vec![ + CommitMetadata { + headline: "Commit 1".to_owned(), + link: "https://github.com/example/project/commit/00000000000000000000000000000001".to_owned(), + }, + CommitMetadata { + headline: "Commit 2".to_owned(), + link: "https://github.com/example/project/commit/00000000000000000000000000000002".to_owned(), + }, + ], + pr_link: Some("https://github.com/example/project/pulls/1".to_owned()), + approvals: Vec::new(), + }, + vec![ + Review { + approved: true, + commit_id: "00000000000000000000000000000001".to_owned(), + submitted_at: 1, + user: "user1".to_owned(), + }, + Review { + approved: true, + commit_id: "00000000000000000000000000000002".to_owned(), + submitted_at: 2, + user: "user2".to_owned(), + }, + Review { + approved: false, + commit_id: "00000000000000000000000000000003".to_owned(), + submitted_at: 3, + user: "user3".to_owned(), + }, + ], + ) + } + + #[test] + fn collect_approved_reviews() { + let (mut changeset, pr_reviews) = gen_change_review(); + changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000002".to_string()); + assert_eq!(changeset.approvals, vec!["user2"]); + } + + #[test] + fn collect_approved_reviews_extra_commit() { + let (mut changeset, pr_reviews) = gen_change_review(); + changeset.collect_approved_reviews(&pr_reviews, &"00000000000000000000000000000003".to_string()); + assert_eq!(changeset.approvals, Vec::::new()); + } +} diff --git a/src/helm_config.rs b/src/helm_config.rs index 20ecebb..654f52f 100644 --- a/src/helm_config.rs +++ b/src/helm_config.rs @@ -26,6 +26,7 @@ pub struct ImageRefs { } impl ImageRefs { + // TODO: add test pub fn parse(repo: &Repository, diff_file: &DiffFile) -> Result { let blob_id = diff_file.id(); let blob = repo