tests/testthat/test-count.R

## require(httptest)
## start_capturing()
## media_ids = c(19831, 38697)
## res_ahr <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids)
## res_ahr <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split = FALSE)
## res_ahr <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split_period = "week")
## res_kw <- count_stories(text = "klimawandel", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids)
## tags <- count_tags(text = "covid", media_id = media_ids, n = 25, after_date = "2020-01-01")
## stop_capturing()

with_mock_api({
    test_that("count_stories, split = TRUE", {
        skip_if(!dir.exists("api.mediacloud.org"))
        media_ids = c(19831, 38697)
        expect_error(count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split_period = "whatever"))
        expect_error(ahr1 <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids), NA)
        expect_error(ahr2 <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split_period = "week"), NA)
        expect_true("tbl_df" %in% class(ahr1))
        expect_true("tbl_df" %in% class(ahr2))
        expect_true(nrow(ahr1) > nrow(ahr2))
        expect_error(ahr1 <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, tibble = FALSE), NA)
        expect_error(ahr2 <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split_period = "week", tibble = FALSE), NA)
        expect_false("tbl_df" %in% class(ahr1))
        expect_false("tbl_df" %in% class(ahr2))
        expect_true(length(ahr1$count) > length(ahr2$count))
    })
})

with_mock_api({
    test_that("count_stories, split = FALSE", {
        skip_if(!dir.exists("api.mediacloud.org"))
        media_ids = c(19831, 38697)
        expect_error(count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split = FALSE, split_period = "whatever"), NA)
        res_ahr <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split = FALSE)
        expect_true("tbl_df" %in% class(res_ahr))
        expect_equal(nrow(res_ahr), 1)
        res_ahr <- count_stories(text = "ahr", after_date = "2021-01-01", before_date = "2021-12-22", media_id = media_ids, split = FALSE, tibble = FALSE)
        expect_false("tbl_df" %in% class(res_ahr))
        expect_equal(length(res_ahr), 1)
    })
})

with_mock_api({
    test_that("count_tag", {
        skip_if(!dir.exists("api.mediacloud.org"))
        media_ids = c(19831, 38697)
        expect_error(tags <- count_tags(text = "covid", media_id = media_ids, n = 25, after_date = "2020-01-01"), NA)
        expect_true("tbl_df" %in% class(tags))
        expect_error(tags <- count_tags(text = "covid", media_id = media_ids, n = 25, after_date = "2020-01-01", tibble = FALSE), NA)
        expect_false("tbl_df" %in% class(tags))
    })
})
joon-e/mediacloud documentation built on Jan. 8, 2022, 12:04 a.m.