Skip to content

Commit

Permalink
Add tests, adjust code.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekbanas committed Dec 19, 2024
1 parent 59a34d2 commit b621cab
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 16 deletions.
9 changes: 6 additions & 3 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions R/test_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/01-get_repos-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

6 changes: 0 additions & 6 deletions tests/testthat/_snaps/01-get_repos-GitLab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/02-get_commits-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/get_release-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

48 changes: 48 additions & 0 deletions tests/testthat/test-01-get_repos-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 32 additions & 7 deletions tests/testthat/test-02-get_commits-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
})
27 changes: 27 additions & 0 deletions tests/testthat/test-get_release-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
})

0 comments on commit b621cab

Please sign in to comment.