From e3050e17119e25c45a300c945af13e576437b0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20L=C3=B6ffler?= Date: Wed, 4 Dec 2024 16:13:19 +0100 Subject: [PATCH] Add test for 'convert.edge.attributes.to.list' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This works towards fixing #271. Signed-off-by: Maximilian Löffler --- tests/test-networks.R | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test-networks.R b/tests/test-networks.R index f1536ed9..0f1901d0 100644 --- a/tests/test-networks.R +++ b/tests/test-networks.R @@ -992,6 +992,54 @@ test_that("Addition of edge attributes with data", { }) + +patrick::with_parameters_test_that("Convert edge attributest to list", { + + ## configure edge attributes + edge.attributes = c("date", "message.id", "thread", "weight", "type", "relation") + attribute.defaults = list(get.date.from.string("2020-01-01 00:00:00"), "abc", "def", 1, TYPE.EDGES.INTRA, "mail") + + ## construct network + network = + igraph::make_empty_graph(n = 0, directed = FALSE) + + igraph::vertices("A", "B", "C", type = TYPE.AUTHOR, kind = TYPE.AUTHOR) + + igraph::edges("A", "B", "B", "C", "C", "A") + + ## assign edge attributes + for (i in seq_along(edge.attributes)) { + network = igraph::set_edge_attr(network, edge.attributes[i], value = attribute.defaults[[i]]) + } + + ## convert specified edge attributes to list + if (is.null(remain.as.is)) { + network.listified = convert.edge.attributes.to.list(network) + + ## set 'remain.as.is' to default value in 'convert.edge.attributes.to.list' + ## i.e., intersection of edge attributes and names(EDGE.ATTR.HANDLING) + remain.as.is = names(EDGE.ATTR.HANDLING) + } else { + network.listified = convert.edge.attributes.to.list(network, remain.as.is = remain.as.is) + } + + ## check edge attributes + for (attr in igraph::edge_attr_names(network)) { + conversion.function = ifelse(attr %in% remain.as.is, identity, as.list) + expect_equal( + conversion.function(igraph::edge_attr(network, attr)), + igraph::edge_attr(network.listified, attr), + info = paste("edge attribute", attr, "values") + ) + } + +}, patrick::cases( + "remain.as.is: weight" = list(remain.as.is = c("weight")), + "remain.as.is: date" = list(remain.as.is = c("date")), + "remain.as.is: weight, date" = list(remain.as.is = c("weight", "date")), + "remain.as.is: default" = list(remain.as.is = NULL) +)) + + + ## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / ## Extract data sources ---------------------------------------------------- test_that("Get the data sources from a network with two relations", {