Skip to content

Commit 949a1a4

Browse files
bschilderclaude
andcommitted
Add unit tests for 15 uncovered functions, coverage 29% -> 50%
New test files for utility functions (split_batches, author_fields, check_pkgs, report_time, stopper, is_url, add_owner_repo, r_repos_upset_queries, r_repos_data_cast), DESCRIPTION helpers (description_authors, description_extract_i), and GitHub API functions (github_code, github_repositories, github_user_events, github_files_httr). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e6cdf21 commit 949a1a4

3 files changed

Lines changed: 704 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
## ---- description_authors ----
2+
3+
test_that("description_authors extracts names from local DESCRIPTION", {
4+
testthat::skip_if_not_installed("desc")
5+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
6+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
7+
d <- desc::desc(desc_path)
8+
res <- echogithub:::description_authors(desc_file = d,
9+
names_only = TRUE,
10+
verbose = FALSE)
11+
testthat::expect_true(is.character(res))
12+
testthat::expect_true(nchar(res) > 0)
13+
## Should contain the known first author
14+
testthat::expect_true(grepl("Brian", res))
15+
testthat::expect_true(grepl("Schilder", res))
16+
})
17+
18+
test_that("description_authors returns full author info when names_only=FALSE", {
19+
testthat::skip_if_not_installed("desc")
20+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
21+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
22+
d <- desc::desc(desc_path)
23+
res <- echogithub:::description_authors(desc_file = d,
24+
names_only = FALSE,
25+
verbose = FALSE)
26+
testthat::expect_true(is.character(res))
27+
## Should have email or role info when names_only=FALSE
28+
testthat::expect_true(nchar(res) > 0)
29+
})
30+
31+
test_that("description_authors with add_html=TRUE wraps in HTML tags", {
32+
testthat::skip_if_not_installed("desc")
33+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
34+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
35+
d <- desc::desc(desc_path)
36+
res <- echogithub:::description_authors(desc_file = d,
37+
names_only = TRUE,
38+
add_html = TRUE,
39+
verbose = FALSE)
40+
testthat::expect_true(grepl("<h4>", res))
41+
testthat::expect_true(grepl("<i>", res))
42+
testthat::expect_true(grepl("Authors:", res))
43+
})
44+
45+
test_that("description_authors returns collapsed string for multiple authors", {
46+
testthat::skip_if_not_installed("desc")
47+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
48+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
49+
d <- desc::desc(desc_path)
50+
res <- echogithub:::description_authors(desc_file = d,
51+
names_only = TRUE,
52+
verbose = FALSE)
53+
## echogithub has multiple authors, so there should be a comma separator
54+
testthat::expect_true(grepl(",", res))
55+
})
56+
57+
## ---- description_extract_i ----
58+
59+
test_that("description_extract_i extracts default fields", {
60+
testthat::skip_if_not_installed("desc")
61+
testthat::skip_if_not_installed("rworkflows")
62+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
63+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
64+
d <- desc::desc(desc_path)
65+
res <- echogithub:::description_extract_i(
66+
desc_file = d,
67+
fields = c("owner", "repo", "authors"),
68+
verbose = FALSE
69+
)
70+
testthat::expect_true(is.list(res))
71+
testthat::expect_true("owner" %in% names(res))
72+
testthat::expect_true("repo" %in% names(res))
73+
testthat::expect_true("authors" %in% names(res))
74+
})
75+
76+
test_that("description_extract_i extracts owner correctly", {
77+
testthat::skip_if_not_installed("desc")
78+
testthat::skip_if_not_installed("rworkflows")
79+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
80+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
81+
d <- desc::desc(desc_path)
82+
res <- echogithub:::description_extract_i(
83+
desc_file = d,
84+
fields = c("owner"),
85+
verbose = FALSE
86+
)
87+
testthat::expect_equal(res$owner, "RajLabMSSM")
88+
})
89+
90+
test_that("description_extract_i extracts repo correctly", {
91+
testthat::skip_if_not_installed("desc")
92+
testthat::skip_if_not_installed("rworkflows")
93+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
94+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
95+
d <- desc::desc(desc_path)
96+
res <- echogithub:::description_extract_i(
97+
desc_file = d,
98+
fields = c("repo"),
99+
verbose = FALSE
100+
)
101+
testthat::expect_equal(res$repo, "echogithub")
102+
})
103+
104+
test_that("description_extract_i returns NULL when desc_file is NULL", {
105+
testthat::skip_if_not_installed("rworkflows")
106+
res <- echogithub:::description_extract_i(desc_file = NULL,
107+
verbose = FALSE)
108+
testthat::expect_null(res)
109+
})
110+
111+
test_that("description_extract_i extracts DESCRIPTION-native fields", {
112+
testthat::skip_if_not_installed("desc")
113+
testthat::skip_if_not_installed("rworkflows")
114+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
115+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
116+
d <- desc::desc(desc_path)
117+
res <- echogithub:::description_extract_i(
118+
desc_file = d,
119+
fields = c("Package", "Version"),
120+
verbose = FALSE
121+
)
122+
testthat::expect_true("Package" %in% names(res))
123+
testthat::expect_equal(res$Package, "echogithub")
124+
})
125+
126+
test_that("description_extract_i as_datatable returns data.table", {
127+
testthat::skip_if_not_installed("desc")
128+
testthat::skip_if_not_installed("rworkflows")
129+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
130+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
131+
d <- desc::desc(desc_path)
132+
res <- echogithub:::description_extract_i(
133+
desc_file = d,
134+
fields = c("Package"),
135+
as_datatable = TRUE,
136+
verbose = FALSE
137+
)
138+
testthat::expect_true(data.table::is.data.table(res))
139+
})
140+
141+
test_that("description_extract_i extracts github_url field", {
142+
testthat::skip_if_not_installed("desc")
143+
testthat::skip_if_not_installed("rworkflows")
144+
desc_path <- system.file("DESCRIPTION", package = "echogithub")
145+
testthat::skip_if(desc_path == "", message = "echogithub not installed")
146+
d <- desc::desc(desc_path)
147+
res <- echogithub:::description_extract_i(
148+
desc_file = d,
149+
fields = c("github_url"),
150+
verbose = FALSE
151+
)
152+
testthat::expect_true("github_url" %in% names(res))
153+
testthat::expect_true(grepl("github.com", res$github_url))
154+
})

