Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tests for utilities #287

Merged
merged 6 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions R/esqlabs-env.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
.getPackageVersion <- function() {
version <- getNamespaceVersion("esqlabsR")
return(version)
}

# Environment that holds various global variables and settings for the esqlabsR,
# It is not exported and should not be directly manipulated by other packages.
esqlabsEnv <- new.env(parent = emptyenv())
Expand All @@ -11,7 +6,7 @@ esqlabsEnv <- new.env(parent = emptyenv())
esqlabsEnv$packageName <- "esqlabsR"

# Version of the package
esqlabsEnv$packageVersion <- .getPackageVersion()
esqlabsEnv$packageVersion <- getNamespaceVersion("esqlabsR")

# Default width of a plot of a single `PlotMapping`
esqlabsEnv$widthPerPlotMapping <- 8
Expand Down
8 changes: 6 additions & 2 deletions R/utilities-individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ writeIndividualToXLS <- function(individualCharacteristics, outputXLSPath) {
units[i] <- individual$distributedParameters$units[[i]]
}

output <- data.frame(unlist(containerPaths, use.names = FALSE), unlist(paramNames, use.names = FALSE), unlist(as.numeric(values), use.names = FALSE), unlist(units, use.names = FALSE))
output <- data.frame(
unlist(containerPaths, use.names = FALSE),
unlist(paramNames, use.names = FALSE),
unlist(as.numeric(values), use.names = FALSE),
unlist(units, use.names = FALSE)
)
colnames(output) <- columnNames

writexl::write_xlsx(output, path = outputXLSPath, col_names = TRUE)
Expand Down Expand Up @@ -118,7 +123,6 @@ readIndividualCharacteristicsFromXLS <- function(XLSpath,
#' @export
#'
#' @examples
#' #'
#' \dontrun{
#' simulation <- loadSimulation(filePath = modelPath)
#' humanIndividualCharacteristics <- createIndividualCharacteristics(
Expand Down
20 changes: 16 additions & 4 deletions R/utilities-steady-state.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,33 @@ exportSteadyStateToXLS <- function(simulation,
}

speciesInitVals <- data.frame(
unlist(moleculeContainerPath, use.names = FALSE), unlist(moleculeName, use.names = FALSE), unlist(moleculeIsPresent, use.names = FALSE), unlist(moleculeValue, use.names = FALSE),
unlist(moleculeUnits, use.names = FALSE), unlist(moleculeScaleDivisor, use.names = FALSE), unlist(moleculeNegValsAllowed, use.names = FALSE)
unlist(moleculeContainerPath, use.names = FALSE),
unlist(moleculeName, use.names = FALSE),
unlist(moleculeIsPresent, use.names = FALSE),
unlist(moleculeValue, use.names = FALSE),
unlist(moleculeUnits, use.names = FALSE),
unlist(moleculeScaleDivisor, use.names = FALSE),
unlist(moleculeNegValsAllowed, use.names = FALSE)
)

if (length(speciesInitVals) > 0) {
colnames(speciesInitVals) <- c("Container Path", "Molecule Name", "Is Present", "Value", "Units", "Scale Divisor", "Neg. Values Allowed")
}

parameterInitVals <- data.frame(
unlist(parameterContainerPath, use.names = FALSE), unlist(parameterName, use.names = FALSE), unlist(parameterValue, use.names = FALSE), unlist(parameterUnits, use.names = FALSE)
unlist(parameterContainerPath, use.names = FALSE),
unlist(parameterName, use.names = FALSE),
unlist(parameterValue, use.names = FALSE),
unlist(parameterUnits, use.names = FALSE)
)

if (length(parameterInitVals) > 0) {
colnames(parameterInitVals) <- c("Container Path", "Parameter Name", "Value", "Units")
}
# Write the results into an excel file.
writexl::write_xlsx(list("Molecules" = speciesInitVals, "Parameters" = parameterInitVals), path = resultsXLSPath, col_names = TRUE)
writexl::write_xlsx(
list("Molecules" = speciesInitVals, "Parameters" = parameterInitVals),
path = resultsXLSPath,
col_names = TRUE
)
}
1 change: 0 additions & 1 deletion man/applyIndividualParameters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions tests/testthat/_snaps/esqlabsr-settings.md

This file was deleted.

11 changes: 10 additions & 1 deletion tests/testthat/test-esqlabsr-settings.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
test_that("Names for settings are as expected", {
expect_snapshot(esqlabsRSettingNames)
expect_equal(
esqlabsRSettingNames,
list(
packageVersion = "packageVersion",
packageName = "packageName",
widthPerPlotMapping = "widthPerPlotMapping",
heightPerPlotMapping = "heightPerPlotMapping"
)
)
})

