Skip to content

Commit

Permalink
Fix tests to cover all
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhao299 committed Mar 13, 2021
1 parent 41410a9 commit 5c038c7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
3 changes: 0 additions & 3 deletions R/noaastnr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
75 changes: 75 additions & 0 deletions tests/testthat/test-plot_weather_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
})

0 comments on commit 5c038c7

Please sign in to comment.