context("Plotting Functions") # Mock the data fetching to avoid actual API calls during tests # You might need more sophisticated mocking frameworks like {httptest} or {mockery} # For simplicity, we'll assume fetch_prep_activities returns pre-defined data # Mock fetch_prep_activities to return sample data # This needs careful setup, maybe using {mockery} # mockery::stub(plot_acwr, 'fetch_prep_activities', sample_activity_data) # --- Test plot_acwr --- test_that("plot_acwr returns a ggplot object", { # Create minimal required input data (bypassing fetch) set.seed(456) days <- 40 sample_load <- dplyr::tibble( date = lubridate::today() - lubridate::days(days:1), load = sample(50:150, days, replace = TRUE) ) acwr_data <- calculate_acwr(sample_load, 7, 28) # Need a mock stoken object (or skip the check inside plot_acwr for tests) # For now, assume plot_acwr can run if data is provided directly (or mock fetch) # This test is basic: does it run without error and return a ggplot? # Need to mock the data fetching part within plot_acwr or pass data directly if allowed # skip("Skipping plot test until data mocking is implemented") # Skip if mocking not set up # Assuming calculate_acwr etc. are tested, focus on plot object creation # We need a dummy stoken dummy_stoken <- structure(list(), class = "Token2.0") # Basic dummy # Mock the internal data steps for a focused test mockery::stub(plot_acwr, 'fetch_prep_activities', sample_load) # Simplistic mock mockery::stub(plot_acwr, 'calculate_daily_load', sample_load) mockery::stub(plot_acwr, 'calculate_acwr', acwr_data) # Test if it returns a ggplot object # Note: This specific mock setup might not work directly, depends on function internals # A better approach might be to test components separately. # For now, let's assume a simple test setup # Expect plot_acwr to run (actual test requires proper mocking setup) # expect_s3_class(plot_acwr(stoken = dummy_stoken), "ggplot") }) # --- Test plot_exposure --- test_that("plot_exposure returns a ggplot object", { dummy_stoken <- structure(list(), class = "Token2.0") # Basic dummy # Similar mocking or component testing needed as for plot_acwr # expect_s3_class(plot_exposure(stoken = dummy_stoken), "ggplot") # skip("Skipping plot test until data mocking is implemented") }) # ADD TESTS for plot_ef, plot_pbs, plot_decoupling # Test error handling: e.g., missing user_ftp for TSS plots test_that("plot_exposure errors if user_ftp is missing for TSS", { dummy_stoken <- structure(list(), class = "Token2.0") # Basic dummy # Mock fetch to return something basic mockery::stub(plot_exposure, 'fetch_prep_activities', dplyr::tibble(id="1", date=lubridate::today())) # Expect error when calling with load_metric='tss' but no user_ftp # This relies on calculate_daily_load throwing the error correctly expect_error(plot_exposure(stoken = dummy_stoken, load_metric = "tss", user_ftp = NULL)) })