|
| 1 | +context("Testing polarization") |
| 2 | + |
| 3 | +# Create a function to set up the database for tests |
| 4 | +setup_dna_database <- function() { |
| 5 | + dna_init() |
| 6 | + samp <- dna_sample(overwrite = TRUE) |
| 7 | + dna_openDatabase(samp, coderId = 1, coderPassword = "sample") |
| 8 | + return(samp) |
| 9 | +} |
| 10 | + |
| 11 | +# Create a function to clean up after tests |
| 12 | +cleanup_dna_database <- function(samp) { |
| 13 | + dna_closeDatabase() |
| 14 | + unlink(samp) |
| 15 | +} |
| 16 | + |
| 17 | +test_that("dna_polarization produces expected output with help file example", { |
| 18 | + testthat::skip_on_cran() |
| 19 | + testthat::skip_on_ci() |
| 20 | + samp <- setup_dna_database() |
| 21 | + |
| 22 | + p <- dna_polarization(timeWindow = "days", |
| 23 | + windowSize = 8, |
| 24 | + kernel = "gaussian", |
| 25 | + normalizeScores = FALSE) |
| 26 | + expect_true("dna_polarization" %in% class(p)) |
| 27 | + expect_length(p, 10) |
| 28 | + expect_length(p$finalMaxQs, 22) |
| 29 | + expect_true(is.numeric(p$finalMaxQs)) |
| 30 | + expect_true("POSIXct" %in% class(p$startDates)) |
| 31 | + expect_true("POSIXct" %in% class(p$middleDates)) |
| 32 | + expect_true("POSIXct" %in% class(p$stopDates)) |
| 33 | + expect_equal(names(p), c("finalMaxQs", "earlyConvergence", "maxQs", "sdQs", "avgQs", "startDates", "middleDates", "stopDates", "memberships", "labels")) |
| 34 | + |
| 35 | + plots <- autoplot(p) |
| 36 | + expect_type(plots, "list") |
| 37 | + expect_length(plots, 3) |
| 38 | + expect_true("ggplot" %in% class(plots[[1]])) |
| 39 | + expect_true("ggplot" %in% class(plots[[2]])) |
| 40 | + expect_true("ggplot" %in% class(plots[[3]])) |
| 41 | + |
| 42 | + p2 <- dna_polarization(timeWindow = "no", |
| 43 | + normalizeScores = TRUE, |
| 44 | + algorithm = "genetic", |
| 45 | + randomSeed = 1234) |
| 46 | + expect_length(p2$finalMaxQs, 1) |
| 47 | + expect_equal(p2$finalMaxQs, 0.5342493, tolerance = 0.1) |
| 48 | + plots2 <- autoplot(p2) |
| 49 | + expect_length(plots2, 2) |
| 50 | + expect_true("ggplot" %in% class(plots2[[1]])) |
| 51 | + expect_true("ggplot" %in% class(plots2[[2]])) |
| 52 | + |
| 53 | + cleanup_dna_database(samp) |
| 54 | +}) |
0 commit comments