Skip to content

Commit

Permalink
Fix use of max_page and connection tests
Browse files Browse the repository at this point in the history
- fix issue #14
  • Loading branch information
statnmap committed May 28, 2021
1 parent b117762 commit a106324
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 127 deletions.
3 changes: 3 additions & 0 deletions R/gitlab_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ gitlab <- function(req,
if (!missing(page)) {
dot_args <- c(dot_args, page = page)
}
if (!missing(max_page)) {
dot_args <- c(dot_args, max_page = max_page)
}
do.call(gitlab_con, c(dot_args, gitlab_con = "self", ...)) %>%
iff(debug, print)
}
Expand Down
1 change: 1 addition & 0 deletions dev/renaming_TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
- [x] 'project' first
- [x] comments // test_comments.R
- [x] 'project' first
- [ ] // test_connection.R

- [ ] NEWS: project first everywhere meaningful
238 changes: 112 additions & 126 deletions tests/testthat/test_connection.R
Original file line number Diff line number Diff line change
@@ -1,77 +1,71 @@
# Unset as it is set in helper.R
unset_gitlab_connection()

my_gitlab_test <- gl_connection(test_url,
private_token = test_private_token,
api_version = test_api_version)

# Only test if connection works ----

# gl_connection ----
# Set GitLab connection for all tests
my_gitlab_test <- gl_connection(
gitlab_url = test_url,
private_token = test_private_token,
api_version = test_api_version)

# Note that we cannot compare directly all outputs because GitLab projects are actively increasing
# Way 1
my_gitlab_projects_output_raw <- my_gitlab_test("projects", max_page = 1)
# Because download may be different as gitlab.com gets new projects modifications every second
# We need to filter out a smaller set of projects to compare together
first_ids <- my_gitlab_projects_output_raw$id[1:10]
first_dates <- my_gitlab_projects_output_raw$created_at[1:10]
# filter
my_gitlab_projects_output <- my_gitlab_projects_output_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# Way_2
my_gitlab_list_projects_output_raw <- my_gitlab(gl_list_projects, max_page = 1)
# filter
my_gitlab_list_projects_output <- my_gitlab_list_projects_output_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
my_gitlab_list_projects_output_raw <- my_gitlab_test(gl_list_projects, max_page = 1)

# Way_3
gitlab_projects_api_raw <- gitlab("projects",
api_root = paste0(test_url, "/api/v", test_api_version, "/"),
private_token = test_private_token,
max_page = 1)
# filter
gitlab_projects_api <- gitlab_projects_api_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# Way_4
gl_list_projects_output_raw <- gl_list_projects(gitlab_con = my_gitlab, max_page = 1)
# filter
gl_list_projects_output <- gl_list_projects_output_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
gl_list_projects_output_raw <- gl_list_projects(gitlab_con = my_gitlab_test, max_page = 1)

# names with dots [.] only exist if there are sub-lists.
# This is not always the case depending on projects.
# Names without dots are mandatory fields, apparently
names_1 <- names(my_gitlab_projects_output_raw)[!grepl("[.]", names(my_gitlab_projects_output_raw))]
names_2 <- names(my_gitlab_list_projects_output_raw)[!grepl("[.]", names(my_gitlab_list_projects_output_raw))]
names_3 <- names(gitlab_projects_api_raw)[!grepl("[.]", names(gitlab_projects_api_raw))]
names_4 <- names(gl_list_projects_output_raw)[!grepl("[.]", names(gl_list_projects_output_raw))]


