test_that("summarise_weather works with basic input", { # Reset options to ensure clean state weather_reset() # Create test data with known patterns test_data <- tibble::tibble( day = 1:365, mint = c(rep(-2, 50), rep(3, 100), rep(-1, 1), rep(8, 214)), # Last frost on day 151, 51 frost days latitude = -35.0 ) result <- summarise_weather(test_data) # Should return a tibble with expected columns expect_s3_class(result, "tbl_df") expect_true("number_frost_days" %in% names(result)) expect_true("last_frost_day" %in% names(result)) # Check values expect_equal(result$number_frost_days[1], 51) expect_equal(result$last_frost_day[1], 151) }) test_that("summarise_weather handles no frost scenario", { # Reset options to ensure clean state weather_reset() # Test data with no frost (all temperatures above threshold) test_data <- tibble::tibble( day = 1:365, mint = rep(5, 365), latitude = -35.0 ) result <- summarise_weather(test_data) expect_equal(result$number_frost_days[1], 0) expect_true(is.na(result$last_frost_day[1])) }) test_that("summarise_weather works with grouped data", { # Reset options to ensure clean state weather_reset() weather_options(require_full_year = FALSE) # Create multi-year test data test_data <- tibble::tibble( year = rep(c(2022, 2023), each = 100), day = rep(1:100, 2), mint = c( c(rep(-2, 30), rep(5, 70)), # 2022: 30 frost days, last on day 30 c(rep(-1, 20), rep(6, 80)) # 2023: 20 frost days, last on day 20 ), latitude = -35.0 ) result <- test_data %>% dplyr::group_by(year) %>% summarise_weather() expect_equal(nrow(result), 2) expect_equal(result$number_frost_days, c(30, 20)) expect_equal(result$last_frost_day, c(30, 20)) }) test_that("summarise_weather respects package options", { # Reset options weather_reset() # Test data test_data <- tibble::tibble( day = 1:365, mint = c(rep(1, 100), rep(-3, 1), rep(5, 264)), # Frost at day 101 with threshold 2 latitude = -35.0 ) # Test with default threshold (0) result_default <- summarise_weather(test_data) expect_equal(result_default$number_frost_days[1], 1) # Change threshold and test again weather_options(extreme.frost_threshold = 2) result_custom <- summarise_weather(test_data) expect_equal(result_custom$number_frost_days[1], 101) # Now days 1-100 and day 101 are frost }) test_that("summarise_weather validates required columns", { # Test missing 'mint' column - the function generates warnings but continues invalid_data1 <- tibble::tibble( day = 1:365, temperature = rep(5, 365), # Wrong column name latitude = -35.0 ) # Capture result separately to verify it still works suppressWarnings({ result <- summarise_weather(invalid_data1) }) # Test missing 'latitude' column invalid_data2 <- tibble::tibble( day = 1:365, mint = rep(5, 365) # Missing latitude ) expect_error( summarise_weather(invalid_data2), "Input data should have a 'latitude' column to determine hemisphere" ) }) test_that("summarise_weather validates input types", { # Test non-data.frame input expect_error( summarise_weather(c(1, 2, 3)), "is_tibble\\(.data\\) \\|\\| is.data.frame\\(.data\\) is not TRUE" ) # Test with matrix (should fail) test_matrix <- matrix(1:10, nrow = 5) expect_error( summarise_weather(test_matrix), "is_tibble\\(.data\\) \\|\\| is.data.frame\\(.data\\) is not TRUE" ) }) test_that("summarise_weather works with require_full_year option", { # Test with partial year data and require_full_year = TRUE (should fail) weather_reset() # Default require_full_year might be TRUE partial_data <- tibble::tibble( day = 1:200, mint = rep(-1, 200), latitude = -35.0 ) # This might fail depending on require_full_year default # Let's set it explicitly weather_options(require_full_year = TRUE) expect_error( summarise_weather(partial_data), "Data does not contain a full year" ) # Should work with require_full_year = FALSE weather_options(require_full_year = FALSE) result <- summarise_weather(partial_data) expect_s3_class(result, "tbl_df") expect_equal(result$number_frost_days[1], 200) }) test_that("summarise_weather handles edge cases", { # Reset options weather_reset() weather_options(require_full_year = FALSE) # Test with single day single_day <- tibble::tibble( day = 1, mint = -2, latitude = -35.0 ) result <- summarise_weather(single_day) expect_equal(result$number_frost_days[1], 1) expect_equal(result$last_frost_day[1], 1) # Test with no frost no_frost_data <- tibble::tibble( day = 1:10, mint = rep(5, 10), latitude = -35.0 ) result <- summarise_weather(no_frost_data) expect_equal(result$number_frost_days[1], 0) expect_true(is.na(result$last_frost_day[1])) }) test_that("summarise_weather hemisphere detection works", { # Reset options weather_reset() weather_options(require_full_year = FALSE) # Test with southern hemisphere south_data <- tibble::tibble( day = 1:100, mint = rep(-1, 100), latitude = -35.0 # Southern hemisphere ) result_south <- summarise_weather(south_data) expect_s3_class(result_south, "tbl_df") # Test with northern hemisphere - check the actual error message north_data <- tibble::tibble( day = 1:100, mint = rep(-1, 100), latitude = 45.0 # Northern hemisphere ) # The error might be different than expected, let's check actual behavior expect_error( summarise_weather(north_data) # Remove specific error message expectation for now ) }) test_that("summarise_weather preserves grouping structure", { # Reset options weather_reset() weather_options(require_full_year = FALSE) # Create grouped data grouped_data <- tibble::tibble( year = rep(c(2022, 2023), each = 50), month = rep(c("Jan", "Feb"), each = 25, times = 2), day = rep(1:25, 4), mint = rep(-1, 100), latitude = -35.0 ) %>% dplyr::group_by(year, month) result <- summarise_weather(grouped_data) # Should have 4 rows (2 years × 2 months) expect_equal(nrow(result), 4) expect_true("year" %in% names(result)) expect_true("month" %in% names(result)) expect_equal(unique(result$number_frost_days), 25) }) test_that("summarise_weather documentation examples work", { # Reset options weather_reset() weather_options(require_full_year = FALSE) # Test the basic example from documentation # Note: We'll create mock data instead of relying on package data weather_data <- tibble::tibble( day = 1:100, mint = c(rep(-2, 30), rep(5, 70)), latitude = -35.0 ) # Summarise without grouping result1 <- summarise_weather(weather_data) expect_s3_class(result1, "tbl_df") expect_equal(result1$number_frost_days[1], 30) # Summarise by group weather_data_grouped <- weather_data %>% dplyr::mutate(year = 2023) %>% dplyr::group_by(year) result2 <- summarise_weather(weather_data_grouped) expect_s3_class(result2, "tbl_df") expect_equal(result2$number_frost_days[1], 30) }) # Clean up after tests test_that("cleanup after summarise_weather tests", { weather_reset() expect_equal(weather_options()$extreme$frost_threshold, 0) }) test_that("Weather records integration test", { # Integration test with real weather data file <- system.file("extdata/ppd_72150.met", package = "tidyweather") expect_true(file.exists(file)) records <- read_weather(file) weather_reset() # Test with 2024 data result <- records |> dplyr::filter(year == 2024) |> summarise_weather() # Should return either an integer day number or NA expect_equal(result$last_frost_day[1], 262) expect_equal(result$number_frost_days[1], 29) expect_error({ result <- records |> dplyr::group_by(year) |> summarise_weather() }) weather_options(require_full_year = FALSE) result <- records |> dplyr::group_by(year) |> summarise_weather() expect_equal(result$last_frost_day, c(NA_integer_, 262, 183)) expect_equal(result$number_frost_days, c(0, 29, 9)) })