From 8760a349b81cb5968609cdc46eb5fba1de83986e Mon Sep 17 00:00:00 2001 From: Nicole White Date: Mon, 5 Oct 2015 01:00:42 -0700 Subject: [PATCH] Properly handle empty collections in cypherToList Fixes #40. --- R/cypherToList.R | 2 +- tests/testthat/test-cypherToList.R | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/R/cypherToList.R b/R/cypherToList.R index 8e73eb7..005618c 100644 --- a/R/cypherToList.R +++ b/R/cypherToList.R @@ -60,7 +60,7 @@ cypherToList.graph = function(graph, query, ...) { for(j in 1:length(result$columns)) { name = result$columns[[j]] record = data[[i]][[j]] - if(!is.null(names(record)) || !is.list(record)) { + if(!is.null(names(record)) || !is.list(record) || length(record) == 0) { datum[[name]] = configure_result(record) } else { depth = length(record) diff --git a/tests/testthat/test-cypherToList.R b/tests/testthat/test-cypherToList.R index 314e3b3..3bf1d01 100644 --- a/tests/testthat/test-cypherToList.R +++ b/tests/testthat/test-cypherToList.R @@ -137,4 +137,33 @@ test_that("it can return nodes with empty collections as properties", { n = n[[1]]$n expect_equal(length(n$a), 0) +}) + +test_that("it can return empty collections", { + clear(neo4j, input=F) + + query = "RETURN [] AS col" + response = cypherToList(neo4j, query)[[1]] + + expect_equal(response$col, list()) +}) + +test_that("it can return empty collections along with a value", { + clear(neo4j, input=F) + + query = "RETURN [] AS col, 5 AS five" + response = cypherToList(neo4j, query)[[1]] + + expect_equal(response$col, list()) + expect_equal(response$five, 5) +}) + +test_that("it can return empty collections and non-empty collections", { + clear(neo4j, input=F) + + query = "RETURN [] AS col1, [5,6] AS col2" + response = cypherToList(neo4j, query)[[1]] + + expect_equal(response$col1, list()) + expect_equal(response$col2, list(5,6)) }) \ No newline at end of file