From 5c038c7576e7ffdae4f8b2ed95dccf34d0d566bf Mon Sep 17 00:00:00 2001 From: Chen Zhao Date: Sat, 13 Mar 2021 16:12:22 -0700 Subject: [PATCH] Fix tests to cover all --- R/noaastnr.R | 3 - tests/testthat/test-plot_weather_data.R | 75 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/R/noaastnr.R b/R/noaastnr.R index 40371a3..b3c3f48 100644 --- a/R/noaastnr.R +++ b/R/noaastnr.R @@ -194,9 +194,6 @@ plot_weather_data <- function(obs_df, col_name, time_basis) { df <- obs_df df <- tidyr::drop_na(df) - if (nrow(df) < 3) { - stop("Dataset is not sufficient to visualize.") - } year <- lubridate::year(lubridate::floor_date(df$datetime, "year")[1]) diff --git a/tests/testthat/test-plot_weather_data.R b/tests/testthat/test-plot_weather_data.R index cc713db..6ddd83f 100644 --- a/tests/testthat/test-plot_weather_data.R +++ b/tests/testthat/test-plot_weather_data.R @@ -83,3 +83,78 @@ test_that("plot should use geom_line", { expect_true("GeomLine" %in% c(class(plot_ws_d$layers[[1]]$geom))) expect_true("GeomLine" %in% c(class(plot_wd_d$layers[[1]]$geom))) }) + +# Test input data type + +test_that("Test failed for checking input param type", { + expect_error( + plot_weather_data( + obs_df = 1, + col_name = "air_temp", + time_basis = "monthly" + ), + "Weather data should be a dataFrame." + ) + expect_error( + plot_weather_data( + obs_df = weather_df, + col_name = 1, + time_basis = "monthly" + ), + "Variable name must be entered as a factor." + ) + expect_error( + plot_weather_data( + obs_df = weather_df, + col_name = "air_temp", + time_basis = 1 + ), + "Time basis must be entered as a factor." + ) +}) + +# Test input value + +test_that("Test failed for checking input values", { + expect_error( + plot_weather_data( + obs_df = weather_df, + col_name = "test", + time_basis = "monthly" + ), + "Variable can only be one of air_temp, atm_press, wind_spd or wind_dir" + ) + expect_error( + plot_weather_data( + obs_df = weather_df, + col_name = "air_temp", + time_basis = "test" + ), + "Time basis can only be monthly or daily" + ) +}) + +# Test input data set size + +station_number <- "714950-99999" +year <- 2004 +weather_df_size_test <- get_weather_data(station_number, year) + +test_that("Test failed for checking input data set size", { + expect_error( + plot_weather_data( + obs_df = weather_df_size_test, + col_name = "air_temp", + time_basis = "monthly" + ), + "Dataset is not sufficient to visualize." + ) + expect_error( + plot_weather_data( + obs_df = weather_df_size_test, + col_name = "air_temp", + time_basis = "daily" + ), + "Dataset is not sufficient to visualize." + ) +})