tests/testthat/test_eq_location_clean.R

context('clean location data')

df <- quakes

test_that('basic location name cleaning function works', {
  df_clean <- eq_location_clean(df)

  expect_is(df_clean$LOCATION_NAME, 'character')

  expect_equal(df_clean$LOCATION_NAME,
               df_clean$LOCATION_NAME %>%  # transform first to lower then title
                 stringr::str_to_lower() %>%
                 stringr::str_to_title())

  expect_equal(purrr::map2(df_clean$COUNTRY, df_clean$LOCATION_NAME,
                               function(cty, loc) {
                                 grep(paste0(cty, ":"), loc)
                               }) %>% unlist() %>% sum(), 0)
})

test_that('missing variables throw an error', {

  expect_error(eq_location_clean(df %>% dplyr::select(-COUNTRY)),
               'Missing required variable: COUNTRY')

  expect_error(eq_location_clean(df %>% dplyr::select(-LOCATION_NAME)),
               'Missing required variable: LOCATION_NAME')

  expect_s3_class(eq_location_clean(df %>% dplyr::select(-MINUTE)), 'tbl_df')
})
JazminCevallos/Package-Evaluation documentation built on May 27, 2019, 12:16 p.m.