Skip to content

Commit 0e9bdb4

Browse files
Improve error handling and node input handling
1 parent 765f317 commit 0e9bdb4

2 files changed

Lines changed: 96 additions & 6 deletions

File tree

R/caugi_graph.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ caugi_graph <- S7::new_class(
215215
)
216216
}
217217

218+
if (!is.null(nodes)) {
219+
if (!is.character(nodes)) {
220+
stop("`nodes` must be a character vector of node names.",
221+
call. = FALSE
222+
)
223+
}
224+
}
225+
218226
if (!simple && class != "UNKNOWN") {
219227
stop("If simple = FALSE, class must be 'UNKNOWN'", call. = FALSE)
220228
}
@@ -230,18 +238,20 @@ caugi_graph <- S7::new_class(
230238
# declared nodes contain all edge nodes: preserve their order
231239
declared <- nodes
232240
} else {
233-
stop("nodes must contain all nodes. ",
234-
"Edge nodes not in nodes: ",
235-
paste(setdiff(edge_node_names, nodes), collapse = ", "),
236-
call. = FALSE
237-
)
241+
# use edge order first, then add declared isolates
242+
declared <- unique(c(edge_node_names, nodes))
238243
}
239244
declared <- unique(c(declared, nodes))
240245
}
241246
} else if (has_vec) {
242247
edges <- .get_edges_tibble(from, edge, to, calls = list())
243248
declared <- nodes
244249
} else {
250+
if (build == TRUE && !missing(build)) {
251+
warning("No edges or nodes provided; graph will not be built.",
252+
call. = FALSE
253+
)
254+
}
245255
edges <- tibble::tibble(
246256
from = character(),
247257
edge = character(),

tests/testthat/test-caugi_graph.R

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,10 @@ test_that("caugi_graph preserves node order from nodes parameter", {
392392
})
393393

394394
# ──────────────────────────────────────────────────────────────────────────────
395-
# ────────────────────────────────────── Errors ────────────────────────────────
395+
# ─────────────────────────── Errors and warnings ──────────────────────────────
396396
# ──────────────────────────────────────────────────────────────────────────────
397397

398+
398399
test_that("caugi_graph errors with trailing commas", {
399400
expect_error(
400401
caugi_graph(
@@ -404,3 +405,82 @@ test_that("caugi_graph errors with trailing commas", {
404405
"Argument 3 is missing"
405406
)
406407
})
408+
409+
test_that("caugi_graph warns when build = TRUE for empty graph", {
410+
expect_warning(
411+
caugi_graph(build = TRUE),
412+
"No edges or nodes provided; graph will not be built."
413+
)
414+
})
415+
416+
test_that("caugi_graph with wrong node input errors", {
417+
expect_error(
418+
caugi_graph(
419+
from = c("A", "B"),
420+
edge = c("-->", "---"),
421+
to = c("B", "C"),
422+
nodes = list("D", "E")
423+
),
424+
"`nodes` must be a character vector"
425+
)
426+
})
427+
428+
# ──────────────────────────────────────────────────────────────────────────────
429+
# ─────────────────────────── .view_to_caugi_graph ─────────────────────────────
430+
# ──────────────────────────────────────────────────────────────────────────────
431+
432+
test_that(".view_to_caugi_graph works as expected", {
433+
cg <- caugi_graph(
434+
A %-->% B,
435+
B %---% C,
436+
C %<->% D
437+
)
438+
439+
cg2 <- .view_to_caugi_graph(cg@ptr)
440+
expect_s7_class(cg2, caugi_graph)
441+
expect_equal(cg, cg2)
442+
})
443+
444+
test_that(".view_to_caugi_graph fails on NULL ptr", {
445+
expect_error(
446+
.view_to_caugi_graph(NULL),
447+
"ptr is NULL"
448+
)
449+
})
450+
451+
test_that(".view_to_caugi_graph fails on faulty node_names", {
452+
cg <- caugi_graph(
453+
A %-->% B
454+
)
455+
expect_error(
456+
.view_to_caugi_graph(cg@ptr, node_names = c("A")),
457+
"length"
458+
)
459+
})
460+
461+
test_that(".view_to_caugi_graph works for empty cg", {
462+
cg <- caugi_graph(A, B, build = TRUE)
463+
expect_equal(cg, .view_to_caugi_graph(cg@ptr))
464+
})
465+
466+
# ──────────────────────────────────────────────────────────────────────────────
467+
# ───────────────────────── Freezing and unfreezing ────────────────────────────
468+
# ──────────────────────────────────────────────────────────────────────────────
469+
470+
test_that("freeze / unfreeze works as expected", {
471+
cg <- caugi_graph(
472+
A %-->% B,
473+
B %---% C
474+
)
475+
476+
# test if built is TRUE
477+
expect_true(cg@built)
478+
# currently frozen
479+
s <- cg@.state
480+
expect_error(s$built <- FALSE)
481+
482+
s <- caugi:::.unfreeze_state(cg@.state)
483+
s$built <- FALSE
484+
expect_false(s$built)
485+
expect_false(cg@.state$built)
486+
})

0 commit comments

Comments
 (0)