test_that("The new data must be a data frame", {
  expect_error(
    SCD2(
      newData = 1,
      currentData = tibble())
  )
})

test_that("The current data must be a data frame", {
  expect_error(
    SCD2(
      newData = tibble(),
      currentData = 1)
  )
})

test_that("The key must be a vector", {
  expect_error(
    SCD2(
      newData = tibble(),
      currentData = tibble(),
      key = 1)
  )
})

test_that("A data frame must be returned", {
  newData_ <- .activate(dataframe = iris)
  currentData_ <- .deactivate(dataframe = newData_)

  checkmate::expect_data_frame(
    SCD2(newData = newData_, currentData = currentData_))
})

test_that("The START_DATE column is present", {
  newData_ <- .activate(dataframe = iris)
  currentData_ <- .deactivate(dataframe = newData_)

  expect_true(any(names(SCD2(
    newData = newData_,
    currentData = currentData_
  )) == "START_DATE"))
})

test_that("The END_DATE column is present", {
  newData_ <- .activate(dataframe = iris)
  currentData_ <- .deactivate(dataframe = newData_)

  expect_true(any(names(SCD2(
    newData = newData_,
    currentData = currentData_
  )) == "END_DATE"))
})

test_that("The IS_ACTIVE column is present", {
  newData_ <- .activate(dataframe = iris)
  currentData_ <- .deactivate(dataframe = newData_)

  expect_true(any(names(SCD2(
    newData = newData_,
    currentData = currentData_
  )) == "IS_ACTIVE"))
})

test_that("SCD2 gets activated", {
  newData_ <- .activate(dataframe = iris)
  currentData_ <- .deactivate(dataframe = newData_)

  mergedData_ <- SCD2(newData = newData_, currentData = currentData_)

  expect_true(all(unique(mergedData_$IS_ACTIVE) %in% c(TRUE, FALSE)))
})

test_that("The function must output as expected", {
  currentData_ <- tibble::tribble(
    ~id, ~colA, ~colB, ~colC,
    1, "a1", "b1", "c1",
    2, "a2", "b2", "c2",
    3, "a3", "b3", "c3"
  )
  currentData_ <- .activate(dataframe = currentData_)

  newData_ <- tibble::tribble(
    ~id, ~colA, ~colB, ~colC,
    1, "a1", "b1", "c1",  # Identical row.
    2, "a2", "b2", "c20", # Almost identical row.
    3, "a4", "b4", "c4"   # Different row.
  )

  mergedData_ <- SCD2(newData = newData_, currentData = currentData_)

  expectedResult_ <- tibble::tribble(
    ~id, ~colA, ~colB, ~colC, ~IS_ACTIVE,
    1, "a1", "b1", "c1", TRUE,
    2, "a2", "b2", "c20", TRUE,
    3, "a4", "b4", "c4", TRUE,
    2, "a2", "b2", "c2", FALSE,
    3, "a3", "b3", "c3", FALSE) |>
  dplyr::arrange(id)

  arrangedMergedData_ <- mergedData_ |>
    dplyr::select(-ends_with("DATE")) |>
    dplyr::arrange(id)

  expect_equal(arrangedMergedData_, expectedResult_)
})