test_that("Gitlab connection creation works", {

expect_is(my_gitlab, "function")
expect_is(my_gitlab_test, "function")

expect_is(my_gitlab_projects_output, "data.frame")
expect_is(my_gitlab_list_projects_output, "data.frame")
expect_is(my_gitlab_projects_output_raw, "data.frame")
expect_is(my_gitlab_list_projects_output_raw, "data.frame")
expect_is(gitlab_projects_api_raw, "data.frame")
expect_is(gl_list_projects_output_raw, "data.frame")

# one page is 20
# one page is 20 lines
expect_equal(nrow(my_gitlab_projects_output_raw), 20)
expect_equal(nrow(my_gitlab_list_projects_output_raw), 20)
expect_equal(nrow(gitlab_projects_api_raw), 20)
# issue #14
# Not working - See why max_page is not used in gl_list_projects_output?
# expect_equal(nrow(gl_list_projects_output_raw), 20)
# expect_equivalent(my_gitlab_list_projects_output,
# my_gitlab_projects_output)

expect_equivalent(my_gitlab_projects_output,
gitlab_projects_api)

## function idiom
expect_is(gl_list_projects_output, "data.frame")
expect_equal(nrow(gl_list_projects_output_raw), 20)

# issue #14
# Not working - See why max_page is not used in gl_list_projects_output?
# expect_equivalent(gl_list_projects_output,
# my_gitlab_projects_output)
#
# Mandatory col names are all the same
expect_length(names_1[!names_1 %in% names_2], 0)
expect_length(names_1[!names_1 %in% names_3], 0)
expect_length(names_1[!names_1 %in% names_4], 0)
expect_length(names_2[!names_2 %in% names_1], 0)
expect_length(names_2[!names_2 %in% names_3], 0)
expect_length(names_3[!names_3 %in% names_4], 0)

})

my_project <- gl_project_connection(test_url, test_project,
private_token = test_private_token,
api_version = test_api_version)
# gl_project_connection ----
# Set project connection for all tests ----
my_project <- gl_project_connection(
gitlab_url = test_url,
project = test_project,
private_token = test_private_token,
api_version = test_api_version)


my_project_list_files <- my_project(gl_list_files, max_page = 1)
my_gl_list_files <- gl_list_files(gitlab_con = my_project, max_page = 1)
Expand All @@ -88,60 +82,53 @@ test_that("Project connection creation works", {
})

# set_gitlab_connection ----
# ## using explicit function creation
# my_gitlab <- gl_connection(test_url,
# private_token = test_private_token,
# api_version = test_api_version)
set_gitlab_connection(my_gitlab)
# Way_1
set_gitlab_connection(my_gitlab_test)
# Note that we cannot compare directly all outputs because GitLab projects are actively increasing
# Way_0 - gitlab_connection already set
gitlab_projects_raw <- gitlab("projects", max_page = 1)
# Because download may be different as gitlab.com gets new projects modifications every second
# We need to filter out a smaller set of projects to compare together
first_ids <- gitlab_projects_raw$id[1:10]
first_dates <- gitlab_projects_raw$created_at[1:10]
# filter
gitlab_projects <- gitlab_projects_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# Way_2
gitlab_projects_self_raw <- my_gitlab("projects", gitlab_con = "self", max_page = 1)
# filter
gitlab_projects_self <- gitlab_projects_self_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
# Way 1 - gitlab_connection already set
my_gitlab_projects_self_raw <- my_gitlab_test("projects", gitlab_con = "self", max_page = 1)

# Way_3
gitlab_list_projects_self_raw <- my_gitlab(gl_list_projects, gitlab_con = "self", max_page = 1)
# filter
gitlab_list_projects_self <- gitlab_list_projects_self_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
# Way_2 - gitlab_connection already set
my_gitlab_list_projects_self_raw <- my_gitlab_test(gl_list_projects, gitlab_con = "self", max_page = 1)

