Skip to content

Commit

Permalink
Update tests for testthat 3e compatibility (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
gothub committed Sep 30, 2020
1 parent 5581468 commit a4db2cb
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 108 deletions.
7 changes: 3 additions & 4 deletions inst/tests/test.ResourceMap.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("ResourceMap")
test_that("datapack library loads", {
expect_true(library(datapack, logical.return=TRUE))
})
Expand Down Expand Up @@ -48,20 +47,20 @@ test_that("ResourceMap creation from DataPackage triples", {

relations <- getRelationships(dp)
# Test if the data frame with retrieved relationships was constructed correctly
expect_that(nrow(relations), equals(7))
expect_equal(nrow(relations), 7)
expect_match(relations[relations$subject == mdId, 'predicate'], "documents")

# Now initialize a ResourceMap with the relationships.
resMapId <- sprintf("resourceMap:%s", UUIDgenerate())
resMap <- new("ResourceMap", id=resMapId)
expect_that(class(resMap)[[1]], equals("ResourceMap"))
expect_equal(class(resMap)[[1]], "ResourceMap")
resMap <- createFromTriples(resMap, relations, getIdentifiers(dp))

# Serialize the ResourceMap to a file.
filePath <- sprintf("%s/%s.rdf", tempdir(), resMapId)
status <- serializeRDF(resMap, filePath)
found <- grep("wasDerivedFrom", readLines(filePath))
expect_that(length(found), is_more_than(0))
expect_gt(length(found), 0)

freeResourceMap(resMap)
rm(resMap)
Expand Down
35 changes: 17 additions & 18 deletions inst/tests/test.SystemMetadata.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
context("SystemMetadata")
test_that("datapack library loads", {
expect_true(library(datapack, logical.return = TRUE))
})
test_that("SystemMetadata constructors", {
library(datapack)
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
expect_true(is.na(sysmeta@identifier))
sysmeta <- new("SystemMetadata", identifier="TestId", formatId="text/csv")
expect_that(sysmeta@identifier, equals("TestId"))
expect_that(sysmeta@formatId, equals("text/csv"))
expect_equal(sysmeta@identifier, "TestId")
expect_equal(sysmeta@formatId, "text/csv")
})
test_that("XML SystemMetadata parsing works", {
library(datapack)
library(XML)
testid <- "doi:10.xxyy/AA/tesdoc123456789"
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
doc <- xmlParseDoc("../testfiles/sysmeta.xml", asText=FALSE)
expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
xml <- xmlRoot(doc)
#getEncoding(doc)
sysmeta <- parseSystemMetadata(sysmeta, xmlRoot(xml))
expect_match(sysmeta@identifier, testid)
expect_that(nrow(sysmeta@accessPolicy), equals(5))
expect_equal(nrow(sysmeta@accessPolicy), 5)
expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
expect_true(sysmeta@archived)
csattrs <- xmlAttrs(xml[["checksum"]])
Expand All @@ -39,13 +38,13 @@ test_that("XML SystemMetadata parsing works", {
# Parse v2.0 system metadata
testid <- "0007f892-0d8f-4451-94e9-94d02ba5dd0d_0"
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
doc <- xmlParseDoc("../testfiles/sysmeta-v2.xml", asText=FALSE)
expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
xml <- xmlRoot(doc)
sysmeta <- parseSystemMetadata(sysmeta, xmlRoot(xml))
expect_match(sysmeta@identifier, testid)
expect_that(nrow(sysmeta@accessPolicy), equals(1))
expect_equal(nrow(sysmeta@accessPolicy), 1)
expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
expect_false(sysmeta@archived)
csattrs <- xmlAttrs(xml[["checksum"]])
Expand All @@ -57,7 +56,7 @@ test_that("XML SystemMetadata parsing works", {
# Parse v2.0 system metadata, checking parsing of replication policy
testid <- "0007f892-0d8f-4451-94e9-94d02ba5dd0d_0"
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
doc <- xmlParseDoc("../testfiles/sysmeta-v2-repfalse.xml", asText=FALSE)
expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
xml <- xmlRoot(doc)
Expand All @@ -67,7 +66,7 @@ test_that("XML SystemMetadata parsing works", {
# Parse v2.0 system metadata, checking parsing when missing numReplicas
testid <- "arctic-data.9794.1"
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
doc <- xmlParseDoc("../testfiles/sysmeta-v2-repfalse-zero-reps.xml", asText=FALSE)
expect_match(xmlValue(xmlRoot(doc)[["identifier"]]), testid)
xml <- xmlRoot(doc)
Expand All @@ -80,7 +79,7 @@ test_that("XML SystemMetadata serialization works", {
library(XML)
testid <- "doi:10.xxyy/AA/tesdoc123456789"
sysmeta <- new("SystemMetadata")
expect_that(sysmeta@serialVersion, equals(1))
expect_equal(sysmeta@serialVersion, 1)
xml <- xmlParseDoc("../testfiles/sysmeta.xml", asText=FALSE)
expect_match(xmlValue(xmlRoot(xml)[["identifier"]]), testid)
sysmeta <- parseSystemMetadata(sysmeta, xmlRoot(xml))
Expand Down Expand Up @@ -124,7 +123,7 @@ test_that("SystemMetadata XML constructor works", {
xml <- xmlRoot(doc)
sysmeta <- SystemMetadata(xmlRoot(xml))
expect_match(sysmeta@identifier, testid)
expect_that(nrow(sysmeta@accessPolicy), equals(5))
expect_equal(nrow(sysmeta@accessPolicy), 5)
expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")
expect_true(sysmeta@archived)
csattrs <- xmlAttrs(xml[["checksum"]])
Expand All @@ -142,22 +141,22 @@ test_that("SystemMetadata validation works", {
expect_true(isValid)
sysmeta <- new("SystemMetadata", identifier="foo", checksum="jdhdjhfd", rightsHolder="ff")
errors <- validate(sysmeta)
expect_that(length(errors), equals(2))
expect_equal(length(errors), 2)
})

test_that("SystemMetadata accessPolicy can be constructed and changed", {
apolicy=data.frame(list("public", "read"))
colnames(apolicy) <- c("subject", "permission")
sysmeta <- new("SystemMetadata", identifier="foo", formatId="text/csv", size=59, checksum="jdhdjhfd", rightsHolder="ff", accessPolicy=apolicy)
expect_that(sysmeta@serialVersion, equals(1))
expect_that(nrow(sysmeta@accessPolicy), equals(1))
expect_equal(sysmeta@serialVersion, 1)
expect_equal(nrow(sysmeta@accessPolicy), 1)
expect_match(as.character(sysmeta@accessPolicy$permission[[1]]), "read")

sysmeta <- addAccessRule(sysmeta, "foo", "write")
expect_that(nrow(sysmeta@accessPolicy), equals(2))
expect_equal(nrow(sysmeta@accessPolicy), 2)
# Try to add same rule again and make sure it didn't get duplicated
sysmeta <- addAccessRule(sysmeta, "foo", "write")
expect_that(nrow(sysmeta@accessPolicy), equals(2))
expect_equal(nrow(sysmeta@accessPolicy), 2)
expect_match(as.character(sysmeta@accessPolicy$permission[[2]]), "write")
expect_true(hasAccessRule(sysmeta, "foo", "write"))
apolicy=data.frame(subject=c("bar", "baz"), permission= c("changePermission", "write"))
Expand All @@ -167,7 +166,7 @@ test_that("SystemMetadata accessPolicy can be constructed and changed", {
expect_true(hasAccessRule(sysmeta, "bar", "changePermission"))
expect_true(hasAccessRule(sysmeta, "baz", "write"))
expect_true(!hasAccessRule(sysmeta, "baz", "changePermission"))
expect_that(nrow(sysmeta@accessPolicy), equals(4))
expect_equal(nrow(sysmeta@accessPolicy), 4)
expect_match(as.character(sysmeta@accessPolicy$permission[[4]]), "write")
expect_match(as.character(sysmeta@accessPolicy$subject[[4]]), "baz")
})
Expand Down
54 changes: 26 additions & 28 deletions inst/tests/test_DataObject.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("DataObject")

test_that("datapack library loads", {
expect_true(library(datapack, logical.return = TRUE))
})
Expand All @@ -15,27 +13,27 @@ test_that("DataObject constructors work", {

# Test the constructor that builds a DataObject object
do <- new("DataObject", identifier, data, filename="test.cvs", format=format, user=user, mnNodeId=node)
expect_that(class(do)[[1]], equals("DataObject"))
expect_that(do@sysmeta@serialVersion, equals(1))
expect_that(do@sysmeta@identifier, equals(identifier))
expect_that(do@sysmeta@submitter, equals(user))
expect_that(do@sysmeta@size, equals(length(data)))
expect_that(length(do@data), equals(length(data)))
expect_that(getIdentifier(do), equals(identifier))
expect_that(getFormatId(do), equals(format))
expect_equal(class(do)[[1]], "DataObject")
expect_equal(do@sysmeta@serialVersion, 1)
expect_equal(do@sysmeta@identifier, identifier)
expect_equal(do@sysmeta@submitter, user)
expect_equal(do@sysmeta@size, length(data))
expect_equal(length(do@data), length(data))
expect_equal(getIdentifier(do), identifier)
expect_equal(getFormatId(do), format)
sha1 <- digest(data, algo="sha1", serialize=FALSE, file=FALSE)
sm <- new("SystemMetadata", identifier=identifier, formatId=format, size=length(data), submitter=user, rightsHolder=user, checksum=sha1, originMemberNode=node, authoritativeMemberNode=node)
expect_that(sm@identifier, equals(identifier))
expect_equal(sm@identifier, identifier)

# Now test the constructor that passes in SystemMetadata and data
do <- new("DataObject", sm, data, filename="test.csv")
expect_that(do@sysmeta@serialVersion, equals(1))
expect_that(do@sysmeta@identifier, equals(identifier))
expect_that(do@sysmeta@submitter, equals(user))
expect_that(do@sysmeta@size, equals(length(data)))
expect_that(length(do@data), equals(length(data)))
expect_that(getIdentifier(do), equals(identifier))
expect_that(getFormatId(do), equals(format))
expect_equal(do@sysmeta@serialVersion, 1)
expect_equal(do@sysmeta@identifier, identifier)
expect_equal(do@sysmeta@submitter, user)
expect_equal(do@sysmeta@size, length(data))
expect_equal(length(do@data), length(data))
expect_equal(getIdentifier(do), identifier)
expect_equal(getFormatId(do), format)

# Now test the constructor that passes in SystemMetadata and a filename (not data)
tf <- tempfile()
Expand All @@ -44,13 +42,13 @@ test_that("DataObject constructors work", {
close(con)
sha1_file <- digest(tf, algo="sha1", serialize=FALSE, file=TRUE)
do <- new("DataObject", sm, filename=tf)
expect_that(do@sysmeta@serialVersion, equals(1))
expect_that(do@sysmeta@identifier, equals(identifier))
expect_that(do@sysmeta@submitter, equals(user))
expect_that(do@sysmeta@size, equals(length(data)))
expect_that(file.info(tf)$size, equals(length(data)))
expect_that(getIdentifier(do), equals(identifier))
expect_that(getFormatId(do), equals(format))
expect_equal(do@sysmeta@serialVersion, 1)
expect_equal(do@sysmeta@identifier, identifier)
expect_equal(do@sysmeta@submitter, user)
expect_equal(do@sysmeta@size, length(data))
expect_equal(file.info(tf)$size, length(data))
expect_equal(getIdentifier(do), identifier)
expect_equal(getFormatId(do), format)
data2 <- getData(do)
sha1_get_data <- digest(data2, algo="sha1", serialize=FALSE, file=FALSE)
expect_match(sha1_get_data, sha1)
Expand All @@ -59,11 +57,11 @@ test_that("DataObject constructors work", {
# Test that the constructor works for a data path, with a few different forms
targetPath="./data/rasters/test.csv"
do <- new("DataObject", sm, data, filename="test.csv", targetPath=targetPath)
expect_that(do@targetPath, equals(targetPath))
expect_equal(do@targetPath, targetPath)

targetPath="data/rasters/test.csv"
do <- new("DataObject", sm, data, filename="test.csv", targetPath=targetPath)
expect_that(do@targetPath, equals(targetPath))
expect_equal(do@targetPath, targetPath)
})
test_that("DataObject accessPolicy methods", {
library(datapack)
Expand All @@ -75,7 +73,7 @@ test_that("DataObject accessPolicy methods", {
node <- "urn:node:KNB"

do <- new("DataObject", identifier, data, format, user, node, filename="test.csv")
expect_that(class(do)[[1]], equals("DataObject"))
expect_equal(class(do)[[1]], "DataObject")

# Public access should not be present at first
canRead <- canRead(do, "uid=anybody,DC=somedomain,DC=org")
Expand Down
Loading

0 comments on commit a4db2cb

Please sign in to comment.