Skip to content

Commit

Permalink
fixes for date range subsetting and extra tests to get this sucker ov…
Browse files Browse the repository at this point in the history
…er 90%
  • Loading branch information
robbriers committed May 10, 2019
1 parent ee0d793 commit 6373fc1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
28 changes: 10 additions & 18 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -278,38 +278,30 @@ check_args <- function(ea_name = NULL, column = NULL, startyr = NULL,
subset_data <- function(full_data, column = NULL,
level = "Overall Water Body", startyr = NULL, endyr = NULL, type = NULL, data_type=NULL) {

# if only start year is set, is it beyond the data range and not objectives
# (only value being passed in to data_type)
if (!is.null(startyr) & is.null(endyr) & !is.null(data_type)){
# if only start year is set beyond the data range and not objectives
# (only value being passed in to data_type) then st to max and ignore endyr
if (!is.null(startyr) & is.null(data_type)){
if (startyr>max(full_data$year)){
message(paste0("Start year is beyond the most recent year of data (",
message(paste0("Start year (", startyr, ") is beyond the most recent year of data (",
max(full_data$year),")"))
message("Just outputting most recent year")
startyr<-max(full_data$year)
# as only outputting most recent year, set endyr to null
if(!is.null(endyr)){
endyr<-NULL
}
}
}
# if endyr is set, is it beyond the data range?
if (!is.null(endyr)){
if (endyr>max(full_data$year)){
message(paste0("End year is beyond the most recent year of data (",
message(paste0("End year (", endyr, ") is beyond the most recent year of data (",
max(full_data$year),")"))
message("Subsetting to most recent year")
endyr<-max(full_data$year)
}
}
# if they are both set, check the endyr
if (!is.null(startyr) & !is.null(endyr)) {
if (endyr>max(full_data$year)){
message(paste0("End year is beyond the most recent year of data (",
max(full_data$year),")"))
message("Subsetting to most recent year")
endyr<-max(full_data$year)
}
# if both years are specified, subset by range
full_data <- full_data[full_data$year >= startyr
& full_data$year <= endyr, ]
}
else if (!is.null(startyr)) {
if (!is.null(startyr)) {
full_data <- full_data[full_data$year == startyr, ]
}
# level subsetting, defaults to "Overall Water Body"
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-get_objectives.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ test_that("invalid level returns an error", {
})

test_that("specifying type for WB download returns a message", {
# retrieve data for level that does not exist
# retrieve data for WB with type specified
expect_message(get_objectives("GB520804714300", "WBID", type="River"))
})

test_that("retuning no objectives give a message", {
# retrieve data for level that does not exist
test_that("returning no objectives give a message", {
# retrieve data for combination with no objectives set
expect_message(get_objectives("Cornwall North Coastal", "OC", year=2040))
})

9 changes: 8 additions & 1 deletion tests/testthat/test-get_status.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("get_status returns a dataframe", {
test_that("dimensions of dataframe are as expected", {
# retrieve data for Avon Warwickshire MC
test_mc_avon<-get_status("Avon Warwickshire", "MC")
# check that the dimensions are 594 rows, 19 cols
# check that the dimensions are 594 rows, 18 cols
expect_true(all(dim(test_mc_avon)== c(594, 18)))
})

Expand Down Expand Up @@ -97,3 +97,10 @@ test_that("incorrect set of criteria returns a message", {
# retrieve data for level that does not exist
expect_message(get_status("Avon Warwickshire", "MC", type="CoastalWater"))
})

test_that("dimensions of dataframe for 'Supporting elements' level are as expected", {
# retrieve data for Avon Warwickshire MC
test_mc_avon<-get_status("Avon Warwickshire", "MC", level="Supporting elements")
# check that the dimensions are 78 rows, 18 cols
expect_true(all(dim(test_mc_avon)== c(78, 18)))
})

0 comments on commit 6373fc1

Please sign in to comment.