Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-dray committed Dec 22, 2019
1 parent b688a60 commit ade60d1
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 51 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -18,5 +18,5 @@ Suggests:
covr,
knitr,
rmarkdown,
testthat
testthat (>= 2.1.0)
VignetteBuilder: knitr
26 changes: 14 additions & 12 deletions R/handle.R
Expand Up @@ -70,11 +70,15 @@ oy_read <- function(path) {

# Warnings based on the number of elements removed
if(count_removed == 1) {
warning(paste(count_removed, "CSV file wasn't in the expected format and was discarded."))
warning(paste(
count_removed,
"CSV file wasn't in the expected format and was discarded.")
)
} else if (count_removed > 0) {
warning(paste(count_removed, "CSV files weren't in the expected format and were discarded."))
} else {
stop("None of the CSVs were in the folder were in the expected format.")
warning(paste(
count_removed,
"CSV files weren't in the expected format and were discarded.")
)
}

}
Expand Down Expand Up @@ -114,20 +118,18 @@ oy_clean <- function(x) {
}

# Stop for non-Oyster-journey-history files
if(!names(x) %in% c("Date", "Start.Time", "End.Time", "Journey.Action",
"Charge", "Credit", "Balance", "Note")) {
input_names <- c(
"Date", "Start.Time", "End.Time", "Journey.Action", "Charge", "Credit",
"Balance", "Note"
)
if(!all(names(x) == input_names)) {
stop(
"The input data.frame doesn't look like an Oyster journey history file\n.",
"The input data.frame doesn't look like an Oyster journey history file.\n",
"It should be an unaltered file received from Transport for London.\n",
"Try reading CSVs with the oy_read() function."
)
}

# Stop for lack of content
if(nrow(x) == 0) {
stop("The input data.frame appears to be empty.")
}

# Meta: make column names lowercase and use underscore instead of period
names(x) <- tolower(gsub("\\.", "_", names(x)))

Expand Down
24 changes: 10 additions & 14 deletions R/viz.R
Expand Up @@ -91,15 +91,11 @@ oy_cols <- function(...) {
} else if (any(is.na(cols))) {
stop("Please provide colour names or indices only.")
} else if (is.numeric(cols) & max(cols) > length(oyster_colours)) {
stop(
"You've provided an index that's out of range.\n",
"Choose only values between 1 and ", length(oy_cols()), "."
)
stop("You've provided an index that's out of range.\n",
"Choose only values between 1 and ", length(oy_cols()), ".")
} else if (is.character(cols) & !all(cols %in% names(oyster_colours))) {
stop(
"You've provided an invalid colour name.\n",
"See ?oy_cols for details or print colour names with names(oy_cols())."
)
stop("You've provided an invalid colour name.\n",
"See ?oy_cols for details or print colour names with names(oy_cols()).")
} else if (!is.null(cols)) {
return(oyster_colours[cols])
}
Expand Down Expand Up @@ -147,7 +143,7 @@ oy_lineplot <- function(
} else if (!x_var %in% c("datetime_start", "datetime_end")) {
stop(
"For now, the x_var argument must be 'datetime_start' or 'datetime_end.\n",
"You provided the variable '", y_var, "'."
"You provided the variable '", x_var, "'."
)
} else if (!y_var %in% c("journey_duration", "balance")) {
stop(
Expand All @@ -165,15 +161,15 @@ oy_lineplot <- function(
)
}

# Eliminate weekends
if (weekdays == TRUE) {
data <- data[!data$weekday_start %in% c("Saturday", "Sunday"), ]
} else { data }

# Wrangle
df <- data[data$mode == mode, c(x_var, y_var)] # train only
df <- df[complete.cases(df), ] # no rows with NA in x or y

# Eliminate weekends
if (weekdays == TRUE) {
df <- df[!df$weekday_start %in% c("Saturday, Sunday"), ]
}

# Plot
plot(
x = df[, x_var], y = df[, y_var],
Expand Down
3 changes: 3 additions & 0 deletions tests/testdata/invalid_multi/invalid1.csv
@@ -0,0 +1,3 @@
col1,col2,col3
1,2,3
a,b,c
3 changes: 3 additions & 0 deletions tests/testdata/invalid_multi/invalid2.csv
@@ -0,0 +1,3 @@
col1,col2,col3
1,2,3
a,b,c
3 changes: 3 additions & 0 deletions tests/testdata/invalid_names/invalid_names.csv
@@ -0,0 +1,3 @@
,,,,,,,
Wrong,Wrong,Wrong,Wrong,Wrong,Wrong,Wrong,Wrong
01-Jan-2019,17:00,17:45,Foo [National Rail] to Bar,1,,10
Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.
10 changes: 6 additions & 4 deletions tests/testthat/test-clean.R
@@ -1,10 +1,7 @@
context("test-clean")

test_that("valid input returns a data.frame", {
test_that("input data returns expected output", {
expect_identical(class(oy_clean(journeys_read)), "data.frame")
})

test_that("invalid input causes an error", {
test_list <- list(x = 1:3, y = 1:3)
expect_error(oy_clean())
expect_error(oy_clean(NA))
Expand All @@ -16,3 +13,8 @@ test_that("invalid input causes an error", {
expect_error(oy_clean(bare_string))
expect_error(oy_clean(test_list))
})

test_that("invalid names produces error", {
invalid_names <- read.csv("../testdata/invalid_names/invalid_names.csv")
expect_error(oy_clean(invalid_names))
})
4 changes: 3 additions & 1 deletion tests/testthat/test-colour.R
Expand Up @@ -23,8 +23,10 @@ test_that("invalid indices fail", {
# TODO: negative values should cause an error
})

test_that("invalid names fail", {
test_that("input character strings behave as expected", {
expect_identical(class(oy_cols("line_circle")), "character")
expect_identical(class(oy_cols("line_circle", "line_victoria")), "character")
expect_identical(class(oy_cols(c("line_circle", "line_victoria"))), "character")
expect_error(oy_cols("invalid_colour_name"))
expect_error(oy_cols("line_circle", NA))
})
13 changes: 10 additions & 3 deletions tests/testthat/test-plot.R
Expand Up @@ -2,20 +2,27 @@ context("test-plot")

test_that("an invalid data object results in an error", {
expect_identical(class(oy_lineplot(journeys_clean)), "NULL")
expect_identical(
class(oy_lineplot(journeys_clean, weekdays = TRUE)), "NULL"
)
expect_error(oy_lineplot(data = 1234))
expect_error(oy_lineplot(data = "a string"))
expect_error(oy_lineplot(data = not_an_object))
expect_error(oy_lineplot(data = FALSE))
})

test_that("an invalid y_var argument causes an error", {
expect_error(oy_lineplot(journeys_clean, y_var = "Not a real Y var"))
expect_error(oy_lineplot(journeys_clean, x_var = "invalid"))
})

test_that("an invalid y_var argument causes an error", {
expect_error(oy_lineplot(journeys_clean, y_var = "invalid"))
})

test_that("an invalid weekdays argument causes an error", {
expect_error(oy_lineplot(journeys_clean, weekdays = "Monday"))
expect_error(oy_lineplot(journeys_clean, weekdays = "invalid"))
})

test_that("an invalid mode argument causes an error", {
expect_error(oy_lineplot(journeys_clean, mode = "Hovercraft"))
expect_error(oy_lineplot(journeys_clean, mode = "Hovercraft"))
})
22 changes: 12 additions & 10 deletions tests/testthat/test-read.R
@@ -1,5 +1,15 @@
context("test-read")

test_that("output is a data frame with specific columns", {
data <- oy_read("../testdata/valid")
columns <- c("Date", "Start.Time", "End.Time", "Journey.Action", "Charge",
"Credit", "Balance", "Note")
expect_identical(class(data), "data.frame")
expect_identical(typeof(data), "list")
expect_length(data, 8)
expect_identical(names(data), columns)
})

test_that("an invalid input path results in an error", {
expect_error(oy_read(1234))
expect_error(oy_read("not a valid filepath"))
Expand All @@ -9,14 +19,6 @@ test_that("an invalid input path results in an error", {

test_that("reading from a folder with an invalid CSV results in a warning", {
expect_warning(oy_read("../testdata/invalid"))
})

test_that("output is a data frame with specific columns", {
data <- oy_read("../testdata/valid")
columns <- c("Date", "Start.Time", "End.Time", "Journey.Action", "Charge",
"Credit", "Balance", "Note")
expect_identical(class(data), "data.frame")
expect_identical(typeof(data), "list")
expect_length(data, 8)
expect_identical(names(data), columns)
expect_warning(oy_read("../testdata/invalid_multi"))
expect_error(oy_read("../testdata/invalid_empty"))
})
17 changes: 11 additions & 6 deletions tests/testthat/test-summary.R
@@ -1,16 +1,21 @@
context("test-summary")

test_that("an invalid data object results in an error", {
test_that("input data object returns expected ouput", {
expect_identical(class(oy_summary(journeys_clean)), "list")
expect_error(oy_summary(data = 1234))
expect_error(oy_summary(data = "a string"))
expect_error(oy_summary(data = not_an_object))
expect_error(oy_summary(data = FALSE))
})

test_that("a valid object outputs a list", {
expect_identical(class(oy_summary(journeys_clean)), "list")
})

test_that("an invalid mode argument causes an error", {
test_that("input mode argument returns expcted output", {
expect_error(oy_summary(journeys_clean, mode = "Hovercraft"))
expect_identical(
class(oy_summary(journeys_clean, mode = "Train")),
"list"
)
expect_identical(
class(oy_summary(journeys_clean, mode = "Bus")),
"list"
)
})

0 comments on commit ade60d1

Please sign in to comment.