tests/testthat/test-process.R

context("process")

library(turnr)

# calc active instruments -------------------------------------------------

test_that("check calc active instruments", {
    df <- pre_process(rp_raw) %>%
        filter(date != "2018-01-01") # making dates non-contiguous

    # wide window used so pick up all serial numbers
    test <- calc_active_instruments(df, window = 700)
    diffs <- test %>%
        group_by(SiteID, InstrumentVersion) %>%
        arrange(date) %>%
        tidyr::nest() %>%
        mutate(diffs = purrr::map(data, function(x) diff(x$date))) %>%
        pull(diffs) %>%
        unlist()
    # check if all dates consecutive
    expect_equal(unique(diffs), 1)

    # total unique serial numbers (may fail if test data changed)
    num_serial <- test %>%
        filter(date == max(date), InstrumentVersion == "all") %>%
        pull(n_active) %>%
        sum()
    expect_equal(num_serial, 56)

    # unique serial number on first day
    first_num_serial <- rp_raw %>%
        pre_process() %>%
        filter(date == min(date)) %>%
        pull(InstrumentSerialNumber) %>%
        lu()
    expect_equal(test %>%
                     filter(date == min(date), InstrumentVersion == "all") %>%
                     pull(n_active) %>%
                     sum(),
                 first_num_serial)

    # testing other pouchtitles
    test_GI <- pre_process(rp_raw, target_PouchTitle = "Gastro_Intestinal") %>%
        filter(date != "2018-01-01") %>%
        calc_active_instruments(window = 700,
                                target_PouchTitle = "Gastro_Intestinal")
    comb <- inner_join(test_GI, test,
                       by = c("SiteID", "date", "InstrumentVersion"))
    expect_equal(comb$daily_TUR_all.y, comb$daily_TUR_all.x)

    # the TURs of the two panels should sum to all
    expect_equal(comb$daily_TUR_rp.y + comb$daily_TUR_rp.x,
                 comb$daily_TUR_all.x)


})

test_that('calc_active_instruments missing pouch title', {
    # check that function doesn't fail if a pouch title
    # never used with a specific instrument
    df <- pre_process(rp_raw) %>%
        filter(!(InstrumentVersion == 'FA1.5' & PouchTitle == "Respiratory_Panel"))
    test <- calc_active_instruments(df) %>%
        filter(InstrumentVersion == "FA1.5")

    expect_equal(unique(test$daily_TUR_rp), 0)
})



# calc_count_by_site_inst --------------------------------------


test_that("test calc_count_by_site_inst", {
    # check sum of pathogens equals TUR:
    df <- pre_process(rp_raw)

    df_co <- co_detection(df) %>%
        calc_count_by_site_inst()

    sum_count_by_site <- df_co %>%
        group_by(date, InstrumentVersion, SiteID) %>%
        summarize(daily_count = sum(daily_count)) %>%
        ungroup()

    test <- calc_active_instruments(df) %>%
        full_join(sum_count_by_site,
                  by = c("date", "InstrumentVersion", "SiteID")) %>%
        # daily count is na when tur = 0, which is ok
        filter(!(is.na(daily_count) & daily_TUR_rp == 0))

    expect_equal(test$daily_count, test$daily_TUR_rp)

    # check for correct pathogens when run other panel

    test2 <- pre_process(rp_raw, target_PouchTitle = "Gastro_Intestinal") %>%
        co_detection(target_PouchTitle = "Gastro_Intestinal") %>%
        calc_count_by_site_inst("Gastro_Intestinal")
    GI_paths <- rp_raw %>%
        filter(PouchTitle ==  "Gastro_Intestinal") %>%
        pull(TargetName) %>%
        unique()
    GI_paths = sort(c(GI_paths, "co-detection", "negative"))
    GI_paths <- GI_paths[!stringr::str_detect(GI_paths, "[Cc]ontrol")]
    expect_equal(sort(unique(test2$TargetName)), GI_paths)
})


# calc_n_sites ------------------------------------------------------------

test_that("test calc_n_sites",{
          df <- calc_n_sites(TUR_dat)
          expect_equal(max(df$n_sites), 7)
})
MartinHoldrege/turnr documentation built on May 16, 2020, 10:39 a.m.