Skip to content

Commit

Permalink
Added unit tests for the instructions (chkVar returns FALSE)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjentsch committed Jan 8, 2024
1 parent 0b40713 commit c352e9e
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tests/testthat/test-jtarrangecols.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ testthat::test_that("jtarrangecols works", {
expect_equal(chkRes$pvwDta$rowCount, 10)
expect_equal(chkRes$pvwDta$rowSelected, 0)
expect_equal(chkRes$pvwDta$width, 76)


# check instructions when chkVar fails (varOrd is empty)
chkRes <- jTransform::jtArrangeCols(data = dtaInp, varAll = names(dtaInp), blnAll = TRUE)
expect_equal(names(chkRes), c("genInf", "pvwDta"))
expect_equal(chkRes$genInf$asString(), paste("\n Please assign the variables in their desired order to \"Desired Order\n",
"of Variables\". By ticking \"Add Remaining Variables at the End\",\n",
"variables that are not contained in \"Desired order of variables\") are\n",
"appended.\n"))

# ensure that an error is thrown if no data are submitted
expect_error(jTransform::jtArrangeCols(varOrd = names(dtaInp)[c(1, 17:21, 12:16, 22:26, 2:11, 27:28)], varAll = names(dtaInp), blnAll = FALSE),
regexp = paste("Argument 'varAll' contains 'ID', 'A1', 'A2', 'A3', 'A4', 'A5', 'C1', 'C2', 'C3', 'C4', 'C5', 'E1', 'E2', 'E3', 'E4', 'E5', 'N1', 'N2',",
"'N3', 'N4', 'N5', 'O1', 'O2', 'O3', 'O4', 'O5', 'gender', 'age' which are not present in the dataset"))
Expand Down
85 changes: 85 additions & 0 deletions tests/testthat/test-jtlong2wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,91 @@ testthat::test_that("jtlong2wide works", {
expect_equal(chkRes$pvwDta$rowSelected, 0)
expect_equal(chkRes$pvwDta$width, 185)

# check instructions when chkVar fails (varID is empty)
chkRes <- jTransform::jtLong2Wide(data = dtaInp, varTme = c("cond", "colour"), varTgt = c("rspCrr", "rspTme"), varExc = "sex", varAgg = "first", varSep = "_")
expect_equal(names(chkRes), c("genInf", "pvwDta", "pvwLvl", "addInf"))
expect_equal(chkRes$genInf$asString(), paste("\n Please assign the variables that identify participant (or another\n",
"measurement unit; e.g., a number or an ID) to \"Variables that identify\n",
"the same unit\", and those that are unique to an unit but not an\n",
"identifier (e.g., gender, age group) to \"Variables NOT to be\n",
"Transformed\". \"Variables That Differentiate Within a Unit\" typically\n",
"contain different (e.g., experimental) conditions, and \"Variables To\n",
"Be Transformed\" are the actual measurements (e.g., responses, reaction\n",
"times, etc.).\n\n For an example about a typical long-to-wide-transformation, see the\n",
"last paragraph in \"Details\" underneath the output tables.\n"))
expect_equal(chkRes$addInf$asString(), paste("\n Details\n\n \"Variables that identify the same unit\" is an ID variable (e.g., a\n",
"participant code). This code needs to be unique (i.e., there can't be\n",
"two participants, or other units, with the same ID).\n\n",
"\"Variables that differentiate within an unit\" are the so-called\n",
"time-varying variables, i.e., variables that are originally are\n",
"arranged in rows and whose steps shall be transformed into columns (a\n",
"new column created for the each combination of a target variable,\n",
"e.g., reaction time [RT], with each experimental condition [cond…]\n",
"leading RT_cond1, RT_cond2, …). There can be several time-varying\n",
"variables (e.g., several facors in an experiment, each represented by\n",
"a different column in the input data set).\n\n",
"\"Variables to be transformed\" are the so-called target variables,\n",
"i.e., variables that exist as one column in the input data set and are\n",
"going to be transformed / combined with each step of a time-varying\n",
"variable resulting in as many columns as there are steps (e.g., would\n",
"reaction time be one column in the input data set, resulting in as\n",
"many columns as there are different combinations of time-varying\n",
"variables in the output data set; if there are 3 steps on the first\n",
"time-varying variable and 4 steps on the second, this would result in\n",
"12 columns in the output data set).\n\n",
"\"Variables NOT to be transformed\" are variables that \"characterize\" a\n",
"participant (or another unit), often also called between-subjects\n",
"variables, e.g., age or sex. However, they are not unique (and thus no\n",
"ID variables; there may be several participant with the same age or\n",
"sex).\n\n",
"\"Variable organization in the output\" determines how the variables are\n",
"to be arranged in the output data set. This is only relevant if there\n",
"is more than one target variable (e.g., if you want to transform both\n",
"correct responses and reaction times). \"Variables are adjacent\" keeps\n",
"the target variables in adjacent columns (for the example above,\n",
"correct responses and reaction time would be adjacent, and on the\n",
"higher levels, the steps of the time-varying variable / conditions\n",
"would vary). \"Time steps are adjacent\" would produce a block where all\n",
"steps of the time-varying variable / conditions for the first target\n",
"variable are adjacent, followed by a block of all conditions of the\n",
"second variable and so on.\n\n",
"\"How to accumulate?\" determines how different rows may be accumulated.\n",
"The setting does not apply if there is only one occurence / row for\n",
"each possible combination of conditions. Let's assume that you ran an\n",
"experiment with two factors: the experimental condition and which\n",
"repetition of that condition an entry represents. For evaluation, you\n",
"want to drop the repetition (you thus only assign the variable\n",
"representing the condition to \"Variables that differentiate within an\n",
"unit\". The setting determines if the mean of these occurrences shall\n",
"be calculated (\"Calculate mean\") or if all occurences after the first\n",
"should be dropped (\"First occurence\").\n\n",
"\"Separator\" defines which character(s) should be placed between the\n",
"target variable and the steps of the time-varying variable /\n",
"conditions when assembling the variable names (e.g., VAR_COND).\n\n",
"The principle of the transformation from long to wide can perhaps\n",
"easiest be understood by looking at example4jtLong2Wide from the Data\n",
"Library of this module. It contains results from a Stroop experiment\n",
"(in long format) with seven variables: ID (identifies the\n",
"participant), sex (of the participant), cond (of the Stroop\n",
"experiment; congruent, neutral, and incongruent), colour (in which the\n",
"target word was written), rep (which repetition of a particular\n",
"cond-colour-combination this entry is), rspCrr (whether the\n",
"participant named the correct colour), and rspTme (the reaction time\n",
"for that response). These variables have to be assigned to the\n",
"following fields: ID to \"Variables that identify the same unit\" (it is\n",
"an unique identifier of each participant); cond, colour and rep to\n",
"\"Variables that differentiate within an unit\" (representing all\n",
"possible combinations of experimental conditions); rspCrr and rspTme\n",
"to \"Variables to be transformed\" (these measurements are repeated for\n",
"each combination of conditions, and in a result we want a column each\n",
"for all possible combinations of those variables and the experimental\n",
"conditions, i.e., within-subject variables); and finally sex to\n",
"\"Variables NOT to be transformed\" (since sex is a between-subjects\n",
"variable that doesn't change between experimental condition, we thus\n",
"only need one column in the output; however, it is not unique and thus\n",
"not suited as ID variable).\n"))

# ensure that an error is thrown if no data are submitted
expect_error(jTransform::jtLong2Wide(varID = "ID", varTme = c("cond", "colour", "rep"), varTgt = c("rspCrr", "rspTme"), varExc = "sex", varSep = "_"),
regexp = "Argument 'varID' contains 'ID' which is not present in the dataset")
})
33 changes: 33 additions & 0 deletions tests/testthat/test-jtmergecols.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ testthat::test_that("jtmergecols works", {
expect_equal(chkRes$pvwDta$rowSelected, 0)
expect_equal(chkRes$pvwDta$width, 80)

# check instructions when chkVar fails (varBy is empty)
chkRes <- jTransform::jtMergeCols(data = dtaInp, varAll = names(dtaInp), fleInp = "../example4jtMergeCols_2.omv; ../example4jtMergeCols_3.omv",
typMrg = "right", btnCrt = FALSE)

expect_equal(names(chkRes), c("genInf", "pvwDta", "addInf"))
expect_equal(chkRes$genInf$asString(), paste("\n Please assign one or more variables that appear in all data sets\n",
"(e.g., a participant code) to \"Variable(s) to Match the Data Sets by\".\n",
"Afterwards, either write the name of (one or more) file(s) to be\n",
"merged under \"Data Set(s) to Add\" (separate mulitiple file names with\n",
"semicolons), or use \"Browse...\" to select input file(s).\n\n",
"For a more comprehensive explanation regarding the types of merging\n",
"operations, see \"Details\" underneath the preview table.\n"))
expect_equal(chkRes$addInf$asString(), paste("\n Details\n\n There are four different types of merging operations: The option\n",
"\"Keeps All Cases (Rows)\" keeps all cases (but if some input data sets\n",
"did not contain that value of the matching variable, there might be\n",
"missing values for variable from that data set). The second option\n",
"\"Keeps Only Cases Contained in All Merged Data Sets\" keeps only those\n",
"cases where a particular value of the matching variable was contained\n",
"in all datasets. The option \"Keeps All Cases From the Currently Opened\n",
"Data Set\" keeps all rows / cases from the active data set (whereas\n",
"cases that are only contained in the data sets defined under \"Data\n",
"Set(s) to Add\" are dropped). The option \"Keeps All Cases From the Data\n",
"Sets To Be Added\" keeps all cases from the data sets defined under\n",
"\"Data Set(s) to Add\" (whereas cases that are only contained in the\n",
"active data set are dropped).\n\n",
"If there are variables with the same name in several of the input data\n",
"sets, it will checked whether they contain the same content. If they\n",
"are the same, only the first of these identical variables is kept (and\n",
"the others are removed). If they are different, the name of the first\n",
"of these variables is kept, and a suffix with their position (_2, _3,\n",
"etc.) will be added to the name of all other variables.\n"))

# ensure that an error is thrown if no data are submitted
expect_error(jTransform::jtMergeCols(varBy = "ID", varAll = names(dtaInp), fleInp = "", fleChs = NULL, typMrg = "right", btnCrt = FALSE),
regexp = "Argument 'varBy' contains 'ID' which is not present in the dataset")
})
17 changes: 17 additions & 0 deletions tests/testthat/test-jtreplace.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ testthat::test_that("jtreplace works", {
expect_equal(chkRes$pvwDta$rowSelected, 0)
expect_equal(chkRes$pvwDta$width, 101)

# check instructions when chkVar fails (rplTrm is empty)
chkRes <- jTransform::jtReplace(data = dtaInp, varAll = names(dtaInp), whlTrm = TRUE, incCmp = TRUE, incRcd = TRUE,
incID = TRUE, incNom = TRUE, incOrd = TRUE, incNum = TRUE, incExc = "include", varSel = c("A1", "A2", "A3", "A4", "A5"), btnCrt = FALSE)
expect_equal(names(chkRes), c("genInf", "pvwDta"))
expect_equal(chkRes$genInf$asString(), paste("\n Please type the original value and the replacement into the entry\n",
"fields. If you want to have several pairs of original and replacment\n",
"values, use separate lines. To replace partial matches, unset the tick\n",
"box \"Whole Word\" (e.g., for orginal: 24 and replacement: 34, 241 will\n",
"be changed into 341).\n\n",
"The \"Include / Exclude\" collapse box permits to specifically select in\n",
"which column types, for which measurement type, and in which variables\n",
"to replace values. Ticking the check boxes includes that variable or\n",
"measurement type. When selecting individual variables using the\n",
"variable input, set the radio button to either only include the\n",
"selected variables or to exclude them.\n"))

# ensure that an error is thrown if no data are submitted
expect_error(jTransform::jtReplace(varAll = names(dtaInp), rplTrm = list(list(rplOld = "4", rplNew = "5")), whlTrm = TRUE, incExc = "exclude", varSel = c(), btnCrt = FALSE),
regexp = paste("Argument 'varAll' contains 'ID', 'A1', 'A2', 'A3', 'A4', 'A5', 'C1', 'C2', 'C3', 'C4', 'C5', 'E1', 'E2', 'E3', 'E4', 'E5',",
"'N1', 'N2', 'N3', 'N4', 'N5', 'O1', 'O2', 'O3', 'O4', 'O5', 'gender', 'age' which are not present in the dataset"))
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-jtsearch.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ testthat::test_that("jtsearch works", {
"230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243,\n",
"244, 245, 246, 247, 248, 249, 250\n"))

# check instructions when chkVar fails (varOrd is empty)
chkRes <- jTransform::jtSearch(data = dtaInp, varAll = names(dtaInp), ignCse = FALSE, whlTrm = FALSE)
expect_equal(names(chkRes), c("srcRes"))
expect_equal(chkRes$srcRes$asString(), paste("\n Please type the term to be search for into the text box. If you want\n",
"that partial matches (i.e., the search term appears within values) are\n",
"found, leave the tick box \"Whole Word\" unset.\n\n",
"The \"Include / Exclude\" collapse box permits to specifically select in\n",
"which column and measurement types the search shall be conducted.\n",
"Ticking the check boxes includes that variable or measurement type.\n"))

# ensure that an error is thrown if no data are submitted
expect_error(jTransform::jtSearch(varAll = names(dtaInp), srcTrm = "6", ignCse = FALSE, whlTrm = FALSE),
regexp = paste("Argument 'varAll' contains 'ID', 'A1', 'A2', 'A3', 'A4', 'A5', 'C1', 'C2', 'C3', 'C4', 'C5', 'E1', 'E2',",
"'E3', 'E4', 'E5', 'N1', 'N2', 'N3', 'N4', 'N5', 'O1', 'O2', 'O3', 'O4', 'O5', 'gender', 'age' which are",
Expand Down
Loading

0 comments on commit c352e9e

Please sign in to comment.