From 8e97951905a55eaf5a22470bf7a7d0a53f262749 Mon Sep 17 00:00:00 2001 From: "Thomas J. Leeper" Date: Thu, 16 Jun 2016 10:47:39 +0100 Subject: [PATCH] update tests (#17) --- CHANGELOG | 5 ++++ DESCRIPTION | 5 ++-- R/equal.r | 4 ++- R/unf6.R | 16 ++++++------ R/unfvector.R | 4 +++ tests/testthat/test-unf-version-dispatch.R | 12 ++++++++- tests/testthat/test-unf6-character.R | 5 ++++ tests/testthat/test-unf6-datetimes.R | 22 +++++++++++++--- .../testthat/test-unf6-equivalence-operator.R | 26 +++++++++++++++++++ tests/testthat/test-unf6-unf-class.R | 2 +- 10 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 tests/testthat/test-unf6-equivalence-operator.R diff --git a/CHANGELOG b/CHANGELOG index a5d776f..9b3bc84 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/DESCRIPTION b/DESCRIPTION index 7bb4b04..752e326 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/equal.r b/R/equal.r index 5176851..e0204ad 100644 --- a/R/equal.r +++ b/R/equal.r @@ -79,6 +79,7 @@ } structure(list(identical = ident, + unfmatch = (unfx$unf == unfy$unf), sorted = sorted, dim.x = dimx, dim.y = dimy, @@ -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=''), @@ -175,4 +176,5 @@ print.UNFtest <- function(x, ...){ print(misy) } } + invisible(x) } diff --git a/R/unf6.R b/R/unf6.R index a96665d..258a1dd 100644 --- a/R/unf6.R +++ b/R/unf6.R @@ -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) } diff --git a/R/unfvector.R b/R/unfvector.R index 9bf6658..9602e30 100644 --- a/R/unfvector.R +++ b/R/unfvector.R @@ -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), ...) } diff --git a/tests/testthat/test-unf-version-dispatch.R b/tests/testthat/test-unf-version-dispatch.R index 055929f..5457b7e 100644 --- a/tests/testthat/test-unf-version-dispatch.R +++ b/tests/testthat/test-unf-version-dispatch.R @@ -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") @@ -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") +}) diff --git a/tests/testthat/test-unf6-character.R b/tests/testthat/test-unf6-character.R index a8320f4..f62671c 100644 --- a/tests/testthat/test-unf6-character.R +++ b/tests/testthat/test-unf6-character.R @@ -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==") }) @@ -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)) +}) diff --git a/tests/testthat/test-unf6-datetimes.R b/tests/testthat/test-unf6-datetimes.R index f3df889..d7a7e4f 100644 --- a/tests/testthat/test-unf6-datetimes.R +++ b/tests/testthat/test-unf6-datetimes.R @@ -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")) @@ -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), @@ -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+") +}) diff --git a/tests/testthat/test-unf6-equivalence-operator.R b/tests/testthat/test-unf6-equivalence-operator.R new file mode 100644 index 0000000..0da141d --- /dev/null +++ b/tests/testthat/test-unf6-equivalence-operator.R @@ -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) +}) diff --git a/tests/testthat/test-unf6-unf-class.R b/tests/testthat/test-unf6-unf-class.R index db654b5..e7cce02 100644 --- a/tests/testthat/test-unf6-unf-class.R +++ b/tests/testthat/test-unf6-unf-class.R @@ -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", {