# TODO: Add comment
#
# Author: furia
###############################################################################
.setUp <-
function()
{
synapseClient:::.setCache("oldWarn", options("warn")[[1]])
}
.tearDown <-
function()
{
options(warn=synapseClient:::.getCache("oldWarn"))
}
unitTestCachingAddObject <-
function()
{
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
ee <- new("CachingEnhancedEnvironment")
bar <- "foo"
addObject(ee, bar)
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "bar")))
}
unitTestCachingRenameObject <-
function()
{
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkEquals(length(files(ee)), 1L)
checkTrue(grepl("^\\.R_OBJECTS.+\\.rbin$", files(ee)))
## do a simple rename. verify the return value
copy <- renameObject(ee, "foo", "blah")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "blah")))
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(!file.exists(synapseClient:::.generateTmpCacheFileName(ee, "foo")))
checkEquals(length(files(ee)), 1L)
## do a double-rename with potential "stomping"
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
addObject(ee, "boo", "blah")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "blah")))
checkEquals(length(files(ee)), 2L)
copy <- renameObject(ee, "blah", "foo")
checkEquals("CachingEnhancedEnvironment", as.character(class(copy)))
checkEquals(length(files(ee)), 1L)
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(ee, "boo")))
checkEquals(length(files(ee)), 1L)
checkTrue(file.exists(synapseClient:::.generateCacheFileName(copy, "foo")))
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(copy, "boo")))
}
unitTestCachingDeleteObject <-
function()
{
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkEquals(length(files(ee)), 1L)
checkTrue(grepl("^\\.R_OBJECTS.+\\.rbin$", files(ee)))
## make sure that no warnings are produced but converting warnings
## to errors by setting warn=2. reset back to original value in tearDown()
options(warn=2)
deleteObject(ee,"foo")
checkEquals(length(files(ee)), 0L)
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
}
unitTestListFiles <-
function()
{
stop("Not Yet Implemented")
}
unitTestDeleteObjectFileDeleteFails <-
function()
{
stop("Not Yet Implemented")
}
unitTestAddObjectFileCreateFails <-
function()
{
stop("Not Yet Implemented")
}
unitTestCachingObjectCRUDVerifyPersistentMetaData <-
function()
{
stop("Not Yet Implemented")
}
unitTestNoZip <-
function()
{
## need to check behavior when zip is not installed
stop("Not Yet Implemented")
}
unitTestLoadCachedObjects <-
function()
{
## check that no warnings are generated by setting warn = 2
options(warn = 2L)
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
copy <- renameObject(ee, "foo", "boo")
## make sure the files were moved around properly
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(!file.exists(synapseClient:::.generateTmpCacheFileName(ee, "foo")))
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "boo")))
checkEquals(ee$boo, "bar")
checkEquals(length(objects(ee)), 1L)
checkEquals(length(files(ee)), 1L)
checkEquals(files(ee), synapseClient:::.generateCacheFileRelativePath(ee, "boo"))
##load from cache and verify values
copy <- synapseClient:::.loadCachedObjects(ee)
checkEquals(length(ee), 1L)
checkEquals(ee$boo, "bar")
## do something more complicated now
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
addObject(ee, "boo", "blah")
checkEquals(ee$foo, "bar")
checkEquals(ee$blah, "boo")
checkEquals(length(files(ee)), 2L)
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "blah")))
## verify the cached values
renameObject(ee, c("foo", "blah"), c("blah", "foo"))
checkEquals(length(files(ee)), 2L)
checkEquals(length(objects(ee)), 2L)
checkTrue(all(names(ee) == c("blah", "foo")))
checkEquals(ee$foo, "boo")
checkEquals(ee$blah, "bar")
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "blah")))
checkTrue(!file.exists(synapseClient:::.generateTmpCacheFileName(ee, "foo")))
checkTrue(!file.exists(synapseClient:::.generateTmpCacheFileName(ee, "blah")))
##load from cache and verify values
ee <- synapseClient:::.loadCachedObjects(ee)
checkEquals(ee$foo, "boo")
checkEquals(ee$blah, "bar")
## verify the cached values
renameObject(ee, "blah", "foo")
checkEquals(length(files(ee)), 1L)
checkEquals(length(objects), 1L)
checkTrue(file.exists(synapseClient:::.generateCacheFileName(ee, "foo")))
checkTrue(!file.exists(synapseClient:::.generateCacheFileName(ee, "boo")))
ee <- synapseClient:::.loadCachedObjects(ee)
checkEquals(length(objects), 1L)
checkEquals(ee$foo, "bar")
checkException(renameObject(ee, "food", "boom"))
}
unitTestCachingAddListWithFcn <-
function()
{
ee <- new("CachingEnhancedEnvironment")
foo <- list(a="b", one=1)
copy <- addObject(ee, foo)
checkEquals(length(ee), 1L)
checkEquals(names(ee), "foo")
checkEquals(ee$foo$a, "b")
checkEquals(ee$foo$one, 1L)
checkEquals(length(copy), 1L)
checkEquals(names(copy), "foo")
checkEquals(copy$foo$a, "b")
checkEquals(copy$foo$one, 1L)
ee <- new("CachingEnhancedEnvironment")
copy <- addObject(ee, foo, "bar")
checkEquals(length(ee), 1L)
checkEquals(names(ee), "bar")
checkEquals(ee$bar$a, "b")
checkEquals(ee$bar$one, 1L)
checkEquals(length(copy), 1L)
checkEquals(names(copy), "bar")
checkEquals(copy$bar$a, "b")
checkEquals(copy$bar$one, 1L)
}
unitTestRenameCatchException <-
function()
{
## need to test that rename puts things back in the event of failure
stop("not yet implemented")
}
unitTestAddObjectOverwriteCatchException <-
function()
{
## need to test that overwriting add puts things back in the event of failure
stop("not yet implemented")
}
##
##
## Testing that basic EnhancedEnvironment functionality all works for
## CachingEnhancedEnvironment
##
##
unitTestAssignment <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee[['foo']] <- "bar"
checkEquals(names(ee), "foo")
}
unitTestAddObjectAssignment <-
function()
{
ee <- new("CachingEnhancedEnvironment")
addObject(ee, "bar", "foo")
checkEquals(names(ee), "foo")
boo <- "blah"
copy <- addObject(ee, boo)
checkEquals("CachingEnhancedEnvironment", as.character(class(copy)))
checkTrue(all(c('foo','boo') %in% names(ee)))
checkEquals(length(names(ee)), 2L)
checkTrue(all(as.character(class(ee)) == as.character(class(copy))))
checkTrue(all(c('foo','boo') %in% names(copy)))
checkEquals(length(names(copy)), 2L)
}
unitTestNames <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(names(ee), "foo")
}
unitTestNameObjectsStartingWithDot <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(objects(ee), "foo")
## add names starting with dot
addObject(ee, "boo", ".bar")
checkEquals(length(names(ee)), 2L)
checkTrue(all(c(".bar", "foo") %in% names(ee)))
}
unitTestObjects <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(objects(ee), "foo")
}
unitTestListObjectsStartingWithDot <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(objects(ee), "foo")
## add names starting with dot
addObject(ee, "boo", ".bar")
checkEquals(length(objects(ee, all.names=TRUE)), 2L)
checkEquals(length(objects(ee)), 1L)
checkTrue(objects(ee) == "foo")
checkTrue(all(c(".bar", "foo") %in% objects(ee, all.names=TRUE)))
}
unitTestBracketAccessor <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(ee["foo"], list(foo="bar"))
checkEquals(class(ee["foo"]), "list")
checkEquals(names(ee["foo"]), "foo")
checkEquals(names(ee[]), "foo")
checkEquals(ee[][[1]], "bar")
}
unitTestDoubleBracketAccessor <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
## get a single object
checkEquals(ee[["foo"]], "bar")
ee$blah <- "boo"
## get two objects. should be an exception
checkException(ee[[c("foo", "blah")]])
## need to select exactly one element
checkException(ee[[]])
}
unitTestAccessor <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(ee$foo, "bar")
}
unitTestMultipleObjects <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
ee$boo <- "blah"
checkTrue(all(names(ee) == sort(c("foo", "boo"))))
checkTrue(all(ee[1:2] == c("blah", "bar")))
checkTrue(all(names(ee[1:2]) == c("boo", "foo")))
checkTrue(all(ee[c(2,1)] == c("bar", "blah")))
checkTrue(all(names(ee[c(2,1)]) == c("foo", "boo")))
checkTrue(is.null(names(ee[[1]])))
checkEquals(ee[[1]], "blah")
checkEquals(ee[[2]], "bar")
checkTrue(all(names(ee[]) == sort(c("foo", "boo"))))
checkTrue(all(ee[] == c("blah", "bar")))
}
unitTestShow <-
function()
{
ee <- new("CachingEnhancedEnvironment")
checkEquals(capture.output(show(ee)), "character(0)")
ee$foo <- "bar"
checkEquals(capture.output(show(ee)), "[1] foo (character)")
ee$boo <- 1
checkTrue(all(capture.output(show(ee)) == c("[1] boo (numeric)", "[2] foo (character)")))
}
unitTestShowMulipleClasses <-
function()
{
ee <- new("CachingEnhancedEnvironment")
checkEquals(capture.output(show(ee)), "character(0)")
ee$boo <- 1
checkEquals(capture.output(show(ee)), "[1] boo (numeric)")
foo <- "A"
class(foo) <- c("FakeClass1", "FakeClass2")
addObject(ee, foo)
checkTrue(all(capture.output(show(ee)) == c("[1] boo (numeric)", "[2] foo (FakeClass1,FakeClass2)")))
}
unitTestDoubleBracketAccessorNegativeIndices <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$boo <- 1
ee$foo <- "bar"
checkEquals(ee[[-1]], "bar")
checkEquals(ee[[-2]], 1)
checkException(ee[[c(-2, -1)]])
}
unitTestDoubleBracketAccessorNegativeIndicesOutOfBounds <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$boo <- 1
ee$foo <- "bar"
checkEquals(ee[[-2]], 1L)
ee$goo <- "boo"
checkException(ee[[-2]])
checkException(ee[[-0]])
checkException(ee[[]])
}
unitTestBracketAccessorNegativeIndices <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$boo <- 1
ee$foo <- "bar"
checkEquals(ee[-1][[1]], "bar")
checkEquals(names(ee[-1]), "foo")
checkEquals(ee[-2][[1]], 1)
checkEquals(names(ee[-2]), "boo")
checkEquals(length(ee[c(-2, -1)]), 0L)
checkEquals("list", as.character(class(ee[-2:-1])))
}
unitTestBracketAccessorNegativeIndicesOutOfBounds <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$boo <- 1
ee$foo <- "bar"
checkEquals(length(ee[-3]), 2L)
checkEquals("list", as.character(class(ee[-3])))
checkTrue(all(names(ee[-3]) %in% c("foo", "boo")))
checkTrue(all(c("foo", "boo") %in% names(ee[-3])))
checkEquals(length(ee[-0]), 0L)
checkEquals("list", as.character(class(ee[-0])))
}
unitTestBracketAccessorNumericIndicies <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(ee[1], list(foo="bar"))
checkEquals(class(ee[1]), "list")
checkEquals(names(ee[1]), "foo")
## check the values of out-of-bounds numeric indices
checkTrue(is.na(names(ee[2])))
checkTrue(is.null(ee[2][[1]]))
checkEquals("list", as.character(class(ee[2])))
## check the values of out-of-bounds numeric indices
checkTrue(is.na(names(ee['c'])))
checkTrue(is.null(ee['c'][[1]]))
checkEquals("list", as.character(class(ee['c'])))
checkEquals(length(ee['c']), 1L)
## one in bounds, one out
## check the values of out-of-bounds numeric indices
checkTrue(is.na(names(ee[c('foo','c')])[2]))
checkEquals(names(ee[c('foo','c')])[1], "foo")
checkTrue(is.null(ee[c('foo','c')][[2]]))
checkEquals(ee[c('foo','c')][[1]], "bar")
checkTrue(is.null(ee[c('c','foo')][[1]]))
checkEquals("list", as.character(class(ee['c'])))
checkEquals(length(ee[c('foo','c')]), 2L)
}
unitTestDoubleBracketAccessorNumericIndicies <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
checkEquals(ee[[1]], "bar")
checkException(ee[[2]])
}
unitTestDoubleBracketAccessor <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
## get a single object
checkEquals(ee[["foo"]], "bar")
ee$blah <- "boo"
## get two objects. should be an exception
checkException(ee[[c("foo", "blah")]])
}
unitTestDeleteObject <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
ee$boo <- "blah"
copy <- deleteObject(ee, "foo")
checkEquals("CachingEnhancedEnvironment", as.character(class(copy)))
checkEquals(length(ee), 1L)
checkEquals(length(copy), 1L)
checkEquals(ee[[1]], "blah")
## make sure that there was no warning by converting warnings to
## errors by setting warn = 2
options(warn = 2)
deleteObject(ee, "boo")
checkEquals(length(ee), 0L)
## make sure that a warning was actually generated when trying to delete
## an objects that doesn't exist by setting warn = 2 and checking for
## an exception
checkException(deleteObject(ee, "fakeObject"))
}
unitTestDeleteMultipleObjects <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
ee$boo <- "blah"
checkEquals(length(files(ee)), 2L)
## make sure that there was no warning by converting warnings to
## errors by setting warn = 2
options(warn = 2)
deleteObject(ee, c("foo", "boo"))
checkEquals(length(ee), 0L)
}
unitTestGetObject <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
ee$boo <- "blah"
checkEquals("bar", getObject(ee, "foo"))
}
unitTestGetMulipleObjects <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$foo <- "bar"
ee$boo <- "blah"
checkEquals("bar", getObject(ee, "foo"))
checkTrue(all(c("bar", "blah") == getObject(ee, c("foo", "boo"))))
checkTrue(all(c("blah", "bar") == getObject(ee, c("boo", "foo"))))
checkTrue(all(c("bar", "blah", "bar") == getObject(ee, c("foo", "boo", "foo"))))
checkTrue(all(c("bar", "blah", "bar") == getObject(ee, c("foo", "boo", "foo"))))
}
unitTestAddList <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$aList <- list(foo="bar", boo = "blah")
checkEquals(length(ee), 1L)
checkEquals("list", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
checkTrue(all(as.character(getObject(ee, "aList")) == c("bar", "blah")))
ee$aNum <- 1L
checkEquals(length(ee), 2L)
checkEquals("list", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
checkTrue(all(as.character(getObject(ee, "aList")) == c("bar", "blah")))
}
unitTestAddDataFrame <-
function()
{
ee <- new("EnhancedEnvironment")
ee$aList <- data.frame(list(foo="bar", boo = "blah"))
checkEquals(length(ee), 1L)
checkEquals("data.frame", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
ee$aNum <- 1L
checkEquals(length(ee), 2L)
checkEquals("data.frame", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
}
unitTestRenameObject <-
function()
{
## start with a simple rename
ee <- new("CachingEnhancedEnvironment")
ee$aList <- 2L
checkEquals(length(ee), 1L)
checkEquals(2L, getObject(ee, "aList"))
copy <- renameObject(ee, 'aList', 'aList1')
checkEquals("CachingEnhancedEnvironment", as.character(class(copy)))
checkEquals(length(ee), 1L)
checkEquals(1L, length(getObject(ee, "aList1")))
checkEquals(2L, ee[['aList1']])
## now for something more complicated
ee <- new("CachingEnhancedEnvironment")
ee$aList <- 1L
checkEquals(length(ee), 1L)
checkEquals(1L, getObject(ee, "aList"))
ee$aNum <- data.frame(list(foo="bar", boo = "blah"))
copy <- renameObject(ee, 'aNum', 'aList')
checkEquals("CachingEnhancedEnvironment", as.character(class(copy)))
checkEquals(length(ee), 1L)
checkEquals("data.frame", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
checkEquals(length(copy), 1L)
checkEquals("data.frame", class(getObject(copy, "aList")))
checkEquals(2L, length(getObject(copy, "aList")))
checkTrue(all(names(getObject(copy, "aList")) == c("foo", "boo")))
}
unitTestRenameMultipleObjects <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$aNum <- 1L
ee$aList <- list(foo="bar", boo = "blah")
renameObject(ee, c('aNum', 'aList'), c('aList', 'aNum'))
checkEquals(length(ee), 2L)
checkEquals("integer", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aNum")))
checkEquals("list", as.character(class(getObject(ee, "aNum"))))
checkTrue(all(names(getObject(ee, "aNum")) == c("foo", "boo")))
}
unitTestAsEnvironment <-
function()
{
ee <- new("CachingEnhancedEnvironment")
ee$aNum <- 1L
env <- as.environment(ee)
checkEquals("environment", class(env))
checkEquals("aNum", objects(env))
}
unitTestAddListUnlist <-
function()
{
ee <- new("CachingEnhancedEnvironment")
aList <- list(foo="bar", boo = "blah")
copy <- addObject(ee, aList, unlist=TRUE)
checkEquals(length(ee), 2L)
checkEquals("bar", getObject(ee, "foo"))
checkTrue(all(names(ee) == c("boo", "foo")))
checkEquals(length(copy), 2L)
checkEquals("bar", getObject(copy, "foo"))
checkTrue(all(names(copy) == c("boo", "foo")))
ee <- new("CachingEnhancedEnvironment")
aList <- list(foo="bar", boo = "blah")
copy <- addObject(ee, aList)
checkEquals(length(ee), 1L)
checkEquals("list", class(getObject(ee, "aList")))
checkEquals(2L, length(getObject(ee, "aList")))
checkTrue(all(names(getObject(ee, "aList")) == c("foo", "boo")))
checkTrue(all(as.character(getObject(ee, "aList")) == c("bar", "blah")))
checkEquals(length(copy), 1L)
checkEquals("list", class(getObject(copy, "aList")))
checkEquals(2L, length(getObject(copy, "aList")))
checkTrue(all(names(getObject(copy, "aList")) == c("foo", "boo")))
checkTrue(all(as.character(getObject(copy, "aList")) == c("bar", "blah")))
}
unitTestNoZip <-
function()
{
## need to write tests to verify the behavior of the CachingEnhancedEnvironment
## when zip is not installed.
##
## 1) only a single object can be managed when zip is not installed. if a
## the caller attempts to add a second object, an exception should be thrown
## and an informative error message should be provided. The object should
## be identical before and after the failed call
##
## 2) By default, this class sets it's "cachePrefix" member variable to
## ".R_OBJECTS/". When zip is not installed, this prefix should instead
## be set to ".R_OBJECTS_" so that the files will be stored in the root
## of the archive.
##
## 3) The loadObjects method should have an optional flag that repairs
## an archive that was built on a system without zip. The repair involves
## dropping the ".R_OBJECTS_" prefix, and moving the single rbin file
## into the "R_OBJECTS" subdirectory. The cacheDir should be modified
## to reflect the change and the archiveFile should be changed back to
## the default value
##
## 4) If, on a system with zip, the addObject method is called for an
## archive built on a system without zip, the archive should automatically
## be "repaired" and an informative warning message should be printed.
stop("not yet implemented: Bruce?")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.