test_that("calculate_incidence works correctly", { # Test 1: Typical data mydata <- data.frame( ID = c("Greag", "Kristma", "Alex", "Jaminae", "Munroe", "Peteo"), Enrollment = as.Date(c("2023-03-10", "2023-02-02", "2023-01-01", "2023-02-02", "2023-01-01", "2023-01-04")), End = as.Date(c("2023-08-10", "2023-07-14", "2023-06-12", "2023-05-13", "2023-05-12", "2023-12-31")), Censored = c(1, 0, 0, 0, 1, 1) ) result <- incidence(mydata) # Define the expected result expected <- "The incidence rate is 2.8 new cases per 1000 person_days or approximately 85 new cases per 1000 person-months" # Compare the actual result to the expected result expect_equal(result, expected) }) test_that("calculate_incidence handles missing required columns", { # Test 1: Critical columns missing. mydata <- data.frame( ID = c("Greag", "Kristma", "Alex", "Jaminae", "Munroe", "Peteo"), Enrollment = as.Date(c("2023-03-10", "2023-02-02", "2023-01-01", "2023-02-02", "2023-01-01", "2023-01-04")), Ends = as.Date(c("2023-08-10", "2023-07-14", "2023-06-12", # Intentional typo "Ends" "2023-05-13", "2023-05-12", "2023-12-31")), Censored = c(1, 0, 0, 0, 1, 1) ) expect_error( incidence(mydata), "One or more required columns not found in the dataframe" ) }) test_that("calculate_incidence handles end dates preceding start dates", { # Test 1: Exit date preceding the entry date mydata <- data.frame( ID = c("Greag", "Kristma", "Alex", "Jaminae", "Munroe", "Peteo"), Enrollment = as.Date(c("2023-03-10", "2023-02-02", "2023-01-01", "2023-02-02", "2023-01-01", "2023-01-04")), End = as.Date(c("2023-02-10", "2023-07-14", "2023-06-12", # Intentional error "2023-05-13", "2023-05-12", "2023-12-31")), Censored = c(1, 0, 0, 0, 1, 1) ) expect_error( incidence(mydata), "One or more of the participant end dates precede the start date. Please check that out." ) })