# Way_4
# Way_4 - gitlab_connection already set
gl_list_projects_empty_raw <- gl_list_projects(max_page = 1)
# filter
gl_list_projects_empty <- gl_list_projects_empty_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# names with dots [.] only exist if there are sub-lists.
# This is not always the case depending on projects.
# Names without dots are mandatory fields, apparently
names_0 <- names(gitlab_projects_raw)[!grepl("[.]", names(gitlab_projects_raw))]
names_1 <- names(my_gitlab_projects_self_raw)[!grepl("[.]", names(my_gitlab_projects_self_raw))]
names_2 <- names(my_gitlab_list_projects_self_raw)[!grepl("[.]", names(my_gitlab_list_projects_self_raw))]
names_4 <- names(gl_list_projects_empty_raw)[!grepl("[.]", names(gl_list_projects_empty_raw))]


test_that("set_gl_connection works", {

expect_is(gitlab, "function")

# one page is 20
# Not working - See why max_page is not used ?
# expect_equal(nrow(gitlab_projects_raw), 20)
expect_equal(nrow(gitlab_projects_self_raw), 20)
expect_equal(nrow(gitlab_list_projects_self_raw), 20)
# issue #14
# Not working - See why max_page is not used ?
# expect_equal(nrow(gl_list_projects_empty_raw), 20)
expect_is(gitlab_projects_raw, "data.frame")
expect_is(my_gitlab_projects_self_raw, "data.frame")
expect_is(my_gitlab_list_projects_self_raw, "data.frame")
expect_is(gl_list_projects_empty_raw, "data.frame")

# issue #14
# Find why gitlab_projects_self and gitlab_list_projects_self have not the same number of columns than others
# expect_is(gitlab_projects, "data.frame")
# expect_equivalent(gitlab_projects,
# gitlab_projects_self)
# expect_equivalent(gl_list_projects_empty,
# gitlab_list_projects_self)
# one page is 20 lines
expect_equal(nrow(gitlab_projects_raw), 20)
expect_equal(nrow(my_gitlab_projects_self_raw), 20)
expect_equal(nrow(my_gitlab_list_projects_self_raw), 20)
expect_equal(nrow(gl_list_projects_empty_raw), 20)

# Mandatory col names are all the same
expect_length(names_1[!names_1 %in% names_2], 0)
expect_length(names_1[!names_1 %in% names_3], 0)
expect_length(names_1[!names_1 %in% names_0], 0)
expect_length(names_2[!names_2 %in% names_1], 0)
expect_length(names_2[!names_2 %in% names_3], 0)
expect_length(names_3[!names_3 %in% names_0], 0)
})
# unset connection
unset_gitlab_connection()

# set_gitlab_connection with dots ----
Expand All @@ -150,54 +137,53 @@ set_gitlab_connection(gitlab_url = test_url,
private_token = test_private_token,
api_version = test_api_version)

# Way_1
# Note that we cannot compare directly all outputs because GitLab projects are actively increasing
# Way_0 - gitlab_connection already set
gitlab_projects_raw <- gitlab("projects", max_page = 1)
# Because download may be different as gitlab.com gets new projects modifications every second
# We need to filter out a smaller set of projects to compare together
first_ids <- gitlab_projects_raw$id[1:10]
first_dates <- gitlab_projects_raw$created_at[1:10]
# filter
gitlab_projects <- gitlab_projects_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# Way_2
gitlab_projects_self_raw <- my_gitlab("projects", gitlab_con = "self", max_page = 1)
# filter
gitlab_projects_self <- gitlab_projects_self_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
# Way 1 - gitlab_connection already set
my_gitlab_projects_self_raw <- my_gitlab_test("projects", gitlab_con = "self", max_page = 1)

# Way_3
gitlab_list_projects_self_raw <- my_gitlab(gl_list_projects, gitlab_con = "self", max_page = 1)
# filter
gitlab_list_projects_self <- gitlab_list_projects_self_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)
# Way_2 - gitlab_connection already set
my_gitlab_list_projects_self_raw <- my_gitlab_test(gl_list_projects, gitlab_con = "self", max_page = 1)

