Skip to content

Commit

Permalink
Avoid column name transform changing column classes #235 + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
layik committed Jul 27, 2024
1 parent 90cf166 commit fbcc832
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ format_stats19 = function(x, type) {
stats19::stats19_schema$variable_formatted == lkp_name,
c("code", "label")
]
original_class = class(x[[i]])
x[[i]] = lookup$label[match(x[[i]], lookup$code)]
x[[i]] = as(x[[i]], original_class)
}

date_in_names = "date" %in% names(x)
Expand Down Expand Up @@ -111,6 +113,7 @@ format_stats19 = function(x, type) {
x
}


#' Format column names of raw STATS19 data
#'
#' This function takes messy column names and returns clean ones that work well with
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-read_stats19.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ test_that("read_casualties works", {
)
expect_equal(nrow(read_formatted), nrow(raw_read))
})

test_that("col types are set", {
skip_download()
skip_on_cran()
cas = get_stats19(year = 2019, type = "cas")
veh = get_stats19(year = 2019, type = "veh")
# print(nrow(cas))
# print("class(veh$age_of_vehicle)")
# print(class(veh$age_of_vehicle))
# print("class(cas$age_of_casualty)")
# print(class(cas$age_of_casualty))
expect_type(cas$age_of_casualty, "integer")
expect_type(veh$age_of_vehicle, "integer")
})

0 comments on commit fbcc832

Please sign in to comment.