Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed base location for graph_from_graphdb and added tests #1732

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions R/foreign.R
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ write.graph.dot <- function(graph, file, ...) {
#' Load a graph from the graph database for testing graph isomorphism.
#'
#' This function downloads a graph from a database created for the evaluation
#' of graph isomorphism testing algothitms.
#' of graph isomorphism testing algorithms.
#'
#' `graph_from_graphdb()` reads a graph from the graph database from an FTP or
#' HTTP server or from a local copy. It has two modes of operation:
Expand Down Expand Up @@ -596,12 +596,19 @@ write.graph.dot <- function(graph, file, ...) {
#' @family foreign
#' @export
#' @keywords graphs
graph_from_graphdb <- function(url = NULL,
prefix = "iso", type = "r001", nodes = NULL, pair = "A", which = 0,
base = "http://cneurocvs.rmki.kfki.hu/graphdb/gzip",
compressed = TRUE, directed = TRUE) {
graph_from_graphdb <- function(
url = NULL,
prefix = "iso",
type = "r001",
nodes = NULL,
pair = "A",
which = 0,
base = "https://github.com/igraph/graphsdb/raw/refs/heads/main",
compressed = TRUE,
directed = TRUE
) {
if (is.null(nodes) && is.null(url)) {
stop("The `nodes' or the `url' argument must be non-null")
cli::cli_abort("Either {.arg nodes}' or `{.arg url}' must be non-null.")
}

if (is.null(url)) {
Expand All @@ -622,10 +629,16 @@ graph_from_graphdb <- function(url = NULL,
typegroup <- typegroups[which(types == type)]

if (!prefix %in% prefixes) {
stop("Invalid prefix!")
cli::cli_abort(c(
"{.value {prefix}} is not a valid prefix.",
i = "Must be one of {.value {prefixes}}."
))
}
if (!type %in% types) {
stop("Invalid graph type!")
cli::cli_abort(c(
"{.value {type}} is not a valid graph type.",
i = "Must be one of {.value {types}}."
))
}
suff <- if (compressed) ".gz" else ""
filename <- paste(
Expand All @@ -641,7 +654,7 @@ graph_from_graphdb <- function(url = NULL,

f <- try(gzcon(file(filename, open = "rb")))
if (inherits(f, "try-error")) {
stop(paste("Cannot open URL:", filename))
cli::cli_abort("Cannot open URL provided in {.arg filename}: {.url {filename}}")
}

buffer <- read.graph.toraw(f)
Expand Down
4 changes: 2 additions & 2 deletions man/graph_from_graphdb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/testthat/_snaps/foreign.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,34 @@
+ edges (vertex names):
[1] 0--1 1--2

# graph_from_graphdb works

Code
g <- graph_from_graphdb(nodes = 1000)

---

Code
g <- graph_from_graphdb()
Condition
Error in `graph_from_graphdb()`:
! Either `nodes`' or ``url`' must be non-null.

---

Code
g <- graph_from_graphdb(nodes = 10, prefix = "not_existing")
Condition
Error in `graph_from_graphdb()`:
! not_existing is not a valid prefix.
i Must be one of iso, si6, mcs10, mcs30, mcs50, mcs70, and mcs90.

---

Code
g <- graph_from_graphdb(nodes = 10, type = "not_existing")
Condition
Error in `graph_from_graphdb()`:
! not_existing is not a valid graph type.
i Must be one of r001, r005, r01, r02, m2D, m2Dr2, m2Dr4, m2Dr6, m3D, m3Dr2, m3Dr4, m3Dr6, m4D, m4Dr2, m4Dr4, m4Dr6, b03, b03m, ..., b09, and b09m.

8 changes: 8 additions & 0 deletions tests/testthat/test-foreign.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ test_that("reading graph in LGL format", {
write_graph(g, lgl_path, "lgl")
expect_snapshot(read_graph(lgl_path, "lgl"))
})

test_that("graph_from_graphdb works", {
skip_on_cran()
expect_snapshot(g <- graph_from_graphdb(nodes = 1000))
expect_snapshot(g <- graph_from_graphdb(), error = TRUE)
expect_snapshot(g <- graph_from_graphdb(nodes = 10, prefix = "not_existing"), error = TRUE)
expect_snapshot(g <- graph_from_graphdb(nodes = 10, type = "not_existing"), error = TRUE)
})