Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Commit

Permalink
Allow shortestPath to traverse both relationship directions
Browse files Browse the repository at this point in the history
The documentation for the graph algorithms endpoint doesn’t mention
that you can do direction=all, but here we are.
  • Loading branch information
nicolewhite committed Oct 10, 2015
1 parent 8760a34 commit b26aa97
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion R/allShortestPaths.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param fromNode A node object.
#' @param relType A character string. The relationship type to traverse.
#' @param toNode A node object.
#' @param direction A character string. The relationship direction to traverse. Should be "in" or "out".
#' @param direction A character string. The relationship direction to traverse; this can be either "in", "out", or "all".
#' @param max_depth An integer. The maximum depth of the path.
#' @param cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ cypher_endpoint = function(graph, query, params) {
shortest_path_algo = function(all, algo, fromNode, relType, toNode, direction = "out", max_depth = 1, cost_property=character()) {
stopifnot(is.character(relType),
"node" %in% class(toNode),
direction %in% c("in", "out"),
direction %in% c("in", "out", "all"),
is.numeric(max_depth),
is.character(cost_property))

Expand Down
2 changes: 1 addition & 1 deletion R/shortestPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param fromNode A node object.
#' @param relType A character string. The relationship type to traverse.
#' @param toNode A node object.
#' @param direction A character string. The relationship direction to traverse. Should be "in" or "out".
#' @param direction A character string. The relationship direction to traverse; this can be either "in", "out", or "all".
#' @param max_depth An integer. The maximum depth of the path.
#' @param cost_property A character string. If retrieving a weighted shortest path, the name of the relationship property that contains the weights.
#'
Expand Down
2 changes: 1 addition & 1 deletion man/allShortestPaths.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allShortestPaths(fromNode, relType, toNode, direction = "out",

\item{toNode}{A node object.}

\item{direction}{A character string. The relationship direction to traverse. Should be "in" or "out".}
\item{direction}{A character string. The relationship direction to traverse; this can be either "in", "out", or "all".}

\item{max_depth}{An integer. The maximum depth of the path.}

Expand Down
2 changes: 1 addition & 1 deletion man/shortestPath.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ shortestPath(fromNode, relType, toNode, direction = "out", max_depth = 1,

\item{toNode}{A node object.}

\item{direction}{A character string. The relationship direction to traverse. Should be "in" or "out".}
\item{direction}{A character string. The relationship direction to traverse; this can be either "in", "out", or "all".}

\item{max_depth}{An integer. The maximum depth of the path.}

Expand Down
26 changes: 23 additions & 3 deletions tests/testthat/test-paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ test_that("shortestPath works with direction=in", {
expect_equal(actual_names, expected_names)
})

test_that("shortestPath works with direction=all", {
p = shortestPath(charles, "WORKS_WITH", elaine, direction = "all", max_depth = 4)
expect_is(p, "path")
expect_equal(p$length, 2)

n = nodes(p)
actual_names = sapply(n, '[[', 'name')
expected_names = c("Charles", "David", "Elaine")
expect_equal(actual_names, expected_names)
})

test_that("allShortestPaths returns null when not found", {
p = allShortestPaths(alice, "WORKS_WITH", david)
expect_null(p)
Expand All @@ -57,18 +68,27 @@ test_that("allShortestPaths returns null when not found", {
expect_null(p)
})

test_that("allShortestPaths works", {
p = allShortestPaths(alice, "WORKS_WITH", david, max_depth = 4)
test_that("allShortestPaths works with direction=out", {
p = allShortestPaths(alice, "WORKS_WITH", david, direction = "out", max_depth = 4)
expect_is(p, "list")
expect_is(p[[1]], "path")
expect_equal(length(p), 2)

})

test_that("allShortestPaths works with direction=in", {
p = allShortestPaths(david, "WORKS_WITH", alice, direction = "in", max_depth = 4)
expect_is(p, "list")
expect_is(p[[1]], "path")
expect_equal(length(p), 2)
})

test_that("allShortestPaths works with direction=all", {
p = allShortestPaths(bob, "WORKS_WITH", elaine, direction = "all", max_depth = 4)
expect_is(p, "list")
expect_is(p[[1]], "path")
expect_equal(length(p), 2)
})

test_that("shortestPath works with cost property", {
p = shortestPath(alice, "WORKS_WITH", david, cost_property="weight")

Expand Down

0 comments on commit b26aa97

Please sign in to comment.