Skip to content

Commit

Permalink
cover unit tests as.alienData() - issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveViss committed Feb 17, 2017
1 parent a9f3ca5 commit 0d46869
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
21 changes: 8 additions & 13 deletions R/as.alienData.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ as.alienData <- function(idObs = NULL, interactPair = NULL, coOcc = NULL, coAbun

# Check for duplicates rows
if (nrow(idObs[duplicated(idObs), ]) != 0) {
stop(cat("some idObs entries are duplicated: \n", idObs[duplicated(idObs),
]))
stop("some idObs entries are duplicated")
}

# Cast all columns has factors
Expand Down Expand Up @@ -117,6 +116,8 @@ as.alienData <- function(idObs = NULL, interactPair = NULL, coOcc = NULL, coAbun
## Check if idFrom and idTo are in levels(idSp) or levels(idInd) and not both
## interactPair are observations at species level OR at individual level but not
## both


if (any(levels(interactPair$idFrom) %in% levels(idObs$idSp)) & any(levels(interactPair$idFrom) %in%
levels(idObs$idInd))) {
stop("'idFrom' values belongs to 'idSp' and 'idInd' in 'idObs'. Interaction can't be at the species AND individual levels")
Expand All @@ -131,13 +132,11 @@ as.alienData <- function(idObs = NULL, interactPair = NULL, coOcc = NULL, coAbun
if (any(c(levels(interactPair$idFrom), levels(interactPair$idTo)) %in% levels(idObs$idSp))) {

if (!all(levels(interactPair$idFrom) %in% levels(idObs$idSp))) {
stop(cat("Some species ids in 'idFrom' are not in 'idObs': \n", levels(interactPair$idFrom)[which(!levels(interactPair$idFrom) %in%
levels(idObs$idSp))]))
stop("Some species ids in 'idFrom' are not in 'idObs'")
}

if (!all(levels(interactPair$idTo) %in% levels(idObs$idSp))) {
stop(cat("Some species ids in 'idTo' are not in 'idObs': \n", levels(interactPair$idFrom)[which(!levels(interactPair$idFrom) %in%
levels(idObs$idSp))]))
stop("Some species ids in 'idTo' are not in 'idObs'")
}

}
Expand All @@ -146,23 +145,19 @@ as.alienData <- function(idObs = NULL, interactPair = NULL, coOcc = NULL, coAbun
if (any(c(levels(interactPair$idFrom), levels(interactPair$idTo)) %in% levels(idObs$idInd))) {

if (!all(levels(interactPair$idFrom) %in% levels(idObs$idInd))) {
stop(cat("Some individus ids in 'idFrom' are not in 'idObs': \n",
levels(interactPair$idFrom)[which(!levels(interactPair$idFrom) %in%
levels(idObs$idInd))]))
stop("Some individus ids in 'idFrom' are not in 'idObs'")
}

if (!all(levels(interactPair$idTo) %in% levels(idObs$idInd))) {
stop(cat("Some individus ids in 'idTo' are not in 'idObs': \n", levels(interactPair$idFrom)[which(!levels(interactPair$idFrom) %in%
levels(idObs$idInd))]))
stop("Some individus ids in 'idTo' are not in 'idObs'")
}

}

# Check if rows are not duplicated
if (nrow(interactPair[duplicated(interactPair[, c("idFrom", "idTo")]), ]) !=
0) {
stop(cat("Some 'idFrom' and 'idTo' are duplicated:\n", interactPair[duplicated(interactPair[,
c("idFrom", "idTo")]), ]))
stop("Some 'idFrom' and 'idTo' are duplicated")
}

}
Expand Down
46 changes: 36 additions & 10 deletions tests/testthat/test-alienData.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
context("alienData function")

load('./argsAlienData.RData')
out <- as.alienData(idObs=idObs,interactPair=interactPair,traitSp=traitSp,traitInd=traitInd,verbose=FALSE)

test_that("check data structure", {
expect_is(out, "alienData")
# Even if items from the list are NULL, all items have to be returned
expect_equal(names(out),c("idObs","interactSp","interactInd","coOcc","coAbund","siteEnv","traitSp","traitInd","phylo"))
})
# load('./argsAlienData.RData')
# out <- as.alienData(idObs=idObs,interactPair=interactPair,traitSp=traitSp,traitInd=traitInd,verbose=FALSE)
#
# test_that("check data structure", {
# expect_is(out, "alienData")
# # Even if items from the list are NULL, all items have to be returned
# expect_equal(names(out),c("idObs","interactSp","interactInd","coOcc","coAbund","siteEnv","traitSp","traitInd","phylo"))
# })

test_that("check data integrity", {
# create fake data
idObs <- data.frame(idSite=rep(3,c("")), idTime=NULL)

# Generate fake data for idObs
len <- 3
sites <- sample(paste("site",letters[1:3],sep="_"),len,replace=TRUE) # 3 sites
sp <- paste0("sp",sample(1:len,len,replace=TRUE)) # 3 species
ind <- 1:len # 3 individus
idObs <- data.frame(idSite=sites,idTime=rep(NA,len),idSp=sp, idInd=ind,stringsAsFactors=FALSE)

interactPair <- data.frame(idTo=c("1","sp1"),idFrom=c("1","2"),strength=c(NA,NA),verbose=FALSE,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"'idTo' values belongs to 'idSp' and 'idInd' in 'idObs'. Interaction can't be at the species AND individual levels")

interactPair <- data.frame(idTo=c("1","2"),idFrom=c("sp1","2"),strength=c(NA,NA),verbose=FALSE,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"'idFrom' values belongs to 'idSp' and 'idInd' in 'idObs'. Interaction can't be at the species AND individual levels")

interactPair <- data.frame(idTo="sp4",idFrom="sp1",strength=NA,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"Some species ids in 'idTo' are not in 'idObs'")

interactPair <- data.frame(idTo="4",idFrom="1",strength=NA,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"Some individus ids in 'idTo' are not in 'idObs'")

interactPair <- data.frame(idTo="sp1",idFrom="sp4",strength=NA,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"Some species ids in 'idFrom' are not in 'idObs'")

interactPair <- data.frame(idTo="1",idFrom="4",strength=NA,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"Some individus ids in 'idFrom' are not in 'idObs'")

interactPair <- data.frame(idTo=c("2","2","3"),idFrom=c("2","2","2"),strength=c(NA,NA,NA),verbose=FALSE,stringsAsFactors=FALSE)
expect_error(as.alienData(idObs=idObs, interactPair=interactPair,verbose=FALSE),"Some 'idFrom' and 'idTo' are duplicated")

})

0 comments on commit 0d46869

Please sign in to comment.