test_that("Check that values for package environment bindings are correct", {
Expand All @@ -9,4 +17,5 @@ test_that("Check that values for package environment bindings are correct", {
)

expect_equal(getEsqlabsRSetting("packageName"), "esqlabsR")
expect_type(getEsqlabsRSetting("packageVersion"), "character")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-sensitivity-calculation.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ test_that("sensitivityCalculation errors if file extension is incorrect", {
# checking `SensitivityCalculation` object ------------------

test_that("sensitivityCalculation returns the correct object", {
expect_s3_class(results, "SensitivityCalculation")
expect_true(isOfType(results, "SensitivityCalculation"))

expect_equal(
length(results$simulationResults),
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-utilities-figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ test_that("col2hsv returns expected HSV values for a given R color name", {

test_that("createEsqlabsPlotConfiguration() creates object with chosen defaults", {
myPC <- createEsqlabsPlotConfiguration()
expect_s3_class(myPC, "DefaultPlotConfiguration")
expect_true(isOfType(myPC, "DefaultPlotConfiguration"))
expect_equal(myPC$titleSize, 8)
})

## context("createEsqlabsPlotGridConfiguration")

test_that("createEsqlabsPlotGridConfiguration() creates object with chosen defaults", {
myPGC <- createEsqlabsPlotGridConfiguration()
expect_s3_class(myPGC, "PlotGridConfiguration")
expect_true(isOfType(myPGC, "PlotGridConfiguration"))
expect_equal(myPGC$tagLevels, "a")
})

Expand All @@ -67,6 +67,6 @@ test_that("createEsqlabsPlotGridConfiguration() creates object with chosen defau
test_that("createEsqlabsExportConfiguration() creates object with chosen defaults", {
myProjConfig <- ProjectConfiguration$new()
myEC <- createEsqlabsExportConfiguration(myProjConfig)
expect_s3_class(myEC, "ExportConfiguration")
expect_true(isOfType(myEC, "ExportConfiguration"))
expect_equal(myEC$units, "cm")
})
4 changes: 0 additions & 4 deletions tests/testthat/test-utilities-file.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
## context("sourceAll")

test_that("`sourceAll()` sources all files in the directory", {
skip_if_not_installed("withr")

withr::with_tempdir(
code = {
f1 <- file.create("f1.R")
Expand All @@ -21,8 +19,6 @@ test_that("`sourceAll()` sources all files in the directory", {
## context("pathFromClipboard")

test_that("`pathFromClipboard()` converts paths as expected", {
skip_if_not_installed("clipr")

# This will work only in interactive mode, i.e. with
# `devtools::test_active_file()` or `devtools::test()`, but not during R CMD
# Check on CRAN or AppVeyor where the system clipboard is not available
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-utilities-individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,27 @@ test_that("It create IndividualCharacteristics when numerical values are empty",
expect_equal(individualCharacteristics$population, "European_ICRP_2002")
expect_equal(individualCharacteristics$gender, "MALE")
})

## context("writeIndividualToXLS")

test_that("`writeIndividualToXLS()` writes correct data to a spreadsheet", {
withr::with_tempdir(
code = {
humanIndividualCharacteristics <- createIndividualCharacteristics(
species = Species$Human,
population = HumanPopulation$European_ICRP_2002,
gender = Gender$Male,
weight = 70
)
tmp <- writeIndividualToXLS(humanIndividualCharacteristics, "ParameterSet.xlsx")
df <- readxl::read_xlsx(tmp)

expect_equal(dim(df), c(96L, 4L))
expect_equal(colnames(df), c("Container Path", "Parameter Name", "Value", "Units"))
expect_equal(
unique(df$Units),
c("year(s)", "week(s)", "dm", NA, "kg", "l", "l/min/kg organ", "l/min", "min")
)
}
)
})
30 changes: 30 additions & 0 deletions tests/testthat/test-utilities-population.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## context("sampleRandomValue")

test_that("`sampleRandomValue()` generates needed distribution", {
expect_error(
sampleRandomValue("xyz", 5, 2, 10),
messages$errorDistributionNotSupported("xyz")
)

set.seed(123)
expect_equal(
sampleRandomValue(Distributions$Normal, 5, 2, 10),
c(
3.87904870689558, 4.53964502103344, 8.11741662829825, 5.14101678284915,
5.25857547032189, 8.43012997376656, 5.9218324119784, 2.46987753078693,
3.62629429621295, 4.10867605980008
),
tolerance = 0.001
)

set.seed(123)
expect_equal(
sampleRandomValue(Distributions$LogNormal, 5, 2, 10),
c(
3.74081271106427, 4.24843764475839, 8.46318202896501, 4.77021554349172,
4.87946908411847, 8.98864517081978, 5.54444951200875, 2.85153959957418,
3.56304555191325, 3.90999158989997
),
tolerance = 0.001
)
})
22 changes: 22 additions & 0 deletions tests/testthat/test-utilities-simulation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## context("initializeSimulation")

test_that("`initializeSimulation()` loads a simulation at the minimum", {
simulation <- loadSimulation(system.file("extdata", "simple.pkml", package = "ospsuite"))
initializeSimulation(simulation, steadyStateTime = TRUE)
simulationResults <- runSimulation(simulation)
expect_true(isOfType(simulationResults, "SimulationResults"))
})

## context("compareSimulationParameters")

test_that("`compareSimulationParameters()` produces no differences with identical simulations", {
simPath <- system.file("extdata", "simple.pkml", package = "ospsuite")
sim1 <- loadSimulation(simPath)
sim2 <- loadSimulation(simPath)

res <- compareSimulationParameters(sim1, sim2)
expect_equal(
res,
list(In1NotIn2 = list(), In2NotIn1 = list(), Different = list())
)
})