a b/tests/testthat/test-RWR.R
1
context("RWR")
2
3
graph1 <- igraph::graph_from_data_frame(list(from = c("A", "B", "A", "D", "C", "A", "C"), 
4
                                             to = c("B", "C", "D", "E", "D", "F", "G")), directed = FALSE)
5
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("A","B","C"),value = "1")
6
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("D","E"),value = "2")
7
graph1 <- set_vertex_attr(graph = graph1, name = 'type', index = c("F", "G"),value = "3")
8
9
rwr_res <- random_walk_restart(X = graph1, seed = c("A", "B", "C", "D", "E"))
10
rwr_res_type <- rwr_find_seeds_between_attributes(X = rwr_res, attribute = "type", k = 3)
11
12
graph1.list <- list("X" = graph1, "Y"= graph1)
13
14
rwr_res.list <- random_walk_restart(X = graph1.list, seed = c("A", "B", "C", "D", "E"))
15
rwr_res_type.list <- rwr_find_seeds_between_attributes(X = rwr_res.list, attribute = "type", k = 3)
16
17
test_that("random_walk_with_restart fails with invalid input", {
18
    # X
19
    expect_error(random_walk_restart(X=NULL))
20
    expect_error(random_walk_restart(X=data.frame()))
21
    expect_error(random_walk_restart(X=list(3)))
22
    
23
    # seed
24
    expect_error(random_walk_restart(X=graph1, seed = 3))
25
    
26
    # r
27
    expect_error(random_walk_restart(X=graph1, r = -2), "'r' must be a numeric value between 0 and 1", fixed = TRUE)
28
    expect_error(random_walk_restart(X=graph1, r = matrix(2)), "'r' must be a numeric value", fixed = TRUE)
29
    
30
})
31
32
test_that("random_walk_with_restart works", {
33
    
34
    # X
35
    expect_is(random_walk_restart(graph1.list), "list.rwr")
36
    expect_is(random_walk_restart(graph1), "rwr")
37
    
38
    # seed
39
    expect_is(random_walk_restart(graph1, seed = 'A'), "rwr")
40
    expect_is(random_walk_restart(graph1.list, seed = "A"), "list.rwr")
41
    expect_is(random_walk_restart(graph1, seed = NULL), "rwr")
42
43
    # r
44
    expect_is(random_walk_restart(graph1, seed = NULL, r = 0.4), "rwr")
45
    
46
})
47
48
49
50
test_that("rwr_find_seeds_between_attributes fails with invalid input", {
51
    # X
52
    expect_error(rwr_find_seeds_between_attributes(X=NULL))
53
    expect_error(rwr_find_seeds_between_attributes(X=matrix()))
54
    
55
    # k
56
    expect_error(rwr_find_seeds_between_attributes(X=rwr_res, k = -5), "'k' must be a numeric value between 0 and 200", fixed = TRUE)
57
    expect_error(rwr_find_seeds_between_attributes(X=rwr_res, k = list(3)),"'k' must be a numeric value", fixed = TRUE)
58
    
59
    # seed / attribute
60
    expect_error(rwr_find_seeds_between_attributes(X=rwr_res, seed = matrix(3)), "'seed' must be a charactor vector", fixed = TRUE)
61
    expect_error(rwr_find_seeds_between_attributes(X=rwr_res, attribute = matrix(3)), "'attribute' must be a charactor vector", fixed = TRUE)
62
    expect_error(rwr_find_seeds_between_attributes(X=rwr_res, attribute = c("4", "5")), "invalid length", fixed = TRUE)
63
})
64
65
66
test_that("rwr_find_seeds_between_attributes works", {
67
    # X
68
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res), "rwr.attributes")
69
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res.list), "list.rwr.attributes")
70
    
71
    # seed
72
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res, seed = logical(0)), "rwr.attributes")
73
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res, attribute = logical(0)), "rwr.attributes")
74
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res, seed = "Z"), "rwr.attributes")
75
    
76
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res.list, seed = "Z"), "list.rwr.attributes")
77
    
78
    
79
    # k 
80
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res, k = 1) , "rwr.attributes")
81
    expect_is(rwr_find_seeds_between_attributes(X=rwr_res, k = NULL), "rwr.attributes")
82
    
83
})
84
85
86
test_that("rwr_find_closest_type fails with invalid input", {
87
    # X
88
    expect_error(rwr_find_closest_type(X=NULL))
89
    expect_error(rwr_find_closest_type(X=matrix()))
90
    
91
    # seed / attribute
92
    expect_error(rwr_find_closest_type(X=rwr_res, seed = matrix(3)), "'seed' must be a charactor vector", fixed = TRUE)
93
    expect_error(rwr_find_closest_type(X=rwr_res, attribute = matrix(3)), "'attribute' must be a charactor vector", fixed = TRUE)
94
    expect_error(rwr_find_closest_type(X=rwr_res, attribute = c("4", "5")), "invalid length", fixed = TRUE)
95
    
96
    # value
97
    expect_error(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = c("a", "b")), "invalid length", fixed = TRUE)
98
    expect_error(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = matrix(1,2)), "'value' must be a charactor vector", fixed = TRUE)
99
    
100
})
101
102
103
test_that("rwr_find_closest_type works", {
104
    # X
105
    expect_is(rwr_find_closest_type(X=rwr_res), "rwr.closest")
106
    expect_is(rwr_find_closest_type(X=rwr_res.list), "list.rwr.closest")
107
    
108
    # seeds / attributes 
109
    expect_is(rwr_find_closest_type(X=rwr_res, seed = logical(0)), "rwr.closest")
110
    expect_is(rwr_find_closest_type(X=rwr_res, attribute = logical(0)), "rwr.closest")
111
    expect_is(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL), "rwr.closest")
112
    expect_is(rwr_find_closest_type(X=rwr_res, attribute = "NULL", seed = NULL), "rwr.closest")
113
    
114
    #value 
115
    expect_is(rwr_find_closest_type(X=rwr_res, attribute = "NULL", seed = NULL, value = NULL), "rwr.closest")
116
    expect_is(rwr_find_closest_type(X=rwr_res, attribute = NULL, seed = NULL, value = "test"), "rwr.closest")
117
    expect_is(rwr_find_closest_type(X=rwr_res, value = "test"), "rwr.closest")
118
    
119
    expect_is(rwr_find_closest_type(X=rwr_res, seed = "Z"), "rwr.closest")
120
    expect_is(rwr_find_closest_type(X=rwr_res.list, seed = "Z"), "list.rwr.closest")
121
    
122
})
123
124
# test_that("top_k_graph returns NULL", {
125
#     rwr_top_k_graph
126
#     rwr_top_k_graph(X = rwr_res$graph, RWRM_Result_Object = rwr_res$rwr, Seed = "A", k = 1)
127
# })
128