Skip to content

Commit

Permalink
Add automated integration tests for all error-free hard-coded models
Browse files Browse the repository at this point in the history
  • Loading branch information
maltelueken committed Sep 20, 2023
1 parent 20f1e22 commit 96af4bb
Show file tree
Hide file tree
Showing 142 changed files with 28,348 additions and 0 deletions.
153 changes: 153 additions & 0 deletions tests/generate-tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Script for generating integration tests for all hard-coded models

getOptionsOneModel <- function() {
options <- jaspTools::analysisOptions("ClassicProcess")
options[["dependent"]] <- "contNormal"
options[["covariates"]] <- list("contGamma", "contcor1", "contcor2", "debCollin1")
options[["factors"]] <- list("facGender", "facExperim")

options[["moderationProbes"]] <- list(
list(
"probePercentile" = 16.0,
"value" = "16"
),
list(
"probePercentile" = 50.0,
"value" = "50"
),
list(
"probePercentile" = 84.0,
"value" = "84"
)
)

options[["emulation"]] = "lavaan"
options[["errorCalculationMethod"]] = "standard"
options[["estimator"]] = "default"
options[["naAction"]] = "fiml"

options[["pathPlotsLegend"]] = TRUE
options[["pathPlotsColor"]] = TRUE
options[["pathPlotsColorPalette"]] = "colorblind"

options[["processModels"]] <- list(
list(
"conceptualPathPlot" = TRUE,
"independentCovariances" = TRUE,
"inputType" = "inputVariables",
"mediationEffects" = TRUE,
"mediatorCovariances" = TRUE,
"modelNumber" = 1,
"modelNumberCovariates" = list(),
"modelNumberIndependent" = "",
"modelNumberMediators" = list(),
"modelNumberModeratorW" = "",
"modelNumberModeratorZ" = "",
"name" = "Model 1",
"pathCoefficients" = TRUE,
"processRelationships" = list(),
"residualCovariances" = TRUE,
"statisticalPathPlot" = TRUE,
"totalEffects" = TRUE,
"localTests" = FALSE,
"localTestType" = "cis",
"localTestBootstrap" = FALSE,
"localTestBootstrapSamples" = 1000
)
)
return(options)
}


replaceVariablesContinuous <- function(v) {
return(switch(v,
"JaspProcess_Dependent_Encoded" = "contNormal",
"JaspProcess_Independent_Encoded" = "contGamma",
"JaspProcess_Mediator_Encoded" = "debCollin1",
"JaspProcess_ModeratorW_Encoded" = "contcor1",
"JaspProcess_ModeratorZ_Encoded" = "contcor2",
v
))
}

replaceVariablesFactors <- function(v) {
return(switch(v,
"JaspProcess_Dependent_Encoded" = "contNormal",
"JaspProcess_Independent_Encoded" = "facGender",
"JaspProcess_Mediator_Encoded" = "debCollin1",
"JaspProcess_ModeratorW_Encoded" = "facExperim",
"JaspProcess_ModeratorZ_Encoded" = "contcor2",
v
))
}


addProcessRelationshipsFromModelNumber <- function(k, options, replaceFun) {

processRelationships <- jaspProcess:::.procGetHardCodedModel(k, 2)
processRelationships <- lapply(processRelationships, function(row) {
row <- lapply(row, replaceFun)
return(row)
})

options$processModels[[1]][["processRelationships"]] <- processRelationships

return(options)
}

polishCapturedCode <- function(out) {
out <- gsub("Now rendering a plot with name: conceptPathPlot", "", out)
out <- gsub("Now rendering a plot with name: statPathPlot", "", out)
out <- gsub("could not find an old plot", "", out)
out <- gsub("Did not store jaspResults", "", out)
out <- gsub("Created Write Seal for jaspResults at: ''", "", out)
out <- gsub(" analysisOptions", " jaspTools::analysisOptions", out)
out <- gsub(" runAnalysis", " jaspTools::runAnalysis", out)
out <- gsub("[\n]{4,}", "", out)
return(out)
}

modelNumbers <- c(
1,2,4,5,7,8,9,10,14,15,16,17,
21,22,28,29,58,59,60,61,62,63,64,65,66,67,
75,76,82,83,84,85,86,87,88,89,90,91,92
)

errorContinuous <- c()
errorFactor <- c()

fileContext <- file("tests/testthat/test-classic-process-all-model-numbers.R")
header <- "# This code is automatically generated by 'generate-tests.R'"
testCode <- "context('Integration tests for variable input across all hard-coded model number settings')"
for (k in jaspProcess:::.procHardCodedModelNumbers()) {
if (k %in% c(6, 80, 81)) next
context <- paste0("\n\ncontext('Model number ", k, " - continuous')")
opts <- getOptionsOneModel()
opts <- addProcessRelationshipsFromModelNumber(k, opts, replaceVariablesContinuous)
out <- try(paste(capture.output(jaspTools::runAnalysis("ClassicProcess", "debug", opts, makeTests = TRUE)), collapse = "\n"))
if (!inherits(out, "try-error")) {
out <- polishCapturedCode(out)
out <- gsub("conceptual-path-plot", paste0("conceptual-path-plot-continuous-", k), out)
out <- gsub("statistical-path-plot", paste0("statistical-path-plot-continuous-", k), out)
testCode <- paste(testCode, context, out, sep = "\n")
} else {
cat("Continuous", k)
errorContinuous <- append(errorContinuous, k)
}
context <- paste0("\n\ncontext('Model number ", k, " - factors')")
opts <- getOptionsOneModel()
opts <- addProcessRelationshipsFromModelNumber(k, opts, replaceVariablesFactors)
out <- try(paste(capture.output(jaspTools::runAnalysis("ClassicProcess", "debug", opts, makeTests = TRUE)), collapse = "\n"))
if (!inherits(out, "try-error")) {
out <- polishCapturedCode(out)
out <- gsub("conceptual-path-plot", paste0("conceptual-path-plot-factor-", k), out)
out <- gsub("statistical-path-plot", paste0("statistical-path-plot-factor-", k), out)
testCode <- paste(testCode, context, out, sep = "\n")
} else {
cat("Factor", k)
errorFactor <- append(errorFactor, k)
}
}
writeLines(paste(header, testCode, sep = "\n\n"), fileContext)
close(fileContext)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 96af4bb

Please sign in to comment.