Skip to content

Commit

Permalink
Merge pull request #483 from r-world-devs/481-mock-api-responses
Browse files Browse the repository at this point in the history
481 mock api responses
  • Loading branch information
maciekbanas authored Sep 30, 2024
2 parents 53af30c + 56b67f8 commit e92dbce
Show file tree
Hide file tree
Showing 17 changed files with 353 additions and 95 deletions.
3 changes: 2 additions & 1 deletion R/GitHostGitHub.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @noRd
GitHostGitHub <- R6::R6Class("GitHostGitHub",
GitHostGitHub <- R6::R6Class(
classname = "GitHostGitHub",
inherit = GitHost,
public = list(
initialize = function(orgs = NA,
Expand Down
8 changes: 0 additions & 8 deletions tests/testthat/_snaps/get_files_content-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@
Output
[1] "query GetFileBlobFromRepo($org: String!, $repo: String!, $expression: String!) {\n repository(owner: $org, name: $repo) {\n repo_id: id\n repo_name: name\n repo_url: url\n file: object(expression: $expression) {\n ... on Blob {\n text\n byteSize\n }\n }\n }\n }"

# `get_files_content()` pulls files only for the repositories specified

Code
gh_files_table <- github_testhost$get_files_content(file_path = "renv.lock")
Message
i [Host:GitHub][Engine:GraphQl][Scope:r-world-devs] Pulling files content: [renv.lock]...
i [Host:GitHub][Engine:GraphQl][Scope:openpharma] Pulling files content: [renv.lock]...

8 changes: 0 additions & 8 deletions tests/testthat/_snaps/get_files_content-GitLab.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,3 @@
Message
i I will switch to pulling files per repository.

# `get_files_content()` pulls two files in the table format

Code
gl_files_table <- gitlab_testhost$get_files_content(file_path = c(
"meta_data.yaml", "README.md"))
Message
i [Host:GitLab][Engine:GraphQl][Scope:mbtests] Pulling files content: [meta_data.yaml, README.md]...

7 changes: 3 additions & 4 deletions tests/testthat/_snaps/get_files_structure-GitHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

Code
gh_files_structure_from_orgs <- github_testhost_priv$
get_files_structure_from_orgs(pattern = "\\.md|\\.qmd|\\.png", depth = 2L,
verbose = TRUE)
get_files_structure_from_orgs(pattern = NULL, depth = 2L, verbose = TRUE)
Message
i [Host:GitHub][Engine:GraphQl][Scope:r-world-devs] Pulling files structure...[files matching pattern: '\.md|\.qmd|\.png']...
i [Host:GitHub][Engine:GraphQl][Scope:openpharma] Pulling files structure...[files matching pattern: '\.md|\.qmd|\.png']...
i [Host:GitHub][Engine:GraphQl][Scope:r-world-devs] Pulling files structure......
i [Host:GitHub][Engine:GraphQl][Scope:openpharma] Pulling files structure......

# when files_structure is empty, appropriate message is returned

Expand Down
File renamed without changes.
195 changes: 195 additions & 0 deletions tests/testthat/helper-fixtures.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,110 @@ test_fixtures$github_file_response <- list(
)
)

test_fixtures$gitlab_file_org_response <- list(
"data" = list(
"group" = list(
"projects" = list(
"count" = 3,
"pageInfo" = list(
"hasNextPage" = FALSE,
"endCursor" = "xxxxxx"
),
"edges" = list(
list(
"node" = list(
"name" = "graphql_tests",
"id" = "gid://gitlab/Project/61399846",
"webUrl" = "https://gitlab.com/mbtests/graphql_tests",
"repository" = list(
"blobs" = list(
"nodes" = list() # empty response for query, no files found in org
)
)
)
),
list(
"node" = list(
"name" = "RM Tests 3",
"id" = "gid://gitlab/Project/44346961",
"webUrl" = "https://gitlab.com/mbtests/rm-tests-3",
"repository" = list(
"blobs" = list(
"nodes" = list(
list(
"name" = "meta_data.yaml",
"rawBlob" = "Some interesting text",
"size" = 4
)
)
)
)
)
),
list(
"node" = list(
"name" = "RM Tests 2",
"id" = "gid://gitlab/Project/44293594",
"webUrl" = "https://gitlab.com/mbtests/rm-tests-2",
"repository" = list(
"blobs" = list(
"nodes" = list(
list(
"name" = "meta_data.yaml",
"rawBlob" = "Some interesting text",
"size" = 5
)
)
)
)
)
)
)
)
)
)
)

test_fixtures$gitlab_file_repo_response <- list(
"data" = list(
"project" = list(
"name" = "TestProject",
"id" = "1010101",
"webUrl" = "https://gitlab.com/mbtests/graphql_tests",
"repository" = list(
"blobs" = list(
"nodes" = list(
list(
"name" = "README.md",
"rawBlob" = "# graphql_tests\n\nThis project is for testing GraphQL capabilities.\n",
"size" = "67"
),
list(
"name" = "project_metadata.yaml",
"rawBlob" = "Name: GraphQL Tests",
"size" = "19"
)
)
)
)
)
)
)

test_fixtures$github_png_file_response <- list(
"data" = list(
"repository" = list(
"repo_id" = "01010101",
"repo_name" = "TestProject",
"repo_url" = "https://github.com/r-world-devs/GitStats",
"file" = list(
"text" = NULL,
"byteSize" = 50L
)
)
)
)

test_fixtures$gitlab_search_response <- list(
list(
"basename" = "test",
Expand All @@ -66,3 +170,94 @@ test_fixtures$gitlab_search_response <- list(
"project_id" = 43400864
)
)

test_fixtures$github_search_response <- list(
"total_count" = 3,
"incomplete_results" = FALSE,
"items" = list(
list(
"name" = "test1.R",
"path" = "examples/test1.R",
"sha" = "0d42b9d23ddfc0bca1",
"url" = "test_url",
"git_url" = "test_git_url",
"html_url" = "test_html_url",
"repository" = list(
"id" = 627452680,
"url" = "https://api.github.com/repos/r-world-devs/GitStats",
"html" = "https://github.com/r-world-devs/GitStats"
),
"score" = 1
),
list(
"name" = "test2.R",
"path" = "tests/test2.R",
"sha" = "01238xb",
"url" = "test_url",
"git_url" = "test_git_url",
"html_url" = "test_html_url",
"repository" = list(
"id" = 604718884,
"url" = "https://api.github.com/repos/r-world-devs/GitStats",
"html" = "https://github.com/r-world-devs/GitStats"
),
"score" = 1
),
list(
"name" = "test3.R",
"path" = "R/test3.R",
"sha" = "20e19af2dda26d04f6",
"url" = "test_url",
"git_url" = "test_git_url",
"html_url" = "test_html_url",
"repository" = list(
"id" = 495151911,
"url" = "https://api.github.com/repos/r-world-devs/GitStats",
"html" = "https://github.com/r-world-devs/GitStats"
),
"score" = 1
)
)
)

test_fixtures$github_files_tree_response <- list(
"data" = list(
"repository" = list(
"id" = "R_kgD0Ivtxsg",
"name" = "GitStats",
"url" = "https://github.com/r-world-devs/GitStats",
"object" = list(
"entries" = list(
list(
"name" = ".Rbuildignore",
"type" = "blob"
),
list(
"name" = ".Renviron",
"type" = "blob"
),
list(
"name" = ".Rprofile",
"type" = "blob"
),
list(
"name" = ".covrignore",
"type" = "blob"
),
list(
"name" = "renv",
"type" = "tree"
),
list(
"name" = "tests",
"type" = "tree"
),
list(
"name" = "vignettes",
"type" = "tree"
)
)
)
)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,29 @@ test_that("`perform_request()` returns proper status", {
"HTTP 404 Not Found"
)
})

test_that("`perform_request()` returns status 200", {
response <- test_rest_github_priv$perform_request(
endpoint = "https://api.github.com/repos/r-world-devs/GitStats",
token = Sys.getenv("GITHUB_PAT")
)
expect_equal(
response$status_code,
200
)
})

test_that("`perform_request()` for GraphQL returns status 200", {
response <- test_graphql_github_priv$perform_request(
gql_query = "{
viewer {
login
}
}",
vars = NULL
)
expect_equal(
response$status_code,
200
)
})
42 changes: 28 additions & 14 deletions tests/testthat/test-get_files_content-GitHub.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ test_that("get_repos_data pulls data on repos and branches", {
})

test_that("GitHub GraphQL Engine pulls file response", {
mockery::stub(
test_graphql_github_priv$get_file_response,
"self$gql_response",
test_fixtures$github_file_response
)
github_file_response <- test_graphql_github_priv$get_file_response(
org = "r-world-devs",
repo = "GitStats",
Expand All @@ -36,6 +41,23 @@ test_that("GitHub GraphQL Engine pulls file response", {
test_mocker$cache(github_file_response)
})

test_that("GitHub GraphQL Engine pulls png file response", {
mockery::stub(
test_graphql_github_priv$get_file_response,
"self$gql_response",
test_fixtures$github_png_file_response
)
github_png_file_response <- test_graphql_github_priv$get_file_response(
org = "r-world-devs",
repo = "GitStats",
def_branch = "master",
file_path = "man/figures/logo.png",
files_query = test_mocker$use("gh_file_blobs_from_repo_query")
)
expect_github_files_response(github_png_file_response)
test_mocker$cache(github_png_file_response)
})

test_that("GitHub GraphQL Engine pulls files from organization", {
mockery::stub(
test_graphql_github$get_files_from_org,
Expand All @@ -56,6 +78,11 @@ test_that("GitHub GraphQL Engine pulls files from organization", {
})

test_that("GitHub GraphQL Engine pulls .png files from organization", {
mockery::stub(
test_graphql_github$get_files_from_org,
"private$get_file_response",
test_mocker$use("github_png_file_response")
)
github_png_files_response <- test_graphql_github$get_files_from_org(
org = "r-world-devs",
repos = NULL,
Expand Down Expand Up @@ -158,21 +185,8 @@ test_that("`get_files_content()` pulls files in the table format", {
test_mocker$use("gh_files_table")
)
gh_files_table <- github_testhost$get_files_content(
file_path = "LICENSE"
file_path = "DESCRIPTION"
)
expect_files_table(gh_files_table, with_cols = "api_url")
test_mocker$cache(gh_files_table)
})

test_that("`get_files_content()` pulls files only for the repositories specified", {
github_testhost <- create_github_testhost(
repos = c("r-world-devs/GitStats", "openpharma/visR", "openpharma/DataFakeR"),
)
expect_snapshot(
gh_files_table <- github_testhost$get_files_content(
file_path = "renv.lock"
)
)
expect_files_table(gh_files_table, with_cols = "api_url")
expect_equal(nrow(gh_files_table), 2) # visR does not have renv.lock
})
Loading

0 comments on commit e92dbce

Please sign in to comment.