Skip to content

Commit

Permalink
#27 first draft tests for as.tbl_json.data.frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Stanley committed Apr 3, 2015
1 parent e2f6ce4 commit 6945f68
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions tests/testthat/test-tbl_json.r
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,59 @@ test_that("throws error on invalid json", {
}
)

context("tbl_json: as.tbl_json.data.frame")

test_that("works for a data.frame and data_frame created objects", {

df <- data.frame(
document.id = 1:2,
json = c('{"name": "bob"}', '{"name": "susan"}'),
stringsAsFactors = FALSE)
# data.frame
expect_identical(
as.tbl_json(df, json.column = "json"),
as.tbl_json(df$json)
)
# data_frame
df <- data_frame(
document.id = 1:2,
json = c('{"name": "bob"}', '{"name": "susan"}'))
expect_identical(
as.tbl_json(df, json.column = "json"),
as.tbl_json(df$json)
)

}
)

test_that("works in a pipeline", {

df <- data_frame(
age = c(32, 45),
json = c('{"name": "bob"}', '{"name": "susan"}')
)

expect_identical(
df %>% as.tbl_json(json.column = "json") %>%
spread_values(name = jstring("name")) %>%
filter(age == 32) %>%
extract2("name"),
"bob"
)

}
)

test_that("throws an error without json.column specified", {
expect_error(as.tbl_json(iris))
}
)

test_that("throws an error if json column doesn't exist", {
expect_error(as.tbl_json(iris, json.column = "json"))
}
)

context("tbl_json")

test_that("tbl_json constructor works with no data", {
Expand Down Expand Up @@ -164,7 +217,6 @@ test_that("dplyr::arrange works with a simple example", {
}
)


test_that("dplyr::mutate works with a simple example", {

x <- as.tbl_json(c('{"name": "bob"}', '{"name": "susan"}'))
Expand All @@ -185,7 +237,6 @@ test_that("dplyr::mutate works with a simple example", {
}
)


test_that("dplyr::mutate works in a more complex pipeline", {

json <- c(
Expand Down

0 comments on commit 6945f68

Please sign in to comment.