tests/testthat/test-add_sector_and_borderline.R

library(r2dii.data)
# TODO: Instead of `loanbook_demo`, can use fake_lbk() everywhere?
test_that("$borderline is of type logical", {
  expect_type(
    add_sector_and_borderline(loanbook_demo)$borderline,
    "logical"
  )
})

test_that("outputs expected value", {
  skip_if(packageVersion("r2dii.data") <= "0.1.4", "We expect different output")
  expect_snapshot_value(add_sector_and_borderline(loanbook_demo), style = "json2")
})

test_that("returns a tibble data frame", {
  expect_s3_class(
    add_sector_and_borderline(loanbook_demo),
    "tbl_df"
  )
})

test_that("adds two columns: `sector` and `borderline`", {
  expect_false(has_name(loanbook_demo, "sector"))
  expect_false(has_name(loanbook_demo, "borderline"))

  out <- add_sector_and_borderline(loanbook_demo)
  new_columns <- sort(setdiff(names(out), names(loanbook_demo)))
  expect_equal(
    new_columns, c("borderline", "sector")
  )
})

test_that("added columns return acceptable values", {
  acceptable_sectors <- c(
    "automotive",
    "aviation",
    "cement",
    "coal",
    "code not found",
    "not in scope",
    "oil and gas",
    "power",
    "shipping",
    "steel"
  )

  expect_true(
    all(
      add_sector_and_borderline(loanbook_demo)$sector %in% acceptable_sectors
    )
  )
})

test_that("preserves typeof() input columns", {
  out <- add_sector_and_borderline(loanbook_demo)

  x <- unname(purrr::map_chr(loanbook_demo, typeof))
  y <- unname(purrr::map_chr(out[names(loanbook_demo)], typeof))
  expect_equal(x, y)
})

test_that("outputs no missing value of `sector`", {
  out <- add_sector_and_borderline(loanbook_demo)
  expect_false(any(is.na(out$sector)))
})

test_that("outputs no missing value of `borderline`", {
  out <- add_sector_and_borderline(loanbook_demo)
  expect_false(any(is.na(out$borderline)))
})

Try the r2dii.match package in your browser

Any scripts or data that you put into this service are public.

r2dii.match documentation built on Oct. 23, 2023, 5:09 p.m.