# Way_4
# Way_4 - gitlab_connection already set
gl_list_projects_empty_raw <- gl_list_projects(max_page = 1)
# filter
gl_list_projects_empty <- gl_list_projects_empty_raw %>%
filter(id %in% first_ids, created_at %in% first_dates)

# names with dots [.] only exist if there are sub-lists.
# This is not always the case depending on projects.
# Names without dots are mandatory fields, apparently
names_0 <- names(gitlab_projects_raw)[!grepl("[.]", names(gitlab_projects_raw))]
names_1 <- names(my_gitlab_projects_self_raw)[!grepl("[.]", names(my_gitlab_projects_self_raw))]
names_2 <- names(my_gitlab_list_projects_self_raw)[!grepl("[.]", names(my_gitlab_list_projects_self_raw))]
names_4 <- names(gl_list_projects_empty_raw)[!grepl("[.]", names(gl_list_projects_empty_raw))]


test_that("set_gl_connection with dots works", {

# one page is 20
# Not working - See why max_page is not used ?
# expect_equal(nrow(gitlab_projects_raw), 20)
expect_equal(nrow(gitlab_projects_self_raw), 20)
expect_equal(nrow(gitlab_list_projects_self_raw), 20)
# issue #14
# Not working - See why max_page is not used ?
# expect_equal(nrow(gl_list_projects_empty_raw), 20)
expect_is(gitlab, "function")

expect_is(gitlab_projects_raw, "data.frame")
expect_is(my_gitlab_projects_self_raw, "data.frame")
expect_is(my_gitlab_list_projects_self_raw, "data.frame")
expect_is(gl_list_projects_empty_raw, "data.frame")

expect_is(gitlab_projects, "data.frame")
# one page is 20 lines
expect_equal(nrow(gitlab_projects_raw), 20)
expect_equal(nrow(my_gitlab_projects_self_raw), 20)
expect_equal(nrow(my_gitlab_list_projects_self_raw), 20)
expect_equal(nrow(gl_list_projects_empty_raw), 20)

# issue #14
# Find why gitlab_projects_self and gitlab_list_projects_self have not the same number of columns than others
# expect_equivalent(gitlab_projects,
# gitlab_projects_self)
# expect_equivalent(gl_list_projects_empty,
# gitlab_list_projects_self)
# Mandatory col names are all the same
expect_length(names_1[!names_1 %in% names_2], 0)
expect_length(names_1[!names_1 %in% names_3], 0)
expect_length(names_1[!names_1 %in% names_0], 0)
expect_length(names_2[!names_2 %in% names_1], 0)
expect_length(names_2[!names_2 %in% names_3], 0)
expect_length(names_3[!names_3 %in% names_0], 0)

})
unset_gitlab_connection()

# Set back the connection for the session as in helper.R
set_gitlab_connection(my_gitlab)
9 changes: 8 additions & 1 deletion tests/testthat/test_pagination.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
test_that("Pagination produces the same results", {

## per_page argument
my_gitlab <- gl_connection(test_url,
my_gitlab <- gl_connection(gitlab_url = test_url,
private_token = test_private_token,
api_version = test_api_version)

users_2 <- my_gitlab("users", per_page = 1, max_page = 4)
users_4 <- my_gitlab("users", per_page = 4, max_page = 1)
expect_equivalent(users_2, users_4)



## get single pages
users_2_1 <- my_gitlab("users", per_page = 2, page = 1)
users_2_2 <- my_gitlab("users", per_page = 2, page = 2)
Expand All @@ -18,4 +21,8 @@ test_that("Pagination produces the same results", {
# users_2_1 <- my_gitlab("users", per_page = 2, page = 1000)
# expect_true(nrow(users_2_1) == 0L)

projects_2 <- my_gitlab("projects", per_page = 1, max_page = 4)
projects_4 <- my_gitlab("projects", per_page = 4, max_page = 1)
expect_equivalent(projects_2, projects_4)

})

0 comments on commit a106324

Please sign in to comment.