Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekbanas committed Dec 20, 2024
1 parent b85ed50 commit 4f357b5
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 17 deletions.
7 changes: 4 additions & 3 deletions R/GitHost.R
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,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_urls_from_orgs <- private$get_repos_urls_from_orgs(
type = type,
Expand Down Expand Up @@ -823,14 +824,14 @@ GitHost <- R6::R6Class(
verbose = verbose,
progress = progress
)
}) %>%
}) |>
purrr::list_flatten()
}
if (output != "raw") {
repos_table <- repos_response %>%
rest_engine$tailor_repos_response(
output = output
) %>%
) |>
rest_engine$prepare_repos_table(
output = output,
verbose = verbose
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/_snaps/01-get_repos-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,53 @@
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_repos_with_code_from_orgs()` pulls raw response

Code
repos_with_code_from_orgs_raw <- github_testhost_priv$
get_repos_with_code_from_orgs(code = "shiny", in_files = c("DESCRIPTION",
"NAMESPACE"), output = "raw", verbose = TRUE)
Message
i [Host:GitHub][Engine:REST][Scope:test_org] Pulling repositories...

# `get_repos_with_code_from_host()` pulls and parses output into table

Code
repos_with_code_from_host_table <- github_testhost_priv$
get_repos_with_code_from_host(code = "DESCRIPTION", in_path = TRUE, output = "table_full",
verbose = TRUE)
Message
i [Host:GitHub][Engine:REST] Pulling repositories...

# `get_repos_with_code_from_repos()` works

Code
repos_with_code_from_repos_full <- github_testhost_priv$
get_repos_with_code_from_repos(code = "tests", output = "table_full",
verbose = TRUE)
Message
i [Host:GitHub][Engine:REST][Scope:] Pulling repositories...
i Preparing repositories table...

# `get_repos_with_code_from_orgs()` pulls minimum version of table

Code
repos_with_code_from_repos_min <- github_testhost_priv$
get_repos_with_code_from_repos(code = "tests", in_files = "DESCRIPTION",
output = "table_min", verbose = TRUE)
Message
i [Host:GitHub][Engine:REST][Scope:] Pulling repositories...
i Preparing repositories table...

# `get_repos_with_code_from_host()` pulls raw response

Code
repos_with_code_from_host_raw <- github_testhost_priv$
get_repos_with_code_from_host(code = "shiny", in_files = c("DESCRIPTION",
"NAMESPACE"), output = "raw", verbose = TRUE)
Message
i [Host:GitHub][Engine:REST] Pulling repositories...

# `get_all_repos()` is set to scan whole git host

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/get_urls_repos-GitHub.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# get_repos_urls_from_orgs prepares web repo_urls vector

Code
gh_repos_urls_from_orgs <- github_testhost_priv$get_repos_urls_from_orgs(type = "web",
verbose = TRUE, progress = FALSE)
Message
i [Host:GitHub][Engine:REST][Scope:test_org] Pulling repositories (URLs)...

105 changes: 95 additions & 10 deletions tests/testthat/test-01-get_repos-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -371,27 +371,112 @@ test_that("`get_repos_with_code_from_orgs()` pulls raw response", {
"rest_engine$get_repos_by_code",
test_mocker$use("gh_repos_by_code_raw")
)
repos_with_code_from_orgs_raw <- github_testhost_priv$get_repos_with_code_from_orgs(
code = "shiny",
in_files = c("DESCRIPTION", "NAMESPACE"),
output = "raw",
verbose = FALSE
expect_snapshot(
repos_with_code_from_orgs_raw <- github_testhost_priv$get_repos_with_code_from_orgs(
code = "shiny",
in_files = c("DESCRIPTION", "NAMESPACE"),
output = "raw",
verbose = TRUE
)
)
expect_type(repos_with_code_from_orgs_raw, "list")
expect_gt(length(repos_with_code_from_orgs_raw), 0)
})

test_that("`get_repos_with_code_from_host()` pulls and parses output into table", {
mockery::stub(
github_testhost_priv$get_repos_with_code_from_host,
"rest_engine$get_repos_by_code",
test_mocker$use("gh_repos_by_code_raw")
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_host,
"rest_engine$prepare_repos_table",
test_mocker$use("gh_repos_by_code_table")
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_host,
"rest_engine$get_repos_issues",
test_mocker$use("gh_repos_by_code_table")
)
expect_snapshot(
repos_with_code_from_host_table <- github_testhost_priv$get_repos_with_code_from_host(
code = "DESCRIPTION",
in_path = TRUE,
output = "table_full",
verbose = TRUE
)
)
expect_repos_table(repos_with_code_from_host_table)
})

test_that("`get_repos_with_code_from_repos()` works", {
github_testhost_priv <- create_github_testhost(
repos = c("TestRepo1", "TestRepo2"),
mode = "private"
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_repos,
"rest_engine$get_repos_by_code",
test_mocker$use("gh_repos_by_code")
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_repos,
"rest_engine$get_repos_issues",
test_mocker$use("gh_repos_by_code_table")
)
github_testhost_priv$searching_scope <- c("repo")
expect_snapshot(
repos_with_code_from_repos_full <- github_testhost_priv$get_repos_with_code_from_repos(
code = "tests",
output = "table_full",
verbose = TRUE
)
)
expect_repos_table(repos_with_code_from_repos_full)
})

test_that("`get_repos_with_code_from_repos()` pulls minimum version of table", {
github_testhost_priv <- create_github_testhost(
repos = c("TestRepo1", "TestRepo2"),
mode = "private"
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_repos,
"rest_engine$get_repos_by_code",
test_mocker$use("gh_repos_by_code")
)
mockery::stub(
github_testhost_priv$get_repos_with_code_from_repos,
"rest_engine$get_repos_issues",
test_mocker$use("gh_repos_by_code_table")
)
github_testhost_priv$searching_scope <- c("org", "repo")
expect_snapshot(
repos_with_code_from_repos_min <- github_testhost_priv$get_repos_with_code_from_repos(
code = "tests",
in_files = "DESCRIPTION",
output = "table_min",
verbose = TRUE
)
)
expect_repos_table(repos_with_code_from_repos_min,
repo_cols = repo_min_colnames)
})

test_that("`get_repos_with_code_from_host()` pulls raw response", {
mockery::stub(
github_testhost_priv$get_repos_with_code_from_host,
"rest_engine$get_repos_by_code",
test_mocker$use("gh_repos_by_code_raw")
)
repos_with_code_from_host_raw <- github_testhost_priv$get_repos_with_code_from_host(
code = "shiny",
in_files = c("DESCRIPTION", "NAMESPACE"),
output = "raw",
verbose = FALSE
expect_snapshot(
repos_with_code_from_host_raw <- github_testhost_priv$get_repos_with_code_from_host(
code = "shiny",
in_files = c("DESCRIPTION", "NAMESPACE"),
output = "raw",
verbose = TRUE
)
)
expect_type(repos_with_code_from_host_raw, "list")
expect_gt(length(repos_with_code_from_host_raw), 0)
Expand Down
39 changes: 35 additions & 4 deletions tests/testthat/test-get_urls_repos-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ test_that("get_repos_urls_from_orgs prepares web repo_urls vector", {
)
github_testhost_priv$searching_scope <- "org"
github_testhost_priv$orgs <- "test_org"
gh_repos_urls_from_orgs <- github_testhost_priv$get_repos_urls_from_orgs(
type = "web",
verbose = FALSE,
progress = FALSE
expect_snapshot(
gh_repos_urls_from_orgs <- github_testhost_priv$get_repos_urls_from_orgs(
type = "web",
verbose = TRUE,
progress = FALSE
)
)
expect_gt(length(gh_repos_urls_from_orgs), 0)
expect_true(any(grepl("test-org", gh_repos_urls_from_orgs)))
Expand Down Expand Up @@ -118,6 +120,35 @@ test_that("get_all_repos_urls prepares web repo_urls vector", {
test_mocker$cache(gh_repos_urls)
})

test_that("get_all_repos_urls is set to scan whole host", {
github_testhost_all_priv <- create_github_testhost_all(
orgs = "test_org",
mode = "private"
)
mockery::stub(
github_testhost_all_priv$get_all_repos_urls,
"graphql_engine$get_orgs",
"test_org"
)
mockery::stub(
github_testhost_all_priv$get_all_repos_urls,
"private$get_repos_urls_from_orgs",
test_mocker$use("gh_repos_urls_from_orgs")
)
mockery::stub(
github_testhost_all_priv$get_all_repos_urls,
"private$get_repos_urls_from_repos",
test_mocker$use("gh_repos_urls")
)
gh_repos_urls <- github_testhost_all_priv$get_all_repos_urls(
type = "web",
verbose = FALSE
)
expect_gt(length(gh_repos_urls), 0)
expect_true(any(grepl("test-org", gh_repos_urls)))
expect_true(all(grepl("https://testhost.com/", gh_repos_urls)))
})

test_that("get_repo_url_from_response retrieves repositories URLS", {
gh_repo_api_urls <- github_testhost_priv$get_repo_url_from_response(
search_response = test_mocker$use("gh_search_repos_response"),
Expand Down

0 comments on commit 4f357b5

Please sign in to comment.