Skip to content

Commit

Permalink
colearendt#12 handle long deparse; breaking tests for mixed type arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy committed Sep 1, 2016
1 parent 34fffa0 commit 06d7576
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ importFrom(igraph,plot.igraph)
importFrom(magrittr,"%>%")
importFrom(purrr,"%||%")
importFrom(purrr,at_depth)
importFrom(purrr,compose)
importFrom(purrr,flatten_chr)
importFrom(purrr,flatten_dbl)
importFrom(purrr,invoke_map)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map2_chr)
importFrom(purrr,map_chr)
importFrom(purrr,map_dbl)
importFrom(purrr,map_if)
importFrom(purrr,map_int)
importFrom(purrr,map_lgl)
importFrom(purrr,partial)
8 changes: 5 additions & 3 deletions R/json_schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ json_schema <- function(x) {
structure <- x %>% json_structure %>% tbl_df

# Change all array indices to 1L in sequence to unify arrays
# Also create an id that is the call to construct the seq
structure <- structure %>%
mutate(seq = seq %>% at_depth(2, . %>% map_if(is.numeric, pmin, 1))) %>%
select(seq, key, type) %>%
unique %>%
mutate(seq.id = seq %>% map_chr(deparse)) %>%
unique

# Also create an id that is the call to construct the seq
structure <- structure %>%
mutate(seq.id = seq %>% map_chr(compose(partial(paste0, collapse = ""), deparse))) %>%
mutate(order = 1:n())

# Which seq.ids are entirely null?
Expand Down
3 changes: 2 additions & 1 deletion R/tidyjson-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' @import assertthat
#' @import jsonlite
#' @importFrom purrr map map_lgl map_dbl map_int map_chr map2 %||% invoke_map
#' @importFrom purrr map_if at_depth flatten_chr flatten_dbl
#' @importFrom purrr map_if at_depth flatten_chr flatten_dbl compose partial
#' @importFrom purrr map2_chr
#' @import dplyr
#' @import tidyr
#' @importFrom igraph graph_from_data_frame plot.igraph V layout_with_kk
Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-json_schema.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,55 @@ test_that("json_schema works for a more complex example", {
)

})

test_that("works for empty arrays", {

expect_identical(json_schema('[]'), '[]')
expect_identical(json_schema('{"key": []}'), '{"key": []}')

})

test_that("works for complex nested types", {

# object x object
expect_identical(
json_schema('{"k1": {"k2": null}}'),
'{"k1": {"k2": "null"}}')
# object x array
expect_identical(
json_schema('{"k1": [1, 2]}'),
'{"k1": ["number"]}')
# array x object
expect_identical(
json_schema('[{"k1": null, "k2": null}]'),
'[{"k1": "null", "k2": "null"}]')
# array x array
expect_identical(
json_schema('[[1, 2], [1, 2]]'),
'[["number"]]')

})

test_that("simple mixed type array", {

expect_identical('["a", 1, true, null]' %>% json_schema,
'["logical", "number", "string"]')

})


test_that("problem with mixed type arrays", {

expect_identical('[[1,2], "a"] ' %>% json_schema,
'[["number"], "string"]')

})


test_that("json_schema works for a very complex example", {

expect_success(
json_schema(companies[1])
)

})

0 comments on commit 06d7576

Please sign in to comment.