Skip to content

Commit

Permalink
update tests (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeper committed Jun 16, 2016
1 parent a270319 commit 8e97951
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.0.4 - Thomas J. Leeper

- Added a `unfmatch` element to the return value of `%unf%`.
- Expanded test suite to cover `%unf%` (#17)

2.0.3 - Thomas J. Leeper

- Expanded test suite to cover `as.unfvector()`. (#2)
Expand Down
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
Package: UNF
Version: 2.0.3
Version: 2.0.4
Title: Tools for Creating Universal Numeric Fingerprints for Data
Date: 2016-06-15
Date: 2016-06-16
Authors@R: c(person("Thomas", "Leeper", role = c("aut","cre"),
email = "thosjleeper@gmail.com"),
person("Micah", "Altman", role = c("aut")))
Description: Computes a universal numeric fingerprint (UNF) for an R data object. UNF is a cryptographic hash or signature that can be used to uniquely identify (a version of) a rectangular dataset, or a subset thereof. UNF can be used, in tandem with a DOI, to form a persistent citation to a versioned dataset.
Depends: R (>= 2.0)
Imports: stats, utils, tools, base64enc, digest
Suggests: knitr, testthat
License: GPL-2
Expand Down
4 changes: 3 additions & 1 deletion R/equal.r
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
}

structure(list(identical = ident,
unfmatch = (unfx$unf == unfy$unf),
sorted = sorted,
dim.x = dimx,
dim.y = dimy,
Expand Down Expand Up @@ -110,7 +111,7 @@ print.UNFtest <- function(x, ...){
cat('Objects use different UNF versions\n\n')
printxvars <- printyvars <- FALSE
printxrows <- printyrows <- FALSE
} else if (x$unf.x$unf==x$unf.y$unf) {
} else if (x$unfmatch) {
a <- attributes(x$unf.x)
cat('Objects are a UNF (v',
paste(a$version,':',a$digits,',',a$characters,sep=''),
Expand Down Expand Up @@ -175,4 +176,5 @@ print.UNFtest <- function(x, ...){
print(misy)
}
}
invisible(x)
}
16 changes: 8 additions & 8 deletions R/unf6.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ function(x,
formatted <- paste0('UNF6:', ifelse(header == "",
as.character(short),
paste0(header,':', as.character(short))))
out <- list(unf = as.character(short),
# return UNF-class structure
structure(list(unf = as.character(short),
hash = hash,
unflong = as.character(long),
formatted = formatted)
class(out) <- c('UNF')
attr(out, 'version') <- 6
attr(out, 'digits') <- digits
attr(out, 'characters') <- characters
attr(out, 'truncation') <- truncation
return(out)
formatted = formatted),
class = "UNF",
version = 6,
digits = digits,
characters = characters,
truncation = truncation)
}
4 changes: 4 additions & 0 deletions R/unfvector.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ as.unfvector.zoo <- function(x, ...) {
}

as.unfvector.difftime <- function(x, ...) {
a <- attributes(x)
if (is.null(a[["units"]]) || a[["units"]] != "days") {
warnings("units for difftime assumed to be days")
}
as.unfvector(as.numeric(x), ...)
}

Expand Down
12 changes: 11 additions & 1 deletion tests/testthat/test-unf-version-dispatch.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context("UNF: Version dispatch")

test_that("Version dispatch", {
test_that("Version dispatch (vector)", {
expect_equivalent(attr(unf(1:3, version=3), "version"), 3, label="Version 3")
expect_equivalent(attr(unf(1:3, version=4), "version"), 4, label="Version 4")
expect_equivalent(attr(unf(1:3, version=4.1), "version"), 4.1, label="Version 4.1")
Expand All @@ -9,3 +9,13 @@ test_that("Version dispatch", {
expect_error(attr(unf(1:3, version=2), "version"), label="Unrecognized numeric version")
expect_error(attr(unf(1:3, version="a"), "version"), label="Unrecognized character version")
})

test_that("Version dispatch (data.frame)", {
expect_equivalent(attr(unf(data.frame(a = 1:3), version=3), "version"), 3, label="Version 3")
expect_equivalent(attr(unf(data.frame(a = 1:3), version=4), "version"), 4, label="Version 4")
expect_equivalent(attr(unf(data.frame(a = 1:3), version=4.1), "version"), 4.1, label="Version 4.1")
expect_equivalent(attr(unf(data.frame(a = 1:3), version=5), "version"), 5, label="Version 5")
expect_equivalent(attr(unf(data.frame(a = 1:3), version=6), "version"), 6, label="Version 6")
expect_error(attr(unf(data.frame(a = 1:3), version=2), "version"), label="Unrecognized numeric version")
expect_error(attr(unf(data.frame(a = 1:3), version="a"), "version"), label="Unrecognized character version")
})
5 changes: 5 additions & 0 deletions tests/testthat/test-unf6-character.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
context("UNFv6: Characters")

test_that("Examples from original R package documentation", {
expect_equal(unf6(c('test','1','2','3'))$unf, "fH4NJMYkaAJ16OWMEE+zpQ==")
})
Expand All @@ -24,3 +25,7 @@ test_that("Numerics stored as factors same as numeric stored as character", {
test_that("truncation less than characters throws error", {
expect_error(unf6(c('1','2','3'), characters = 128, truncation=5))
})

test_that("Requesting short truncation fails", {
expect_error(unf6(c('test','1','2','3'), truncation = 1L))
})
22 changes: 19 additions & 3 deletions tests/testthat/test-unf6-datetimes.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
context("UNFv6: Dates")
test_that("Partial dates (year-only) supported", {})
test_that("Partial dates (year-month) supported", {})

test_that("Full dates supported", {
expect_true(as.unfvector(structure(0, class = "Date")) == "1970-01-01")
})

test_that("Partial dates (year-only) supported", {
# note: R does not have a year-only or year/month-only Date class
expect_true(as.unfvector("1970") == "1970", label = "year-only dates")
expect_true(as.unfvector("1970-01") == "1970-01", label = "year-only dates")
})

context("UNFv6: Datetimes")

test_that("Examples from v6 specification",{
expect_equal(unf6("2014-08-22T16:51:05Z"),
unf6(strptime("2014-08-22T16:51:05Z", "%Y-%m-%dT%H:%M:%OSZ", tz="UTC"), timezone="UTC"))
Expand All @@ -18,7 +27,7 @@ test_that("UNFs differ by timezone", {

})

test_that("Correct UNF for UTC timezones", {})
#test_that("Correct UNF for UTC timezones", {})

test_that("Tests of `decimal_seconds` rounding parameter", {
expect_equal(unf6(as.POSIXct(1408726265.12345, origin="1970-01-01"), decimal_seconds=0),
Expand All @@ -32,3 +41,10 @@ test_that("Tests of `decimal_seconds` rounding parameter", {
expect_false(identical(unf6(as.POSIXct(1408726265.12345, origin="1970-01-01"), decimal_seconds=0),
unf6(as.POSIXct(1408726265.12345, origin="1970-01-01"), decimal_seconds=1)))
})

context("UNFv6: difftimes")

test_that("difftimes formatted correctly", {
expect_true(as.unfvector(Sys.Date() - (Sys.Date() + 1)) == "-1.e+")
expect_true(as.unfvector(Sys.Date() - (Sys.Date() - 7.5)) == "+7.5e+")
})
26 changes: 26 additions & 0 deletions tests/testthat/test-unf6-equivalence-operator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
context("UNFv6: UNF equivalence operator")

test_that("Equivalence operator class and printing", {
expect_equal(class(unf(1) %unf% unf(1)), "UNFtest")
expect_equal(class(print(unf(1) %unf% unf(1))), "UNFtest")
})

test_that("Compare two UNFs", {
expect_true((unf(1) %unf% unf(1))$unfmatch)
expect_true(!(unf(1) %unf% unf(2))$unfmatch)
})

test_that("Compare two data.frames", {
expect_true((data.frame(a = 1:3) %unf% data.frame(a = 1:3))$unfmatch)
expect_true(!(data.frame(a = 3:1) %unf% data.frame(a = 1:3))$unfmatch)
})

test_that("Compare data.frames to unf", {
expect_true((unf(data.frame(a = 1:3)) %unf% data.frame(a = 1:3))$unfmatch)
expect_true(!(unf(data.frame(a = 1:3)) %unf% data.frame(a = 3:1))$unfmatch)
})

test_that("Compare data.frames to unf hash", {
expect_true((unf(data.frame(1:3,4:6,7:9), version = 6) %unf% "UNF6:ukDZSJXck7fn4SlPJMPFTQ==")$unfmatch)
expect_true(!(unf(data.frame(1:3,4:6,9:7), version = 6) %unf% "UNF6:ukDZSJXck7fn4SlPJMPFTQ==")$unfmatch)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-unf6-unf-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("Object is 'UNF' class", {
})

test_that("'UNF' class object prints", {
expect_equal(print(unf6(1)), "UNF")
expect_equal(class(print(unf6(1))), "UNF")
})

test_that("Object slots", {
Expand Down

0 comments on commit 8e97951

Please sign in to comment.