Skip to content

Commit

Permalink
Merge pull request #47 from quandl/AP-1780/handle-empty-response
Browse files Browse the repository at this point in the history
set column names for empty dataframe properly
  • Loading branch information
A-Scott-Rowe committed Apr 18, 2016
2 parents 178b3cc + 4df9eb8 commit 5a971dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
18 changes: 18 additions & 0 deletions R/Quandldatatable.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ Quandl.datatable <- function(code, paginate=FALSE, ...) {
"https://github.com/quandl/quandl-r/blob/master/README.md#datatables"), call. = FALSE)
}

df <- quandl.datatable.set_df_columns(df, columns)

return(df)
}

quandl.datatable.set_df_columns <- function(df, columns) {
ncols <- length(columns[,1])
# if df is empty create an empty df with ncolumns set
# or else we won't be able to set the column names
if (nrow(df) <= 0 && ncols > 0) {
df <- data.frame(matrix(ncol = ncols, nrow = 0))
}

# set column names
names(df) <- columns[,1]

# set column types
df <- quandl.datatable.convert_df_columns(df, columns[,2])

return(df)
Expand All @@ -63,6 +79,8 @@ quandl.datatable.convert_df_columns <- function(df, column_types) {
df[,i] <- as.numeric(df[,i])
} else if (grepl("^date", column_types[i])) {
df[,i] <- as.Date(df[,i])
} else {
df[,i] <- as.character(df[,i])
}
}
return(df)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-datatable-helper.r
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ mock_datatable_data <- function(cursor_id = 'null') {
mock_data <- gsub("\"#cursor_id\"", cursor_id, mock_data)
return(mock_data)
}

mock_empty_datatable_data <- function() {
mock_data <- "{\"datatable\":
{\"data\": [],
\"columns\":[{\"name\":\"ticker\",\"type\":\"String\"},
{\"name\":\"oper_income\",\"type\":\"BigDecimal(12,4)\"},
{\"name\":\"comm_share_holder\",\"type\":\"Integer\"},
{\"name\":\"per_end_date\",\"type\":\"Date\"}]},
\"meta\":{\"next_cursor_id\": null
}
}"
return(mock_data)
}
20 changes: 19 additions & 1 deletion tests/testthat/test-datatable.r
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ with_mock(
data <- Quandl.datatable('ZACKS/FC')
expect_equal(names(data), c("ticker", "oper_income", "comm_share_holder", "per_end_date"))
}),
test_that("response data columns are convereted to proper data types", {
test_that("response data columns are converted to proper data types", {
data <- Quandl.datatable('ZACKS/FC')
expect_is(data[,1], "character")
expect_is(data[,2], "numeric")
Expand All @@ -91,4 +91,22 @@ with_mock(
})
)

context("Quandl.datatable() empty data response")
with_mock(
`httr::VERB` = function(http, url, config, body, query) {
mock_response(content = mock_empty_datatable_data())
},
`httr::content` = function(response, as="text") {
response$content
},
test_that("empty response data columns are converted to proper data types", {
data <- Quandl.datatable('ZACKS/FC')
expect_equal(nrow(data), 0)
expect_is(data[,1], "character")
expect_is(data[,2], "numeric")
expect_is(data[,3], "numeric")
expect_is(data[,4], "Date")
})
)

reset_config()

0 comments on commit 5a971dd

Please sign in to comment.