Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
fix #36 tests for check_name internal fxn
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Jul 29, 2020
1 parent b497e67 commit cb2c83a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
7 changes: 0 additions & 7 deletions R/map_leaflet.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ map_leaflet.default <- function(x, lon = 'longitude', lat = 'latitude',
}

# helpers ------------------------------------
dat_cleaner <- function(x, lon = 'longitude', lat = 'latitude', name = NULL) {
x <- guess_latlon(x, lat, lon)
x <- x[stats::complete.cases(x$latitude, x$longitude), ]
x <- check_name(x, name)
return(x)
}

make_map <- function(x, color, size) {
lf <- leaflet::leaflet(data = x)
lf <- leaflet::addTiles(lf)
Expand Down
13 changes: 11 additions & 2 deletions R/zzz.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,19 @@ check_name <- function(x, name = NULL) {
call. = FALSE)
}
if ("name" %in% names(x)) {
message("existing 'name' column found; setting it to 'name_old'")
names(x)[which(names(x) == "name")] <- "name_old"
if (name != "name") {
message("existing 'name' column found; setting it to 'name_old'")
names(x)[which(names(x) == "name")] <- "name_old"
}
}
names(x)[which(names(x) == name)] <- "name"
}
return(x)
}

dat_cleaner <- function(x, lon = 'longitude', lat = 'latitude', name = NULL) {
x <- guess_latlon(x, lat, lon)
x <- x[stats::complete.cases(x$latitude, x$longitude), ]
x <- check_name(x, name)
return(x)
}
Binary file added tests/testthat/spocc_df_1.rda
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("name param works to change taxon name used", {
skip_on_cran()

# prepare data
# library(spocc)
# res <- occ(query = "Lynx rufus californicus", from = "gbif")
# spocc_df_1 <- occ2df(res)
# save(spocc_df_1, file = "tests/testthat/spocc_df_1.rda", version = 2)

load("spocc_df_1.rda")
expect_is(check_name(spocc_df_1), "data.frame")
# column not found
expect_error(check_name(spocc_df_1, "foobar"))
# column found
expect_message((new1 <- check_name(spocc_df_1, "key")))
expect_named(new1,
c("name_old", "longitude", "latitude", "prov", "date", "name"))
# column name given as "name"
## no message given
expect_message((new2 <- check_name(spocc_df_1, "name")), NA)
expect_named(new2,
c("name", "longitude", "latitude", "prov", "date", "key"))
})

0 comments on commit cb2c83a

Please sign in to comment.