From b621cab388c2a18ddc73b3acb853d36ff1335ceb Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Thu, 19 Dec 2024 12:40:55 +0000 Subject: [PATCH] Add tests, adjust code. --- R/GitHost.R | 9 ++-- R/test_helpers.R | 22 +++++++++ tests/testthat/_snaps/01-get_repos-GitHub.md | 7 +++ tests/testthat/_snaps/01-get_repos-GitLab.md | 6 --- .../testthat/_snaps/02-get_commits-GitHub.md | 8 ++++ tests/testthat/_snaps/get_release-GitHub.md | 8 ++++ tests/testthat/test-01-get_repos-GitHub.R | 48 +++++++++++++++++++ tests/testthat/test-02-get_commits-GitHub.R | 39 ++++++++++++--- tests/testthat/test-get_release-GitHub.R | 27 +++++++++++ 9 files changed, 158 insertions(+), 16 deletions(-) diff --git a/R/GitHost.R b/R/GitHost.R index 3f6a496e..c7aa46b2 100644 --- a/R/GitHost.R +++ b/R/GitHost.R @@ -125,7 +125,8 @@ GitHost <- R6::R6Class( progress = TRUE) { if (private$scan_all && is.null(private$orgs) && verbose) { cli::cli_alert_info("[{private$host_name}][Engine:{cli::col_yellow('GraphQL')}] Pulling all organizations...") - private$orgs <- private$engines$graphql$get_orgs() + graphql_engine <- private$engines$graphql + private$orgs <- graphql_engine$get_orgs() } commits_from_orgs <- private$get_commits_from_orgs( since = since, @@ -234,7 +235,8 @@ GitHost <- R6::R6Class( if (verbose) { cli::cli_alert_info("[{private$host_name}][Engine:{cli::col_yellow('GraphQL')}] Pulling all organizations...") } - private$orgs <- private$engines$graphql$get_orgs() + graphql_engine <- private$engines$graphql + private$orgs <- graphql_engine$get_orgs() } until <- until %||% Sys.time() release_logs_from_orgs <- private$get_release_logs_from_orgs( @@ -604,7 +606,8 @@ GitHost <- R6::R6Class( information = "Pulling all organizations" ) } - private$orgs <- private$engines$graphql$get_orgs() + graphql_engine <- private$engines$graphql + private$orgs <- graphql_engine$get_orgs() } repos_table <- purrr::list_rbind( list( diff --git a/R/test_helpers.R b/R/test_helpers.R index 056e2a86..18afac35 100644 --- a/R/test_helpers.R +++ b/R/test_helpers.R @@ -127,6 +127,28 @@ create_github_testhost <- function(host = NULL, return(test_host) } +#' @noRd +create_github_testhost_all <- function(host = NULL, + orgs = NULL, + repos = NULL, + token = NULL, + mode = "") { + suppressMessages( + test_host <- GitHostGitHubTest$new( + host = NULL, + token = token, + orgs = orgs, + repos = repos + ) + ) + test_host$.__enclos_env__$private$orgs <- NULL + test_host$.__enclos_env__$private$scan_all <- TRUE + if (mode == "private") { + test_host <- environment(test_host$initialize)$private + } + return(test_host) +} + #' @noRd create_gitlab_testhost <- function(host = NULL, orgs = NULL, diff --git a/tests/testthat/_snaps/01-get_repos-GitHub.md b/tests/testthat/_snaps/01-get_repos-GitHub.md index 2edba7cf..bafe0009 100644 --- a/tests/testthat/_snaps/01-get_repos-GitHub.md +++ b/tests/testthat/_snaps/01-get_repos-GitHub.md @@ -5,3 +5,10 @@ Output [1] "\n query GetReposByOrg($login: String!) {\n repositoryOwner(login: $login) {\n ... on Organization {\n \n repositories(first: 100) {\n totalCount\n pageInfo {\n endCursor\n hasNextPage\n }\n nodes {\n repo_id: id\n repo_name: name\n default_branch: defaultBranchRef {\n name\n }\n stars: stargazerCount\n forks: forkCount\n created_at: createdAt\n last_activity_at: pushedAt\n languages (first: 5) { nodes {name} }\n issues_open: issues (first: 100 states: [OPEN]) {\n totalCount\n }\n issues_closed: issues (first: 100 states: [CLOSED]) {\n totalCount\n }\n organization: owner {\n login\n }\n repo_url: url\n }\n }\n \n }\n }\n }" +# `get_all_repos()` is set to scan whole git host + + Code + gh_repos <- github_testhost_all_priv$get_all_repos(verbose = TRUE, progress = FALSE) + Message + i [Host:GitHub][Engine:GraphQl] Pulling all organizations... + diff --git a/tests/testthat/_snaps/01-get_repos-GitLab.md b/tests/testthat/_snaps/01-get_repos-GitLab.md index 9e412a4f..d84ced03 100644 --- a/tests/testthat/_snaps/01-get_repos-GitLab.md +++ b/tests/testthat/_snaps/01-get_repos-GitLab.md @@ -13,9 +13,3 @@ Message i [Host:GitLab][Engine:GraphQl][Scope:mbtests] Pulling repositories... -# get_repos_from_repos prints proper message - - Code - gl_repos_from_repos <- gitlab_testhost_priv$get_repos_from_repos(verbose = TRUE, - progress = FALSE) - diff --git a/tests/testthat/_snaps/02-get_commits-GitHub.md b/tests/testthat/_snaps/02-get_commits-GitHub.md index 5fe49647..8c59addf 100644 --- a/tests/testthat/_snaps/02-get_commits-GitHub.md +++ b/tests/testthat/_snaps/02-get_commits-GitHub.md @@ -9,3 +9,11 @@ i GraphQL response error +# `get_commits()` is set to scan whole git host + + Code + gh_commits_table <- github_testhost_all$get_commits(since = "2023-01-01", + until = "2023-02-28", verbose = TRUE, progress = FALSE) + Message + i [GitHub][Engine:GraphQL] Pulling all organizations... + diff --git a/tests/testthat/_snaps/get_release-GitHub.md b/tests/testthat/_snaps/get_release-GitHub.md index 116312b0..94bc9e8e 100644 --- a/tests/testthat/_snaps/get_release-GitHub.md +++ b/tests/testthat/_snaps/get_release-GitHub.md @@ -5,3 +5,11 @@ Output [1] "query GetReleasesFromRepo ($org: String!, $repo: String!) {\n repository(owner:$org, name:$repo){\n name\n url\n releases (last: 100) {\n nodes {\n name\n tagName\n publishedAt\n url\n description\n }\n }\n }\n }" +# `get_release_logs()` is set to scan whole git host + + Code + gh_releases_table <- github_testhost_all$get_release_logs(since = "2023-01-01", + until = "2023-02-28", verbose = TRUE, progress = FALSE) + Message + i [GitHub][Engine:GraphQL] Pulling all organizations... + diff --git a/tests/testthat/test-01-get_repos-GitHub.R b/tests/testthat/test-01-get_repos-GitHub.R index 0e34136e..e86c31bf 100644 --- a/tests/testthat/test-01-get_repos-GitHub.R +++ b/tests/testthat/test-01-get_repos-GitHub.R @@ -395,6 +395,7 @@ test_that("`get_repos_with_code_from_host()` pulls raw response", { ) expect_type(repos_with_code_from_host_raw, "list") expect_gt(length(repos_with_code_from_host_raw), 0) + test_mocker$cache(repos_with_code_from_host_raw) }) test_that("get_repos_with_code() works", { @@ -432,6 +433,25 @@ test_that("get_repos_with_code() works", { test_mocker$cache(github_repos_with_code_min) }) + +test_that("get_repos_with_code() scans whole host", { + mockery::stub( + github_testhost_priv$get_repos_with_code, + "private$get_repos_with_code_from_host", + test_mocker$use("repos_with_code_from_host_raw") + ) + github_testhost_priv$scan_all <- TRUE + github_repos_with_code_raw <- github_testhost_priv$get_repos_with_code( + code = "test-code", + output = "raw", + verbose = FALSE, + progress = FALSE + ) + expect_type(github_repos_with_code_raw, "list") + expect_gt(length(github_repos_with_code_raw), 0) + github_testhost_priv$scan_all <- FALSE +}) + test_that("GitHub prepares repos table from repositories response", { gh_repos_table <- test_graphql_github$prepare_repos_table( repos_list = test_mocker$use("gh_repos_from_org") @@ -504,6 +524,34 @@ test_that("`get_all_repos()` works as expected", { test_mocker$cache(gh_repos_table) }) +test_that("`get_all_repos()` is set to scan whole git host", { + github_testhost_all_priv <- create_github_testhost_all( + orgs = "test_org", + mode = "private" + ) + mockery::stub( + github_testhost_all_priv$get_all_repos, + "graphql_engine$get_orgs", + "test_org" + ) + mockery::stub( + github_testhost_all_priv$get_all_repos, + "private$get_repos_from_orgs", + test_mocker$use("gh_repos_from_orgs") + ) + mockery::stub( + github_testhost_all_priv$get_all_repos, + "private$get_repos_from_repos", + test_mocker$use("gh_repos_individual") + ) + expect_snapshot( + gh_repos <- github_testhost_all_priv$get_all_repos( + verbose = TRUE, + progress = FALSE + ) + ) +}) + test_that("GitHost adds `repo_api_url` column to GitHub repos table", { repos_table <- test_mocker$use("gh_repos_table") gh_repos_table_with_api_url <- github_testhost_priv$add_repo_api_url(repos_table) diff --git a/tests/testthat/test-02-get_commits-GitHub.R b/tests/testthat/test-02-get_commits-GitHub.R index a42025e5..38f0c5e1 100644 --- a/tests/testthat/test-02-get_commits-GitHub.R +++ b/tests/testthat/test-02-get_commits-GitHub.R @@ -177,16 +177,41 @@ test_that("`get_commits()` retrieves commits in the table format", { "private$get_commits_from_repos", test_mocker$use("gh_commits_from_repos") ) - suppressMessages( - gh_commits_table <- github_testhost$get_commits( - since = "2023-01-01", - until = "2023-02-28", - verbose = FALSE, - progress = FALSE - ) + gh_commits_table <- github_testhost$get_commits( + since = "2023-01-01", + until = "2023-02-28", + verbose = FALSE, + progress = FALSE ) expect_commits_table( gh_commits_table ) test_mocker$cache(gh_commits_table) }) + +test_that("`get_commits()` is set to scan whole git host", { + github_testhost_all <- create_github_testhost_all(orgs = "test_org") + mockery::stub( + github_testhost_all$get_commits, + "graphql_engine$get_orgs", + "test_org" + ) + mockery::stub( + github_testhost_all$get_commits, + "private$get_commits_from_orgs", + test_mocker$use("gh_commits_from_orgs") + ) + mockery::stub( + github_testhost_all$get_commits, + "private$get_commits_from_repos", + test_mocker$use("gh_commits_from_repos") + ) + expect_snapshot( + gh_commits_table <- github_testhost_all$get_commits( + since = "2023-01-01", + until = "2023-02-28", + verbose = TRUE, + progress = FALSE + ) + ) +}) diff --git a/tests/testthat/test-get_release-GitHub.R b/tests/testthat/test-get_release-GitHub.R index a576cfd6..19b8f49a 100644 --- a/tests/testthat/test-get_release-GitHub.R +++ b/tests/testthat/test-get_release-GitHub.R @@ -102,3 +102,30 @@ test_that("`get_release_logs()` pulls release logs in the table format", { expect_lt(max(releases_table$published_at), as.POSIXct("2023-09-30")) test_mocker$cache(releases_table) }) + +test_that("`get_release_logs()` is set to scan whole git host", { + github_testhost_all <- create_github_testhost_all(orgs = "test_org") + mockery::stub( + github_testhost_all$get_release_logs, + "graphql_engine$get_orgs", + "test_org" + ) + mockery::stub( + github_testhost_all$get_release_logs, + "private$get_release_logs_from_repos", + test_mocker$use("releases_from_repos") + ) + mockery::stub( + github_testhost_all$get_release_logs, + "private$get_release_logs_from_orgs", + test_mocker$use("releases_from_orgs") + ) + expect_snapshot( + gh_releases_table <- github_testhost_all$get_release_logs( + since = "2023-01-01", + until = "2023-02-28", + verbose = TRUE, + progress = FALSE + ) + ) +})