1context("Print graph summary") 2 3get_printed_output <- function(graph, line = NULL) { 4 5 captured_output <- 6 capture_output( 7 graph %>% print()) %>% 8 stringr::str_split(pattern = "\n") %>% 9 unlist() 10 11 if (!is.null(line)) { 12 captured_output <- captured_output[line] 13 } 14 15 captured_output 16} 17 18test_that("Printing a summary of an empty graph works", { 19 20 graph <- create_graph() 21 22 expect_equal( 23 graph %>% get_printed_output(1), 24 "DiagrammeR Graph // no nodes") 25 26 expect_equal( 27 graph %>% get_printed_output(2), 28 " -- empty graph (mode: directed)") 29 30 expect_equal( 31 create_graph(directed = FALSE) %>% get_printed_output(2), 32 " -- empty graph (mode: undirected)") 33 34 expect_equal( 35 graph %>% get_printed_output(3), "") 36 37 expect_equal( 38 graph %>% 39 get_printed_output(4) %>% substr(1, 45) %>% stringr::str_trim(), 40 "NODES / type: <unused> / label: <unused>") 41 42 expect_equal( 43 graph %>% get_printed_output(5) %>% substr(1, 45) %>% stringr::str_trim(), 44 "-- no additional node attributes") 45 46 expect_equal( 47 graph %>% 48 get_printed_output(6) %>% substr(1, 45) %>% stringr::str_trim(), 49 "EDGES / rel: <unused>") 50 51 expect_equal( 52 graph %>% 53 get_printed_output(7) %>% substr(1, 45) %>% stringr::str_trim(), 54 "-- no additional edge attributes") 55 56 expect_equal( 57 graph %>% 58 get_printed_output(8) %>% substr(1, 45) %>% stringr::str_trim(), 59 "SELECTION / <none>") 60 61 expect_equal( 62 graph %>% 63 get_printed_output(9) %>% substr(1, 45) %>% stringr::str_trim(), 64 "CACHE / <none>") 65 66 expect_equal( 67 graph %>% 68 get_printed_output(10) %>% substr(1, 30) %>% stringr::str_trim(), 69 "GLOBAL ATTRS / 17 are set") 70 71 expect_equal( 72 graph %>% 73 get_printed_output(11) %>% substr(1, 45) %>% stringr::str_trim(), 74 "GRAPH ACTIONS / <none>") 75 76 expect_equal( 77 graph %>% 78 get_printed_output(12) %>% substr(1, 45) %>% stringr::str_trim(), 79 "GRAPH LOG / create_graph()") 80}) 81 82test_that("Printing a summary of node types works", { 83 84 graph_type_complete <- 85 create_graph() %>% 86 add_path( 87 n = 4, 88 type = "a") 89 90 graph_type_incomplete <- 91 create_graph() %>% 92 add_path( 93 n = 4, 94 type = c("a", "a", NA, NA)) 95 96 expect_equal( 97 graph_type_complete %>% get_printed_output(4) %>% stringr::str_trim(), 98 "NODES / type: 1 vals - complete / label: 4 vals - complete & unique") 99 100 expect_equal( 101 graph_type_incomplete %>% get_printed_output(4) %>% stringr::str_trim(), 102 "NODES / type: 1 val / label: 4 vals - complete & unique") 103}) 104 105test_that("Printing a summary of node labels works", { 106 107 graph_label_complete <- 108 create_graph() %>% 109 add_path( 110 n = 4, 111 label = c(1, 2, 3, 4)) 112 113 graph_label_incomplete <- 114 create_graph() %>% 115 add_path( 116 n = 4, 117 label = c(1, 2, NA, NA)) 118 119 expect_equal( 120 graph_label_complete %>% 121 get_printed_output(4) %>% substr(1, 60) %>% stringr::str_trim(), 122 "NODES / type: <unused> / label: 4 vals - complete & unique") 123 124 expect_equal( 125 graph_label_incomplete %>% 126 get_printed_output(4) %>% substr(1, 45) %>% stringr::str_trim(), 127 "NODES / type: <unused> / label: 2 vals") 128}) 129 130test_that("Printing a summary of extra node attrs works", { 131 132 graph_node_attr_1 <- 133 create_graph() %>% 134 add_path( 135 n = 4, 136 node_data = node_data( 137 value_1 = c(1, 2, 3, 4))) 138 139 graph_node_attr_2 <- 140 create_graph() %>% 141 add_path( 142 n = 4, 143 node_data = node_data( 144 value_1 = c(1, 2, 3, 4), 145 value_2 = c(5, 6, 7, 9))) 146 147 graph_node_attr_4 <- 148 create_graph() %>% 149 add_path( 150 n = 4, 151 node_data = node_data( 152 value_1 = c(1, 2, 3, 4), 153 value_2 = c(5, 6, 7, 9), 154 ctg_1 = c("a", "b", "c", "d"), 155 ctg_2 = c("e", "f", "g", "h"))) 156 157 expect_equal( 158 graph_node_attr_1 %>% 159 get_printed_output(5) %>% stringr::str_trim(), 160 "-- 1 additional node attribute (value_1)") 161 162 expect_equal( 163 graph_node_attr_2 %>% 164 get_printed_output(5) %>% stringr::str_trim(), 165 "-- 2 additional node attributes (value_1, value_2)") 166 167 expect_equal( 168 graph_node_attr_4 %>% 169 get_printed_output(5) %>% stringr::str_trim(), 170 "-- 4 additional node attributes (value_1, value_2, ctg_1 + 1 more)") 171}) 172 173test_that("Printing a summary of edge rel values works", { 174 175 graph_rel_complete <- 176 create_graph() %>% 177 add_path( 178 n = 4, 179 label = c(1, 2, 3, 4), 180 rel = c("a", "a", "b")) 181 182 graph_rel_incomplete <- 183 create_graph() %>% 184 add_path( 185 n = 4, 186 label = c(1, 2, 3, 4), 187 rel = c("a", "b", NA)) 188 189 expect_equal( 190 graph_rel_complete %>% 191 get_printed_output(6) %>% substr(1, 45) %>% stringr::str_trim(), 192 "EDGES / rel: 2 vals - complete") 193 194 expect_equal( 195 graph_rel_incomplete %>% 196 get_printed_output(4) %>% substr(1, 60) %>% stringr::str_trim(), 197 "NODES / type: <unused> / label: 4 vals - complete & unique") 198}) 199 200test_that("Printing a summary of extra edge attrs works", { 201 202 graph_edge_attr_1 <- 203 create_graph() %>% 204 add_path( 205 n = 5, 206 edge_data = edge_data( 207 value_1 = c(1, 2, 3, 4))) 208 209 graph_edge_attr_2 <- 210 create_graph() %>% 211 add_path( 212 n = 5, 213 edge_data = edge_data( 214 value_1 = c(1, 2, 3, 4), 215 value_2 = c(5, 6, 7, 9))) 216 217 graph_edge_attr_4 <- 218 create_graph() %>% 219 add_path( 220 n = 5, 221 edge_data = edge_data( 222 value_1 = c(1, 2, 3, 4), 223 value_2 = c(5, 6, 7, 9), 224 ctg_1 = c("a", "b", "c", "d"), 225 ctg_2 = c("e", "f", "g", "h"))) 226 227 expect_equal( 228 graph_edge_attr_1 %>% 229 get_printed_output(7) %>% stringr::str_trim(), 230 "-- 1 additional edge attribute (value_1)") 231 232 expect_equal( 233 graph_edge_attr_2 %>% 234 get_printed_output(7) %>% stringr::str_trim(), 235 "-- 2 additional edge attributes (value_1, value_2)") 236 237 expect_equal( 238 graph_edge_attr_4 %>% 239 get_printed_output(7) %>% stringr::str_trim(), 240 "-- 4 additional edge attributes (value_1, value_2, ctg_1 + 1 more)") 241}) 242 243test_that("Describing if a graph is weighted works", { 244 245 graph_weighted <- 246 create_graph() %>% 247 add_path( 248 n = 5, 249 edge_data = edge_data( 250 weight = c(1, 2, 3, 4))) 251 252 graph_unweighted <- 253 create_graph() %>% 254 add_path(n = 5) 255 256 expect_true( 257 stringr::str_detect( 258 graph_weighted %>% get_printed_output(2), 259 "weighted")) 260 261 expect_false( 262 stringr::str_detect( 263 graph_unweighted %>% get_printed_output(2), 264 "weighted")) 265}) 266 267test_that("Describing if a graph is a DAG works", { 268 269 graph_dag <- 270 create_graph() %>% 271 add_path(n = 5) 272 273 graph_cycle <- 274 create_graph() %>% 275 add_cycle(n = 5) 276 277 expect_true( 278 stringr::str_detect( 279 graph_dag %>% get_printed_output(2), 280 "DAG")) 281 282 expect_false( 283 stringr::str_detect( 284 graph_cycle %>% get_printed_output(2), 285 "DAG")) 286}) 287 288test_that("Describing if a graph is a property graph works", { 289 290 graph_pg <- 291 create_graph() %>% 292 add_path( 293 n = 4, 294 type = "a", 295 label = 1:4, 296 rel = c("a", "a", "b", "b")) 297 298 graph_not_pg <- 299 create_graph() %>% 300 add_path( 301 n = 4, 302 label = 1:4, 303 rel = c("a", "a", "b", "b")) 304 305 expect_true( 306 stringr::str_detect( 307 graph_pg %>% get_printed_output(2), 308 "property graph")) 309 310 expect_false( 311 stringr::str_detect( 312 graph_not_pg %>% get_printed_output(2), 313 "property graph")) 314}) 315 316test_that("Describing if a graph is a simple graph works", { 317 318 graph_simple <- 319 create_graph() %>% 320 add_path( 321 n = 4) 322 323 graph_not_simple <- 324 create_graph() %>% 325 add_path( 326 n = 4) %>% 327 add_edges_w_string( 328 edges = "1->2") 329 330 expect_true( 331 stringr::str_detect( 332 graph_simple %>% get_printed_output(2), 333 "simple")) 334 335 expect_false( 336 stringr::str_detect( 337 graph_not_simple %>% get_printed_output(2), 338 "simple")) 339}) 340 341test_that("Describing if a graph is a connected or not works", { 342 343 graph_connected <- 344 create_graph() %>% 345 add_path( 346 n = 4) 347 348 graph_not_connected <- 349 create_graph() %>% 350 add_n_nodes(n = 4) 351 352 expect_true( 353 stringr::str_detect( 354 graph_connected %>% get_printed_output(2), 355 " connected ")) 356 357 expect_true( 358 stringr::str_detect( 359 graph_not_connected %>% get_printed_output(2), 360 " disconnected ")) 361}) 362 363test_that("The number of reported nodes is correct", { 364 365 graph_no_nodes <- 366 create_graph() 367 368 graph_1_node <- 369 create_graph() %>% 370 add_node() 371 372 graph_4_nodes <- 373 create_graph() %>% 374 add_n_nodes(n = 4) 375 376 expect_true( 377 stringr::str_detect( 378 graph_no_nodes %>% get_printed_output(1), 379 " no nodes")) 380 381 expect_true( 382 stringr::str_detect( 383 graph_1_node %>% get_printed_output(1), 384 " 1 node")) 385 386 expect_true( 387 stringr::str_detect( 388 graph_4_nodes %>% get_printed_output(1), 389 " 4 nodes")) 390}) 391 392test_that("The number of reported edges is correct", { 393 394 graph_no_edges <- 395 create_graph() %>% 396 add_n_nodes(n = 4) 397 398 graph_1_edge <- 399 create_graph() %>% 400 add_path(n = 2) 401 402 graph_3_edges <- 403 create_graph() %>% 404 add_path(n = 4) 405 406 expect_false( 407 stringr::str_detect( 408 graph_no_edges %>% get_printed_output(1), 409 " edge")) 410 411 expect_true( 412 stringr::str_detect( 413 graph_1_edge %>% get_printed_output(1), 414 " 1 edge")) 415 416 expect_true( 417 stringr::str_detect( 418 graph_3_edges %>% get_printed_output(1), 419 " 3 edges")) 420 421 expect_true( 422 stringr::str_detect( 423 graph_3_edges %>% get_printed_output(1), 424 "DiagrammeR Graph // 4 nodes / 3 edges")) 425}) 426 427test_that("Printing a summary line for node/edge selections works", { 428 429 graph_all_nodes_selected <- 430 create_graph() %>% 431 add_path(n = 4) %>% 432 select_nodes() 433 434 graph_2_nodes_selected <- 435 create_graph() %>% 436 add_path(n = 4) %>% 437 select_nodes_by_id(nodes = c(1, 2)) 438 439 graph_all_edges_selected <- 440 create_graph() %>% 441 add_path(n = 4) %>% 442 select_edges() 443 444 graph_2_edges_selected <- 445 create_graph() %>% 446 add_path(n = 4) %>% 447 select_edges_by_edge_id(edges = c(1, 2)) 448 449 expect_equal( 450 graph_all_nodes_selected %>% 451 get_printed_output(8) %>% substr(1, 45) %>% stringr::str_trim(), 452 "SELECTION / 4 nodes selected (ALL)") 453 454 expect_equal( 455 graph_2_nodes_selected %>% 456 get_printed_output(8) %>% substr(1, 45) %>% stringr::str_trim(), 457 "SELECTION / 2 nodes selected") 458 459 expect_equal( 460 graph_all_edges_selected %>% 461 get_printed_output(8) %>% substr(1, 45) %>% stringr::str_trim(), 462 "SELECTION / 3 edges selected (ALL)") 463 464 expect_equal( 465 graph_2_edges_selected %>% 466 get_printed_output(8) %>% substr(1, 45) %>% stringr::str_trim(), 467 "SELECTION / 2 edges selected") 468}) 469 470test_that("Printing a summary line for graph caches works", { 471 472 graph_no_cache <- 473 create_graph() %>% 474 add_path(n = 4) 475 476 graph_w_1_cache <- 477 create_graph() %>% 478 add_path(n = 4) %>% 479 set_cache( 480 name = "cache", 481 to_cache = get_node_ids(.)) 482 483 graph_w_2_caches <- 484 create_graph() %>% 485 add_path(n = 4) %>% 486 set_cache( 487 name = "cache_1", 488 to_cache = get_node_ids(.)) %>% 489 set_cache( 490 name = "cache_2", 491 to_cache = get_node_ids(.) * 2) 492 493 expect_equal( 494 graph_no_cache %>% 495 get_printed_output(9) %>% substr(1, 45) %>% stringr::str_trim(), 496 "CACHE / <none>") 497 498 expect_equal( 499 graph_w_1_cache %>% 500 get_printed_output(9) %>% substr(1, 45) %>% stringr::str_trim(), 501 "CACHE / 1 cache") 502 503 expect_equal( 504 graph_w_2_caches %>% 505 get_printed_output(9) %>% substr(1, 45) %>% stringr::str_trim(), 506 "CACHE / 2 caches") 507}) 508 509test_that("Printing a summary line for global graph attributes works", { 510 511 graph_default_global_attrs <- 512 create_graph() 513 514 graph_no_global_attrs <- 515 create_graph( 516 attr_theme = NULL) 517 518 expect_equal( 519 graph_default_global_attrs %>% 520 get_printed_output(10) %>% substr(1, 30) %>% stringr::str_trim(), 521 "GLOBAL ATTRS / 17 are set") 522 523 expect_equal( 524 graph_no_global_attrs %>% 525 get_printed_output(10) %>% substr(1, 30) %>% stringr::str_trim(), 526 "GLOBAL ATTRS / <none>") 527}) 528 529test_that("Printing a summary line for graph actions works", { 530 531 graph_no_actions <- 532 create_graph() 533 534 graph_w_1_action <- 535 create_graph() %>% 536 add_gnm_graph( 537 n = 10, 538 m = 22, 539 set_seed = 23) %>% 540 add_graph_action( 541 fcn = "set_node_attr_w_fcn", 542 node_attr_fcn = "get_betweenness", 543 column_name = "btwns", 544 action_name = "get_btwns") 545 546 graph_w_2_actions <- 547 create_graph() %>% 548 add_gnm_graph( 549 n = 10, 550 m = 22, 551 set_seed = 23) %>% 552 add_graph_action( 553 fcn = "set_node_attr_w_fcn", 554 node_attr_fcn = "get_pagerank", 555 column_name = "pagerank", 556 action_name = "get_pagerank") %>% 557 add_graph_action( 558 fcn = "rescale_node_attrs", 559 node_attr_from = "pagerank", 560 node_attr_to = "width", 561 action_name = "pagerank_to_width") 562 563 expect_equal( 564 graph_no_actions %>% 565 get_printed_output(11) %>% substr(1, 30) %>% stringr::str_trim(), 566 "GRAPH ACTIONS / <none>") 567 568 expect_equal( 569 graph_w_1_action %>% 570 get_printed_output(11) %>% substr(1, 30) %>% stringr::str_trim(), 571 "GRAPH ACTIONS / 1 is set") 572 573 expect_equal( 574 graph_w_2_actions %>% 575 get_printed_output(11) %>% substr(1, 30) %>% stringr::str_trim(), 576 "GRAPH ACTIONS / 2 are set") 577}) 578