library(testthat) # most common expectations: # equality: expect_equal() and expect_identical() # regexp: expect_match() # catch-all: expect_true() and expect_false() # console output: expect_output() # messages: expect_message() # warning: expect_warning() # errors: expect_error() escapeString <- function(s) { t <- gsub("(\\\\)", "\\\\\\\\", s) t <- gsub("(\n)", "\\\\n", t) t <- gsub("(\r)", "\\\\r", t) t <- gsub("(\")", "\\\\\"", t) return(t) } prepStr <- function(s) { t <- escapeString(s) u <- eval(parse(text=paste0("\"", t, "\""))) if(s!=u) stop("Unable to escape string!") t <- paste0("\thtml <- \"", t, "\"") utils::writeClipboard(t) return(invisible()) } evaluationMode <- "sequential" processingLibrary <- "dplyr" description <- "test: sequential dplyr" countFunction <- "n()" isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) testScenarios <- function(description="test", releaseEvaluationMode="batch", releaseProcessingLibrary="dplyr", runAllForReleaseVersion=FALSE) { isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) if(isDevelopmentVersion||runAllForReleaseVersion) { evaluationModes <- c("sequential", "batch") processingLibraries <- c("dplyr", "data.table") } else { evaluationModes <- releaseEvaluationMode processingLibraries <- releaseProcessingLibrary } testCount <- length(evaluationModes)*length(processingLibraries) c1 <- character(testCount) c2 <- character(testCount) c3 <- character(testCount) c4 <- character(testCount) testCount <- 0 for(evaluationMode in evaluationModes) for(processingLibrary in processingLibraries) { testCount <- testCount + 1 c1[testCount] <- evaluationMode c2[testCount] <- processingLibrary c3[testCount] <- paste0(description, ": ", evaluationMode, " ", processingLibrary) c4[testCount] <- ifelse(processingLibrary=="data.table", ".N", "n()") } df <- data.frame(evaluationMode=c1, processingLibrary=c2, description=c3, countFunction=c4, stringsAsFactors=FALSE) return(df) } context("PIVOT ROW/COLUMN TESTS") scenarios <- testScenarios("pvt row/col tests: get empty rows") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC", fromData=FALSE, explicitListOfValues=list("Arriva Trains Wales", "CrossCountry", "South West Trains", "Southern", "Virgin Trains")) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # sum(pt$getEmptyRows()) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
South West Trains
Southern
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 238282) expect_equal(sum(pt$getEmptyRows()), 7) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: get empty columns") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory", fromData=FALSE, explicitListOfValues=list("Express Passenger", "Empty Coaching Stock", "Ordinary Passenger")) pt$addRowDataGroups("TOC") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # sum(pt$getEmptyColumns()) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerEmpty Coaching StockOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_equal(sum(pt$getEmptyColumns()), 2) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove row (single level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC", fromData=FALSE, explicitListOfValues=list("Arriva Trains Wales", "CrossCountry", "South West Trains", "Southern", "Virgin Trains")) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeRow(3) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
Southern
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 238282) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove rows (multi-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(noDataGroupNBSP=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$addRowDataGroups("PowerType", onlyCombinationsThatExist=FALSE) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeRows(c(2, 3, 6, 11, 15)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
Total30798303909
CrossCountryDMU221336322196
HST732732
Total228656322928
London MidlandDMU5638559111229
EMU88492820137050
Total144873379248279
Virgin TrainsDMU21372137
EMU64576457
Total85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove empty rows (single-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC", fromData=FALSE, explicitListOfValues=list("Arriva Trains Wales", "CrossCountry", "South West Trains", "Southern", "Virgin Trains")) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeEmptyRows() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 238282) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove empty rows (multi-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(noDataGroupNBSP=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addRowDataGroups("TOC") pt$addRowDataGroups("PowerType", onlyCombinationsThatExist=FALSE) pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeEmptyRows() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
Total30798303909
CrossCountryDMU221336322196
HST732732
Total228656322928
London MidlandDMU5638559111229
EMU88492820137050
Total144873379248279
Virgin TrainsDMU21372137
EMU64576457
Total85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove column (single level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TOC", fromData=FALSE, explicitListOfValues=list("Arriva Trains Wales", "CrossCountry", "South West Trains", "Southern", "Virgin Trains")) pt$addRowDataGroups("TrainCategory") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeColumn(3) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Arriva Trains WalesCrossCountrySouthernVirgin TrainsTotal
Express Passenger307922865859449025
Ordinary Passenger8306334685
Total390922928859483710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 238282) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove columns (multi-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(noDataGroupNBSP=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TOC") pt$addColumnDataGroups("PowerType", onlyCombinationsThatExist=FALSE) pt$addRowDataGroups("TrainCategory") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeColumns(c(2, 3, 6, 11, 15)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Arriva Trains WalesCrossCountryLondon MidlandVirgin TrainsTotal
DMUTotalDMUHSTTotalDMUEMUTotalDMUEMUTotal
Express Passenger307930792213373222865563888491448721376457859449025
Ordinary Passenger83083063635591282013379234685
Total39093909221967322292811229370504827921376457859483710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove empty columns (single-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode) pt$addData(bhmtrains) pt$addColumnDataGroups("TOC", fromData=FALSE, explicitListOfValues=list("Arriva Trains Wales", "CrossCountry", "South West Trains", "Southern", "Virgin Trains")) pt$addRowDataGroups("TrainCategory") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeEmptyColumns() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Arriva Trains WalesCrossCountryVirgin TrainsTotal
Express Passenger307922865859449025
Ordinary Passenger8306334685
Total390922928859483710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 238282) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: remove empty columns (multi-level)") for(i in 1:nrow(scenarios)) { if(!isDevelopmentVersion) break evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(noDataGroupNBSP=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TOC") pt$addColumnDataGroups("PowerType", onlyCombinationsThatExist=FALSE) pt$addRowDataGroups("TrainCategory") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() pt$removeEmptyColumns() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Arriva Trains WalesCrossCountryLondon MidlandVirgin TrainsTotal
DMUTotalDMUHSTTotalDMUEMUTotalDMUEMUTotal
Express Passenger307930792213373222865563888491448721376457859449025
Ordinary Passenger83083063635591282013379234685
Total39093909221967322292811229370504827921376457859483710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } scenarios <- testScenarios("pvt row/col tests: vectorised methods") for(i in 1:nrow(scenarios)) { evaluationMode <- scenarios$evaluationMode[i] processingLibrary <- scenarios$processingLibrary[i] description <- scenarios$description[i] countFunction <- scenarios$countFunction[i] test_that(description, { skip_on_cran() library(pivottabler) pt <- PivotTable$new(processingLibrary=processingLibrary, evaluationMode=evaluationMode, compatibility=list(noDataGroupNBSP=TRUE)) pt$addData(bhmtrains) pt$addColumnDataGroups("TrainCategory") pt$addColumnDataGroups("PowerType") pt$addRowDataGroups("TOC") pt$addRowDataGroups("Status") pt$defineCalculation(calculationName="TotalTrains", summariseExpression=countFunction) pt$evaluatePivot() # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
DMUEMUHSTTotalDMUEMUTotal
Arriva Trains WalesA301830188158153833
C5959151574
R222
Total307930798308303909
CrossCountryA2156170922270606022330
C5462356922571
R26261127
Total2213373222865636322928
London MidlandA55348599141335520273313285146984
C101235336678479141250
R315184232745
Total56388849144875591282013379248279
Virgin TrainsA2028633183598359
C107119226226
R2799
Total2137645785948594
Total3298715306732490256484282013468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 753390) expect_identical(as.character(pt$getHtml()), html) fx <- function(x) { x$caption } gx <- function(x) { captions <- lapply(unlist(x), fx) paste(captions, collapse=" ") } grps <- pt$getColumnGroupsByLevel(c(1, 2)) text <- "Express Passenger Ordinary Passenger Total DMU EMU HST Total DMU EMU Total " expect_equal(length(grps), 2) expect_identical(gx(grps), text) grps <- pt$getColumnGroupsByLevel(c(1, 2), collapse=TRUE) text <- "Express Passenger Ordinary Passenger Total DMU EMU HST Total DMU EMU Total " expect_equal(length(grps), 11) expect_identical(gx(grps), text) levels <- pt$findGroupColumnNumbers(grps, collapse=TRUE) expect_equal(length(levels), 8) expect_equal(sum(levels), 36) grps <- pt$getLeafColumnGroup(c(2, 4, 7)) text <- "EMU Total Total" expect_equal(length(grps), 3) expect_identical(gx(grps), text) levels <- pt$findGroupColumnNumbers(grps) expect_equal(length(levels), 3) expect_equal(sum(unlist(levels)), 13) levels <- pt$findGroupColumnNumbers(grps, collapse=TRUE) expect_equal(length(levels), 3) expect_equal(sum(levels), 13) grps <- pt$getRowGroupsByLevel(c(1, 2)) text <- "Arriva Trains Wales CrossCountry London Midland Virgin Trains Total A C R Total A C R Total A C R Total A C R Total " expect_equal(length(grps), 2) expect_identical(gx(grps), text) grps <- pt$getRowGroupsByLevel(c(1, 2), collapse=TRUE) text <- "Arriva Trains Wales CrossCountry London Midland Virgin Trains Total A C R Total A C R Total A C R Total A C R Total " expect_equal(length(grps), 22) expect_identical(gx(grps), text) levels <- pt$findGroupRowNumbers(grps, collapse=TRUE) expect_equal(length(levels), 17) expect_equal(sum(levels), 153) grps <- pt$getLeafRowGroup(c(2, 4, 7)) text <- "C Total R" expect_equal(length(grps), 3) expect_identical(gx(grps), text) levels <- pt$findGroupRowNumbers(grps) expect_equal(length(levels), 3) expect_equal(sum(unlist(levels)), 13) levels <- pt$findGroupRowNumbers(grps, collapse=TRUE) expect_equal(length(levels), 3) expect_equal(sum(levels), 13) }) }