Skip to content

Commit 79a2746

Browse files
committed
test(functional): add tests for seeds, warm-up and logs
1 parent 5ba0cd7 commit 79a2746

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

tests/testthat/test-functionaltest.R

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,131 @@ patrick::with_parameters_test_that(
173173
init_value = 30L, adj_value = 10L)
174174
)
175175
)
176+
177+
178+
# -----------------------------------------------------------------------------
179+
# 3. Seeds
180+
# -----------------------------------------------------------------------------
181+
182+
test_that("the same seed returns the same result", {
183+
184+
param <- create_parameters(
185+
warm_up_period = 20L, data_collection_period = 20L,
186+
cores = 1L, number_of_runs = 3L
187+
)
188+
189+
# Run model twice using same run number (which will set the seed)
190+
same1 <- model(run_number = 0L, param = param)[["occupancy"]]
191+
same2 <- model(run_number = 0L, param = param)[["occupancy"]]
192+
expect_identical(same1, same2)
193+
194+
# Conversely, if run with different run number, expect different
195+
diff <- model(run_number = 1L, param = param)[["occupancy"]]
196+
expect_failure(expect_identical(same1, diff))
197+
198+
# Repeat experiment, but with multiple replications
199+
same_repeat1 <- runner(param = param)[["occupancy"]]
200+
same_repeat2 <- runner(param = param)[["occupancy"]]
201+
expect_identical(same_repeat1, same_repeat2)
202+
})
203+
204+
205+
test_that("model and runner produce same results if override future.seed", {
206+
207+
param <- create_parameters(
208+
warm_up_period = 20L, data_collection_period = 20L,
209+
cores = 1L, number_of_runs = 3L
210+
)
211+
212+
# Get result from runner, using future seeding
213+
futureseed_res <- runner(param, use_future_seeding = TRUE)[["occupancy"]]
214+
215+
# Get results from runner - with run numbers as seeds (future seed = FALSE)
216+
runnumber_res <- runner(param, use_future_seeding = FALSE)[["occupancy"]]
217+
218+
# Get results from model run in a loop
219+
model_res <- bind_rows(lapply(1L:param$number_of_runs, function(i) {
220+
model(run_number = i, param = param, set_seed = TRUE)[["occupancy"]]
221+
}))
222+
223+
# Expect model to differ from runner with future seeding, but match other
224+
expect_failure(expect_identical(futureseed_res, model_res))
225+
expect_identical(runnumber_res, model_res)
226+
})
227+
228+
229+
# -----------------------------------------------------------------------------
230+
# 4. Warm-up
231+
# -----------------------------------------------------------------------------
232+
233+
test_that("results are as expected if model runs with only a warm-up", {
234+
235+
# Run with only warm-up and no data collection period
236+
param <- create_parameters(
237+
warm_up_period = 100L, data_collection_period = 0L,
238+
cores = 1L, number_of_runs = 1L
239+
)
240+
result <- runner(param = param)
241+
242+
# Arrivals should be empty
243+
expect_identical(nrow(result[["arrivals"]]), 0L)
244+
245+
# Occupancy will have one record for each unit, from the final time of
246+
# warm-up (which would be the start of data collection, if existing)
247+
expect_identical(nrow(result[["occupancy"]]), 2L)
248+
expect_identical(nrow(result[["occupancy_stats"]][["asu_bed"]]), 1L)
249+
expect_identical(nrow(result[["occupancy_stats"]][["rehab_bed"]]), 1L)
250+
})
251+
252+
253+
test_that("running with warm-up leads to different results than without", {
254+
# Run without warm-up, expect first audit to have time and occupancy of 0
255+
param <- create_parameters(
256+
warm_up_period = 0L, data_collection_period = 20L,
257+
cores = 1L, number_of_runs = 1L
258+
)
259+
result <- runner(param = param)
260+
first_audit <- result[["occupancy"]] |>
261+
dplyr::arrange(time) |>
262+
dplyr::slice(1L:2L)
263+
expect_true(all(first_audit[["time"]] == 0L))
264+
expect_true(all(first_audit[["occupancy"]] == 0L))
265+
266+
# Run with warm-up, expect first audit to have time and occupancy > 0
267+
param <- create_parameters(
268+
warm_up_period = 50L, data_collection_period = 20L,
269+
cores = 1L, number_of_runs = 1L
270+
)
271+
result <- runner(param = param)
272+
first_audit <- result[["occupancy"]] |>
273+
dplyr::arrange(time) |>
274+
dplyr::slice(1L:2L)
275+
expect_true(all(first_audit[["time"]] > 0L))
276+
expect_true(all(first_audit[["occupancy"]] > 0L))
277+
})
278+
279+
280+
# -----------------------------------------------------------------------------
281+
# 5. Logs
282+
# -----------------------------------------------------------------------------
283+
284+
test_that("log to console and file work correctly", {
285+
# Set parameters and create temporary file for log
286+
log_file <- tempfile(fileext = ".log")
287+
param <- create_parameters(
288+
warm_up_period = 0L,
289+
data_collection_period = 20L,
290+
log_to_console = TRUE,
291+
log_to_file = TRUE,
292+
file_path = log_file
293+
)
294+
295+
# Check if "Parameters:" and "Log:" are in the console output
296+
expect_output(model(run_number = 1L, param = param),
297+
"Parameters:.*Log:", fixed = FALSE)
298+
299+
# Check if "Parameters:" and "Log:" are in the file output
300+
expect_true(file.exists(log_file))
301+
expect_match(readLines(log_file), "Parameters:", all = FALSE)
302+
expect_match(readLines(log_file), "Log:", all = FALSE)
303+
})

0 commit comments

Comments
 (0)