Skip to content

Commit 4ec337c

Browse files
Add mock tests
1 parent 02cb257 commit 4ec337c

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

R/content.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,27 +341,21 @@ Content <- R6::R6Class(
341341
parser = NULL
342342
)
343343
if (httr::status_code(resp) == 404) {
344-
resp <- NULL
345-
} else {
346-
con$raise_error(resp)
347-
resp <- httr::content(resp, as = "parsed")
344+
# 404 means there is no repository set
345+
return(NULL)
348346
}
349-
resp
347+
con$raise_error(resp)
348+
httr::content(resp, as = "parsed")
350349
},
351350
#' @description Adjust Git polling.
352351
#' @param polling Polling enabled.
353352
repo_enable = function(polling = TRUE) {
354353
con <- self$get_connect()
355354
guid <- self$get_content()$guid
356-
resp <- con$PATCH(
355+
con$PATCH(
357356
v1_url("content", guid, "repository"),
358357
body = list(polling = polling)
359358
)
360-
if (httr::status_code(resp) == 404) {
361-
stop("This content item is not git-backed")
362-
}
363-
con$raise_error(resp)
364-
httr::content(resp, as = "parsed")
365359
},
366360
#' @description Adjust Git repository
367361
#' @param repository Git repository URL
@@ -380,7 +374,7 @@ Content <- R6::R6Class(
380374
body = list(
381375
repository = repository,
382376
branch = branch,
383-
directory = subdirectory,
377+
directory = directory,
384378
polling = polling
385379
)
386380
)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
This content item returns 403 errors when setting and deleting vanity URLs.
1+
This content item returns 403 errors when setting and deleting vanity URLs.
2+
3+
It is also not git-backed, so repository is a 404
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
structure(
2+
list(
3+
url = "__api__/v1/content/c3426b0b/repository",
4+
status_code = 404L,
5+
content = charToRaw(
6+
"{}"
7+
)
8+
),
9+
class = "response"
10+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"repository": "https://github.com/dbkegley/rsc-sample-content",
3+
"branch": "main",
4+
"directory": "local/python-shiny",
5+
"polling": true,
6+
"last_error": "thread caused non-unwinding panic. aborting. (signal: aborted (core dumped))",
7+
"last_known_commit": "5e69ff238376182fa7ed38b30497d457684c6e24"
8+
}

tests/testthat/test-git.R

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,36 @@ without_internet({
3939
})
4040

4141
with_mock_api({
42-
test_that("we can retrieve a content item", {
42+
test_that("we can retrieve a repository information if it exists", {
4343
con <- Connect$new(server = "https://connect.example", api_key = "fake")
4444
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
45+
expect_true(item$repository()$polling)
46+
})
47+
48+
test_that("repository is null if it is not set", {
49+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
50+
item <- content_item(con, "c3426b0b-e21d-3d80-c698-a935ad614066")
51+
expect_null(item$repository())
52+
})
53+
54+
test_that("we can set a repository", {
55+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
56+
item <- content_item(con, "c3426b0b-e21d-3d80-c698-a935ad614066")
57+
expect_PUT(
58+
item$repo_set(repository = "https://github.com/posit-dev/connectapi"),
59+
"https://connect.example/__api__/v1/content/c3426b0b/repository",
60+
'{"repository":"https://github.com/posit-dev/connectapi",',
61+
'"branch":"main","directory":".","polling":false}'
62+
)
63+
})
64+
65+
test_that("we can enable polling", {
66+
con <- Connect$new(server = "https://connect.example", api_key = "fake")
67+
item <- content_item(con, "f2f37341-e21d-3d80-c698-a935ad614066")
68+
expect_PATCH(
69+
item$repo_enable(TRUE),
70+
"https://connect.example/__api__/v1/content/f2f37341-e21d-3d80-c698-a935ad614066/repository",
71+
'{"polling":true}'
72+
)
4573
})
4674
})

0 commit comments

Comments
 (0)