tests/testthat/test-github_api.R

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
## ---- github_code ----
2+
3+
test_that("github_code searches and returns data.table", {
4+
testthat::skip_if_offline()
5+
testthat::skip_if(
6+
nchar(gh::gh_token()) == 0,
7+
message = "No GitHub token available"
8+
)
9+
res <- tryCatch(
10+
echogithub::github_code(query = "echogithub path:DESCRIPTION",
11+
.limit = 5,
12+
verbose = FALSE),
13+
error = function(e) {
14+
testthat::skip(paste("GitHub API error:", e$message))
15+
}
16+
)
17+
testthat::expect_true(data.table::is.data.table(res))
18+
testthat::expect_true(nrow(res) >= 1)
19+
testthat::expect_true("owner_repo" %in% names(res))
20+
})
21+
22+
## ---- github_repositories ----
23+
24+
test_that("github_repositories searches and returns data.table", {
25+
testthat::skip_if_offline()
26+
testthat::skip_if(
27+
nchar(gh::gh_token()) == 0,
28+
message = "No GitHub token available"
29+
)
30+
res <- tryCatch(
31+
echogithub::github_repositories(query = "echogithub user:RajLabMSSM",
32+
.limit = 5,
33+
verbose = FALSE),
34+
error = function(e) {
35+
testthat::skip(paste("GitHub API error:", e$message))
36+
}
37+
)
38+
testthat::expect_true(data.table::is.data.table(res))
39+
testthat::expect_true(nrow(res) >= 1)
40+
testthat::expect_true("owner_repo" %in% names(res))
41+
})
42+
43+
test_that("github_repositories handles vector query", {
44+
testthat::skip_if_offline()
45+
testthat::skip_if(
46+
nchar(gh::gh_token()) == 0,
47+
message = "No GitHub token available"
48+
)
49+
res <- tryCatch(
50+
echogithub::github_repositories(query = c("language:r", "user:RajLabMSSM"),
51+
.limit = 5,
52+
verbose = FALSE),
53+
error = function(e) {
54+
testthat::skip(paste("GitHub API error:", e$message))
55+
}
56+
)
57+
testthat::expect_true(data.table::is.data.table(res))
58+
})
59+
60+
## ---- github_user_events ----
61+
62+
test_that("github_user_events returns data.table for known user", {
63+
testthat::skip_if_offline()
64+
testthat::skip_if(
65+
nchar(gh::gh_token()) == 0,
66+
message = "No GitHub token available"
67+
)
68+
res <- tryCatch(
69+
echogithub::github_user_events(owner = "bschilder",
70+
.limit = 10,
71+
public_only = TRUE,
72+
verbose = FALSE),
73+
error = function(e) {
74+
testthat::skip(paste("GitHub API error:", e$message))
75+
}
76+
)
77+
if (!is.null(res)) {
78+
testthat::expect_true(data.table::is.data.table(res))
79+
testthat::expect_true("owner" %in% names(res))
80+
testthat::expect_true("owner_repo" %in% names(res))
81+
}
82+
})
83+
84+
test_that("github_user_events returns NULL for nonexistent user with error=FALSE", {
85+
testthat::skip_if_offline()
86+
testthat::skip_if(
87+
nchar(gh::gh_token()) == 0,
88+
message = "No GitHub token available"
89+
)
90+
res <- tryCatch(
91+
echogithub::github_user_events(
92+
owner = "this_user_definitely_does_not_exist_12345xyz",
93+
error = FALSE,
94+
.limit = 5,
95+
verbose = FALSE
96+
),
97+
error = function(e) {
98+
testthat::skip(paste("GitHub API error:", e$message))
99+
}
100+
)
101+
## Should be NULL because the user doesn't exist and error=FALSE
102+
testthat::expect_null(res)
103+
})
104+
105+
test_that("github_user_events public_only changes endpoint", {
106+
testthat::skip_if_offline()
107+
testthat::skip_if(
108+
nchar(gh::gh_token()) == 0,
109+
message = "No GitHub token available"
110+
)
111+
## Just verify both modes run without error
112+
res_public <- tryCatch(
113+
echogithub::github_user_events(owner = "bschilder",
114+
.limit = 5,
115+
public_only = TRUE,
116+
verbose = FALSE),
117+
error = function(e) {
118+
testthat::skip(paste("GitHub API error:", e$message))
119+
}
120+
)
121+
res_all <- tryCatch(
122+
echogithub::github_user_events(owner = "bschilder",
123+
.limit = 5,
124+
public_only = FALSE,
125+
verbose = FALSE),
126+
error = function(e) {
127+
testthat::skip(paste("GitHub API error:", e$message))
128+
}
129+
)
130+
## Both should return data.tables (or NULL if no events)
131+
if (!is.null(res_public)) {
132+
testthat::expect_true(data.table::is.data.table(res_public))
133+
}
134+
if (!is.null(res_all)) {
135+
testthat::expect_true(data.table::is.data.table(res_all))
136+
}
137+
})
138+
139+
## ---- github_files_httr ----
140+
141+
test_that("github_files_httr lists files from known repo", {
142+
testthat::skip_if_offline()
143+
testthat::skip_if_not_installed("httr")
144+
res <- tryCatch(
145+
echogithub:::github_files_httr(
146+
owner = "RajLabMSSM",
147+
repo = "echogithub",
148+
branch = "master",
149+
verbose = FALSE
150+
),
151+
error = function(e) {
152+
testthat::skip(paste("GitHub API error:", e$message))
153+
}
154+
)
155+
testthat::expect_true(data.table::is.data.table(res))
156+
testthat::expect_true(nrow(res) >= 1)
157+
testthat::expect_true("path" %in% names(res))
158+
testthat::expect_true("link" %in% names(res))
159+
## Should contain the DESCRIPTION file
160+
testthat::expect_true(any(grepl("DESCRIPTION", res$path)))
161+
})
162+
163+
test_that("github_files_httr link column has proper GitHub URLs", {
164+
testthat::skip_if_offline()
165+
testthat::skip_if_not_installed("httr")
166+
res <- tryCatch(
167+
echogithub:::github_files_httr(
168+
owner = "RajLabMSSM",
169+
repo = "echogithub",
170+
branch = "master",
171+
verbose = FALSE
172+
),
173+
error = function(e) {
174+
testthat::skip(paste("GitHub API error:", e$message))
175+
}
176+
)
177+
## All links should start with https://github.com
178+
testthat::expect_true(all(grepl("^https://github.com", res$link)))
179+
})

0 commit comments

Comments
 (0)