tests/testthat/test-crosswalk_basketball.R

test_that(".bb_normalize_college_team gives a consistent contracting key", {
  skip_on_cran()
  expect_equal(.bb_normalize_college_team("Missouri State"), .bb_normalize_college_team("Missouri St."))
  expect_equal(.bb_normalize_college_team("Texas A&M"), "texas a and m")
  expect_equal(.bb_normalize_college_team("Saint Mary's"), .bb_normalize_college_team("St. Mary's"))
})

test_that(".bb_normalize_name strips accents, suffixes, and punctuation", {
  skip_on_cran()
  expect_equal(.bb_normalize_name("LeBron James"), "lebron james")
  expect_equal(.bb_normalize_name("De'Aaron Fox"), "deaaron fox")
  expect_equal(.bb_normalize_name("Nikola Jokić"), "nikola jokic")
  expect_equal(.bb_normalize_name("Karl-Anthony Towns"), "karl anthony towns")
  expect_equal(.bb_normalize_name("Gary Payton II"), "gary payton")
  expect_equal(.bb_normalize_name("  Luka   Dončić  "), "luka doncic")
  expect_equal(.bb_normalize_name(NA_character_), "")
})

test_that(".bb_normalize_team normalizes city+name strings", {
  skip_on_cran()
  expect_equal(.bb_normalize_team("Los Angeles Lakers"), "los angeles lakers")
  expect_equal(.bb_normalize_team("The Thunder"), "thunder")
  expect_equal(
    .bb_normalize_team("Golden State  Warriors"), "golden state warriors"
  )
})

test_that(".bb_to_eastern returns the correct local ET game date", {
  skip_on_cran()
  expect_equal(.bb_to_eastern("2024-12-26T01:30:00Z"), as.Date("2024-12-25"))
  expect_equal(.bb_to_eastern("2024-12-26T16:00:00Z"), as.Date("2024-12-26"))
  expect_equal(.bb_to_eastern(as.Date("2024-12-25")), as.Date("2024-12-25"))
  expect_true(is.na(.bb_to_eastern(NA_character_)))
})

test_that(".bb_fuzzy_match returns typed empty frame when left has zero rows", {
  skip_on_cran()
  left  <- data.frame(.block    = character(),
                      .id       = character(),
                      .name_key = character(),
                      stringsAsFactors = FALSE)
  right <- data.frame(.block    = character(),
                      .id       = character(),
                      .name_key = character(),
                      stringsAsFactors = FALSE)
  m <- .bb_fuzzy_match(left, right)
  expect_s3_class(m, "data.frame")
  expect_equal(nrow(m), 0)
  expect_true(all(c(".block", "left_id", "right_id",
                    "match_method", "match_confidence") %in% names(m)))
})

test_that(".bb_fuzzy_match does not match empty name keys", {
  skip_on_cran()
  left <- data.frame(
    .block    = c("A", "A"),
    .id       = c("L1", "L2"),
    .name_key = c("", ""),
    stringsAsFactors = FALSE
  )
  right <- data.frame(
    .block    = c("A", "A"),
    .id       = c("R1", "R2"),
    .name_key = c("", ""),
    stringsAsFactors = FALSE
  )
  m <- .bb_fuzzy_match(left, right, min_confidence = 0.92)
  expect_equal(nrow(m), 2)
  expect_true(all(is.na(m$right_id)))
  expect_true(all(m$match_method == "unmatched"))
})

test_that(".bb_fuzzy_match does exact, fuzzy, tiebreak, and unmatched", {
  skip_on_cran()
  left <- data.frame(
    .block = c("A", "A", "A", "B"),
    .id = c("L1", "L2", "L3", "L4"),
    .name_key = c(
      "lebron james", "anthony davis", "deaaron fox", "stephen curry"
    ),
    .jersey = c("23", "3", "5", "30"), stringsAsFactors = FALSE)
  right <- data.frame(
    .block = c("A", "A", "A"),
    .id = c("R1", "R2", "R3"),
    .name_key = c("lebron james", "anthony davis", "deaaron foxx"),
    .jersey = c("23", "3", "5"), stringsAsFactors = FALSE)
  m <- .bb_fuzzy_match(left, right, min_confidence = 0.92)
  expect_equal(m$right_id[m$left_id == "L1"], "R1")
  expect_equal(m$match_method[m$left_id == "L1"], "exact_name")
  expect_equal(m$right_id[m$left_id == "L3"], "R3")
  expect_equal(m$match_method[m$left_id == "L3"], "fuzzy_jw")
  expect_true(is.na(m$right_id[m$left_id == "L4"]))
  expect_equal(m$match_method[m$left_id == "L4"], "unmatched")
})

Try the hoopR package in your browser

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

hoopR documentation built on Aug. 25, 2026, 9:07 a.m.