Skip to content

Commit 3a6c69d

Browse files
Write tests for as_igraph and fix small bug
1 parent 9c52413 commit 3a6c69d

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

R/as_igraph.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ as_igraph <- function(x, ...) {
3838
}
3939

4040
# edge validity for Unknown
41-
if (x@graph_class == "Unknown") {
41+
if (x@graph_class == "UNKNOWN") {
4242
if (any(!(et %in% c("-->", "<->", "---")))) {
4343
stop("Conversion to igraph is only supported for 'Unknown' caugi graphs ",
4444
" with '-->', '<->', or '---' edges.",

tests/testthat/test-as_igraph.R

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
test_that("cg --> as_igraph --> as_caugi --> as_igraph works for directed", {
2+
cg <- caugi_graph(
3+
A %-->% B + C,
4+
B %-->% D,
5+
C %-->% D,
6+
E %-->% F,
7+
class = "DAG"
8+
)
9+
10+
ig <- as_igraph(cg)
11+
cg2 <- as_caugi(ig, class = "DAG")
12+
13+
ig2 <- as_igraph(cg2)
14+
expect_equal(igraph::V(ig)$name, igraph::V(ig2)$name)
15+
expect_equal(igraph::as_data_frame(ig), igraph::as_data_frame(ig2))
16+
expect_equal(nodes(cg), nodes(cg2))
17+
expect_equal(edges(cg), edges(cg2))
18+
})
19+
20+
test_that("errors on non-caugi input", {
21+
expect_error(as_igraph("not-a-caugi"))
22+
})
23+
24+
25+
test_that("Unknown graph with undirected edges become undirected igraph", {
26+
cg <- caugi_graph(A %---% B + C,
27+
C %---% D,
28+
class = "Unknown"
29+
)
30+
ig <- as_igraph(cg)
31+
expect_false(igraph::is_directed(ig))
32+
})
33+
34+
test_that("Unknown with PAG edges fails", {
35+
cg <- caugi_graph(A %o-o% B %o->% C, class = "Unknown")
36+
expect_error(as_igraph(cg))
37+
})
38+
39+
test_that("empty graph handled with given vertices", {
40+
# Only nodes, no edges
41+
cg <- caugi_graph(A, B, C, class = "DAG")
42+
ig <- as_igraph(cg)
43+
expect_true(all(sort(igraph::V(ig)$name) == sort(nodes(cg)$name)))
44+
expect_equal(nrow(igraph::as_data_frame(ig)), 0L)
45+
# Directedness defaults to FALSE when no edge types present
46+
expect_false(igraph::is_directed(ig))
47+
})
48+
49+
test_that("all undirected edge types yield undirected igraph", {
50+
cg <- caugi_graph(A %---% B %<->% C, class = "Unknown")
51+
ig <- as_igraph(cg)
52+
expect_false(igraph::is_directed(ig))
53+
ed <- igraph::as_data_frame(ig)
54+
# undirected collapsed
55+
expect_true(any(ed$from == "A" & ed$to == "B") || any(ed$from == "B" & ed$to == "A"))
56+
expect_true(any(ed$from == "B" & ed$to == "C") || any(ed$from == "C" & ed$to == "B"))
57+
# no duplicates
58+
expect_equal(nrow(ed), 2L)
59+
})
60+
61+
test_that("all directed edges yield directed igraph", {
62+
cg <- caugi_graph(
63+
A %-->% B,
64+
B %-->% C,
65+
D %-->% C,
66+
class = "DAG"
67+
)
68+
ig <- as_igraph(cg)
69+
expect_true(igraph::is_directed(ig))
70+
ed <- igraph::as_data_frame(ig)
71+
expect_true(all(ed$from %in% c("A", "B", "D")))
72+
expect_true(all(ed$to %in% c("B", "C")))
73+
expect_equal(nrow(ed), 3L)
74+
})
75+
76+
test_that("mixed edges: directed kept, undirected duplicated as bidirected", {
77+
cg <- caugi_graph(
78+
A %-->% B,
79+
B %---% C,
80+
C %---% D,
81+
class = "PDAG"
82+
)
83+
ig <- as_igraph(cg)
84+
expect_true(igraph::is_directed(ig))
85+
ed <- igraph::as_data_frame(ig)
86+
# A->B remains single
87+
expect_true(any(ed$from == "A" & ed$to == "B"))
88+
# B---C becomes B->C and C->B
89+
expect_true(any(ed$from == "B" & ed$to == "C"))
90+
expect_true(any(ed$from == "C" & ed$to == "B"))
91+
# C<->D becomes C->D and D->C
92+
expect_true(any(ed$from == "C" & ed$to == "D"))
93+
expect_true(any(ed$from == "D" & ed$to == "C"))
94+
# total edges: 1 directed + 2*undirected (2 undirected edges) = 5
95+
expect_equal(nrow(ed), 5L)
96+
})

0 commit comments

Comments
 (0)