a b/tests/testthat/test-combine_layers.R
1
context("combine_layers")
2
3
graph1 <- igraph::graph_from_data_frame(
4
    list(from = c("A", "B"), to = c("B", "C")), directed = FALSE)
5
graph2 <- igraph::graph_from_data_frame(
6
    list(from = c(1), to = c(2)), directed = FALSE)
7
graph3 <- igraph::make_empty_graph(directed = FALSE)
8
graph1.1 <- set_vertex_attr(graph1, name = "type", value = 'ty')
9
10
interaction.df1 <- as.data.frame(list(from = c("C", "B"), to = c(1, 2)))
11
interaction.df2 <- as.data.frame(list(from = c("D", "D"), to = c(3, 4)))
12
interaction.df1.graph <- igraph::graph_from_data_frame(as.data.frame(
13
    list(from = c("C", "B"), to = c(1, 2))), directed = FALSE)
14
15
16
graph1.list <- list(graph1, graph1)
17
graph2.list <- list(graph2, graph2)
18
graph3.list <- list(graph1, graph1, graph1)
19
class(graph1.list) <- class(graph2.list) <- class(graph3.list) <- "list.igraph"
20
21
graph1.list.named <- list(graph1 = graph1, graph2 = graph1)
22
graph2.list.named <- list(graph1 = graph2, graph2 = graph2)
23
graph3.list.named <- list(graph2 = graph2, graph1 = graph2)
24
graph4.list.named <- list(graph3 = graph2, graph2 = graph2)
25
class(graph1.list.named) <- class(graph2.list.named) <- 
26
    class(graph3.list.named) <- class(graph4.list.named)  <- "list.igraph"
27
28
29
test_that("get_grn fails on invalid input", {
30
    expect_error(combine_layers(graph1 = ""), "graph1 must be an igraph or list.igraph object", fixed = TRUE)
31
    expect_error(combine_layers(graph1, graph2 = ""), "graph2 must be an igraph or list.igraph object or NULL", fixed = TRUE)
32
    expect_error(combine_layers(graph1, graph2, interaction.df = ""))
33
    expect_error(combine_layers(graph1, graph2 = graph2.list), "graph1 and graph2 must have the same length", fixed = TRUE)
34
    expect_error(combine_layers(graph1.list, graph2 = graph3.list), "graph1 and graph2 must have the same length", fixed = TRUE)
35
    
36
    expect_error(combine_layers(graph1.list, graph2 = graph3.list), "graph1 and graph2 must have the same length", fixed = TRUE)
37
    expect_error(combine_layers(graph1.list.named, graph2 = graph4.list.named), "graph1 and graph2 must have the same names", fixed = TRUE)
38
    
39
})
40
41
42
test_that("combine_layers works", {
43
    # graph1
44
    expect_is(combine_layers(graph1 = graph1), "igraph")
45
    
46
    # graph1 and graph2
47
    expect_is(combine_layers(graph1 = graph1, graph2 = graph2), "merged.igraph")
48
    expect_is(combine_layers(graph1 = graph1.1, graph2 = graph1.1), "merged.igraph")
49
    
50
    
51
    # graph1 and interaction.df
52
    expect_is(combine_layers(graph1 = graph1, interaction.df = interaction.df1), "merged.igraph")
53
    #expect_warning(combine_layers(graph1 = graph1, interaction.df = interaction.df2), "Some, but not all graphs are named, not using vertex names", fixed = TRUE)
54
    expect_is(combine_layers(graph1 = graph1, interaction.df = interaction.df2), "merged.igraph")
55
56
57
    expect_is(combine_layers(graph1 = graph1, graph2 = graph2, interaction.df = interaction.df1), "merged.igraph")
58
    expect_is(combine_layers(graph1 = graph1, graph2 = graph2, interaction.df = interaction.df2), "merged.igraph")
59
    expect_is(combine_layers(graph1 = graph1.list, graph2 = graph2, interaction.df = interaction.df1.graph), "list.merged.igraph")
60
    
61
    
62
    expect_is(combine_layers(graph1 = graph1.list, graph2 = graph2), "list.merged.igraph")
63
    expect_is(combine_layers(graph1 = graph1.list, graph2 = graph2, interaction.df = interaction.df1), "list.merged.igraph")
64
    expect_is(combine_layers(graph1 = graph1.list, graph2 = NULL, interaction.df = interaction.df1), "list.merged.igraph")
65
    
66
    
67
    expect_is(combine_layers(graph1 = graph1.list, graph2 = graph2.list), "list.merged.igraph")
68
    expect_is(combine_layers(graph1 = graph1.list, graph2 = graph2.list, interaction.df = interaction.df1), "list.merged.igraph")
69
    
70
    #named list
71
    expect_is(combine_layers(graph1.list.named, graph2 = graph2.list.named), "list.merged.igraph")
72
    expect_is(combine_layers(graph1.list.named, graph2 = graph3.list.named), "list.merged.igraph")
73
    
74
    # with empty graph
75
    expect_warning(combine_layers(graph1, graph2 = graph3), "Some, but not all graphs are named, not using vertex names", fixed = TRUE)
76
    
77
    
78
})
79