From fbcc832aaedbd553f74be24dfac94e60c0bf2899 Mon Sep 17 00:00:00 2001 From: Layik Hama Date: Sat, 27 Jul 2024 21:09:53 +0100 Subject: [PATCH] Avoid column name transform changing column classes #235 + tests --- R/format.R | 3 +++ tests/testthat/test-read_stats19.R | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/R/format.R b/R/format.R index b06324f..1d95c77 100644 --- a/R/format.R +++ b/R/format.R @@ -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) @@ -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 diff --git a/tests/testthat/test-read_stats19.R b/tests/testthat/test-read_stats19.R index c0d0230..2465f2c 100644 --- a/tests/testthat/test-read_stats19.R +++ b/tests/testthat/test-read_stats19.R @@ -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") +})