Skip to content

Commit

Permalink
test: Replace "expect_equal" with "expect_identical"
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Oct 25, 2024
1 parent b157ae4 commit 3c962c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
14 changes: 8 additions & 6 deletions tests/testthat/test-DiseasyBaseModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ test_that("$hash works", {

# Change the hash
m$set_moduleowner(moduleowner = "testmodule")
expect_equal(m$hash, hash_new_instance)
expect_identical(m$hash, hash_new_instance)

# Try to set the hash
# test_that cannot capture this error, so we have to hack it
expect_identical(tryCatch(m$hash <- "test", error = \(e) e), # nolint: implicit_assignment_linter
simpleError("`$hash` is read only"))
expect_equal(m$hash, hash_new_instance)
expect_identical(m$hash, hash_new_instance)

rm(m)
})
Expand All @@ -60,7 +60,7 @@ test_that("$cache works", {

# Cache an item
private$cache("mtcars", mtcars)
expect_equal(m$hash, hash_new_instance)
expect_identical(m$hash, hash_new_instance)

# Try to retrieve a hash that does not exist
expect_error(private$cache("non_existent"),
Expand Down Expand Up @@ -107,11 +107,13 @@ test_that("$stratification_to_string() works", {
private <- m$.__enclos_env__$private

# Test the stratification_to_string with empty input
expect_equal(private$stratification_to_string(NULL), NA_character_)
expect_identical(private$stratification_to_string(NULL), NA_character_)

# Test the stratification_to_string with a couple of permutations
expect_equal(private$stratification_to_string(rlang::quos(test = a + b, test_id)),
"test = a + b, test_id")
expect_identical(
private$stratification_to_string(rlang::quos(test = a + b, test_id)),
"test = a + b, test_id"
)

rm(m)
})
48 changes: 24 additions & 24 deletions tests/testthat/test-DiseasyModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test_that("initialize works with functional modules", {

# Check a label can be set
m_label <- DiseasyModel$new(label = "test")
expect_equal(m_label$hash, m$hash) # label should not change the hash
expect_identical(m_label$hash, m$hash) # label should not change the hash

rm(m, m_act_instance, m_s_instance, m_obs_instance, m_act_boolean, m_s_boolean, m_obs_boolean, m_var_boolean, m_label)
})
Expand Down Expand Up @@ -77,14 +77,14 @@ test_that("initialize works with model parameters", {

# Test that parameters use the default value
m <- DiseasyModelParameterTest$new()
expect_equal(m %.% parameters %.% a, 1)
expect_equal(m %.% parameters %.% b, 2)
expect_identical(m %.% parameters %.% a, 1)
expect_identical(m %.% parameters %.% b, 2)
rm(m)

# Test that parameters can be set during initialization
m <- DiseasyModelParameterTest$new(parameters = list("a" = 3, "b" = 4))
expect_equal(m %.% parameters %.% a, 3)
expect_equal(m %.% parameters %.% b, 4)
expect_identical(m %.% parameters %.% a, 3)
expect_identical(m %.% parameters %.% b, 4)
rm(m)

# Check that setting non-existing parameters will give an error
Expand Down Expand Up @@ -208,16 +208,16 @@ test_that("$hash works", {

# Check reloading modules works
m$load_module(act)
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)

m$load_module(s)
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)

m$load_module(obs)
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)

m$load_module(var)
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)


# Check loading of altered module changes the hash
Expand All @@ -229,20 +229,20 @@ test_that("$hash works", {
m$load_module(act) # Reset to original

s_alt <- DiseasySeason$new(reference_date = as.Date("2020-03-01"))
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)
m$load_module(s_alt)
expect_false(m$hash == hash_4)
m$load_module(s) # Reset to original

obs_alt <- DiseasyObservables$new(last_queryable_date = as.Date("2020-03-01"))
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)
m$load_module(obs_alt)
expect_false(m$hash == hash_4)
m$load_module(obs) # Reset to original

var_alt <- DiseasyVariant$new()
var_alt$add_variant(name = "WT")
expect_equal(m$hash, hash_4)
expect_identical(m$hash, hash_4)
m$load_module(var_alt)
expect_false(m$hash == hash_4)
m$load_module(var) # Reset to original
Expand Down Expand Up @@ -281,7 +281,7 @@ test_that("cloning works", {

# Create a simple clone
m_c <- m$clone()
expect_equal(m_c$hash, hash_loaded) # Hash should be the same
expect_identical(m_c$hash, hash_loaded) # Hash should be the same



Expand All @@ -290,49 +290,49 @@ test_that("cloning works", {
act_alt$set_activity_units(dk_activity_units)
act_alt$change_activity(head(scenario_1, 3))
m_c$load_module(act_alt)
expect_equal(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_false(m_c$hash == hash_loaded) # Hash should be changed for the clone instance

s_alt <- DiseasySeason$new(reference_date = as.Date("2020-03-01"))
m_c$load_module(act)
expect_equal(m_c$hash, hash_loaded) # Hash should now be reset
expect_identical(m_c$hash, hash_loaded) # Hash should now be reset
m_c$load_module(s_alt)
expect_equal(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_false(m_c$hash == hash_loaded) # Hash should be changed for the clone instance

obs_alt <- DiseasyObservables$new(last_queryable_date = as.Date("2020-03-01"))
m_c$load_module(s)
expect_equal(m_c$hash, hash_loaded) # Hash should now be reset
expect_identical(m_c$hash, hash_loaded) # Hash should now be reset
m_c$load_module(obs_alt)
expect_equal(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_false(m_c$hash == hash_loaded) # Hash should be changed for the clone instance



# If we change the module inside of one, it should not change in the other
m_c$load_module(obs)
expect_equal(m_c$hash, hash_loaded) # Hash should now be reset
expect_identical(m_c$hash, hash_loaded) # Hash should now be reset


# - activity
m_c$activity$reset_scenario()
expect_equal(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_false(m_c$hash == hash_loaded)# Hash should be changed for the clone instance
expect_false(act$hash == m_c$activity$hash) # module hashes should also be different

# - season
m_c$load_module(act)
expect_equal(m_c$hash, hash_loaded) # Hash should now be reset
expect_identical(m_c$hash, hash_loaded) # Hash should now be reset
m_c$season$set_reference_date(as.Date("2020-03-01"))
expect_equal(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash should be the same for the original instance
expect_false(m_c$hash == hash_loaded)# Hash should be changed for the clone instance
expect_false(s$hash == m_c$season$hash) # module hashes should also be different

# - observables
m_c$load_module(s)
expect_equal(m_c$hash, hash_loaded) # Hash should now be reset
expect_identical(m_c$hash, hash_loaded) # Hash should now be reset
m_c$observables$set_last_queryable_date(as.Date("2020-03-01"))
expect_equal(m$hash, hash_loaded) # Hash "202should be the same for the original instance
expect_identical(m$hash, hash_loaded) # Hash "202should be the same for the original instance
expect_false(m_c$hash == hash_loaded)# Hash should be changed for the clone instance
expect_false(obs$hash == m_c$observables$hash) # module hashes should also be different

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-DiseasyObservables.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ test_that("$set_study_period() works", {
obs <- DiseasyObservables$new()
obs$set_study_period(start_date = as.Date("2021-03-01"),
end_date = as.Date("2021-03-03"))
expect_equal(obs$start_date, as.Date("2021-03-01"))
expect_equal(obs$end_date, as.Date("2021-03-03"))
expect_identical(obs$start_date, as.Date("2021-03-01"))
expect_identical(obs$end_date, as.Date("2021-03-03"))

# Testing malformed inputs

Expand Down
34 changes: 17 additions & 17 deletions tests/testthat/test-DiseasySeason.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ test_that("$get- and $use_season_model() works with known model", {
s <- DiseasySeason$new(reference_date = Sys.Date())

models <- expect_no_error(s$get_season_model("cosine_season"))
expect_equal(models$model_t, s$get_cosine_season()$model_t)
expect_equal(models$model_date, s$get_cosine_season()$model_date)
expect_equal(models$model_t, s$get_cosine_season()$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(models$model_date, s$get_cosine_season()$model_date) # nolint: expect_identical_linter. Functions have different environments

expect_no_error(s$use_season_model("cosine_season"))
expect_equal(s$model_t, s$get_cosine_season()$model_t)
expect_equal(s$model_date, s$get_cosine_season()$model_date)
expect_equal(s$model_t, s$get_cosine_season()$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(s$model_date, s$get_cosine_season()$model_date) # nolint: expect_identical_linter. Functions have different environments

rm(s)

Expand Down Expand Up @@ -81,8 +81,8 @@ test_that("$get- and $use_constant_season() works", {
models <- s$get_constant_season()
s$use_constant_season()

expect_equal(s$model_t, models$model_t)
expect_equal(s$model_date, models$model_date)
expect_equal(s$model_t, models$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(s$model_date, models$model_date) # nolint: expect_identical_linter. Functions have different environments

expect_identical(s$model_t(0), 1)
expect_identical(s$model_t(1), 1)
Expand Down Expand Up @@ -111,8 +111,8 @@ test_that("$get- and $use_cosine_season() works", {
models <- s$get_cosine_season()
s$use_cosine_season()

expect_equal(s$model_t, models$model_t)
expect_equal(s$model_date, models$model_date)
expect_equal(s$model_t, models$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(s$model_date, models$model_date) # nolint: expect_identical_linter. Functions have different environments

expect_identical(s$model_t(0), 1)
expect_false(s$model_t(1) == 1)
Expand Down Expand Up @@ -153,8 +153,8 @@ test_that("$use_covid_season_v1() works", {
models <- s$get_covid_season_v1()
s$use_covid_season_v1()

expect_equal(s$model_t, models$model_t)
expect_equal(s$model_date, models$model_date)
expect_equal(s$model_t, models$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(s$model_date, models$model_date) # nolint: expect_identical_linter. Functions have different environments

expect_identical(s$model_t(0), 1)
expect_false(s$model_t(1) == 1)
Expand Down Expand Up @@ -199,8 +199,8 @@ test_that("$use_covid_season_v2() works", {
models <- s$get_covid_season_v1()
s$use_covid_season_v1()

expect_equal(s$model_t, models$model_t)
expect_equal(s$model_date, models$model_date)
expect_equal(s$model_t, models$model_t) # nolint: expect_identical_linter. Functions have different environments
expect_equal(s$model_date, models$model_date) # nolint: expect_identical_linter. Functions have different environments
s$use_covid_season_v2()

expect_identical(s$model_t(0), 1)
Expand Down Expand Up @@ -342,13 +342,13 @@ test_that("active binding: model_t works", {
s <- DiseasySeason$new()

# Retrieve the model_t
expect_equal(s$model_t, s$get_season_model("constant_season")$model_t)
expect_equal(s$model_t, s$get_season_model("constant_season")$model_t) # nolint: expect_identical_linter. Functions have different environments

# Try to set the model_t
# test_that cannot capture this error, so we have to hack it
expect_identical(tryCatch(s$model_t <- \(t) t, error = \(e) e), # nolint: implicit_assignment_linter
simpleError("`$model_t` is read only"))
expect_equal(s$model_t, s$get_season_model("constant_season")$model_t)
expect_equal(s$model_t, s$get_season_model("constant_season")$model_t) # nolint: expect_identical_linter. Functions have different environments

rm(s)
})
Expand All @@ -358,13 +358,13 @@ test_that("active binding: model_date works", {
s <- DiseasySeason$new()

# Retrieve the model_date
expect_equal(s$model_date, s$get_season_model("constant_season")$model_date)
expect_equal(s$model_date, s$get_season_model("constant_season")$model_date) # nolint: expect_identical_linter. Functions have different environments

# Try to set the model_date
# test_that cannot capture this error, so we have to hack it
expect_identical(tryCatch(s$model_date <- \(date) date, error = \(e) e), # nolint: implicit_assignment_linter
simpleError("`$model_date` is read only"))
expect_equal(s$model_date, s$get_season_model("constant_season")$model_date)
expect_equal(s$model_date, s$get_season_model("constant_season")$model_date) # nolint: expect_identical_linter. Functions have different environments

rm(s)
})
Expand All @@ -382,7 +382,7 @@ test_that("active binding: available_season_models works", {
# test_that cannot capture this error, so we have to hack it
expect_identical(tryCatch(s$available_season_models <- "unknown_season_model", error = \(e) e), # nolint: implicit_assignment_linter
simpleError("`$available_season_models` is read only"))
expect_equal(s$available_season_models, expected_season_models)
expect_identical(s$available_season_models, expected_season_models)

rm(s)
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-DiseasyVariant.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ test_that("active binding: cross_immunity works", {
var <- DiseasyVariant$new()

# Retrieve the cross_immunity
expect_equal(var %.% cross_immunity, matrix(1))
expect_identical(var %.% cross_immunity, matrix(1))

# Try to set the cross_immunity
# test_that cannot capture this error, so we have to hack it
expect_identical(tryCatch(var$cross_immunity <- matrix(2), error = \(e) e), # nolint: implicit_assignment_linter
simpleError("`$cross_immunity` is read only"))
expect_equal(var %.% cross_immunity, matrix(1))
expect_identical(var %.% cross_immunity, matrix(1))

rm(var)
})
Expand Down

0 comments on commit 3c962c3

Please sign in to comment.