tests/testthat/test-helpers.R

#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
context("Validate functionality in creating urls for get requests")

test_url <- create_url("sources", list())

resp <- httr::GET(test_url)

if (httr::http_error(resp)) {
  stop("Newsapi request failed ", httr::status_code(resp))
  call. = FALSE
} else {
  cat("dowloading...\n")
  test_url <- jsonlite::fromJSON(httr::content(resp, "text"))
  cat("finished dowloading...\n")
}
as.character(unique(test_url[[2]][7]))


test_that("Validate content of GET request for empty list. Would break if content is changing",
  {

    expect_true(as.character(unique(test_url[[2]][5])) == 'c("general", "business", "technology", "sports", "entertainment", "health", "science")')
    expect_true(as.character(unique(test_url[[2]][6])) == 'c("en", "no", "it", "ar", "ud", "de", "pt", "es", "fr", "he", "ru", "se", "nl", "zh")')
    expect_true(as.character(unique(test_url[[2]][7])) == 'c("us", "au", "no", "it", "sa", "pk", "gb", "de", "br", "ca", "es", "ar", "fr", "in", "is", "ru", "se", "za", "ie", "nl", "zh")')
  })


test_url_general <- create_url("sources", list(category = "general"))

resp <- httr::GET(test_url_general)

if (httr::http_error(resp)) {
  stop("Newsapi request failed ", httr::status_code(resp))
  call. = FALSE
} else {
  cat("dowloading...\n")
  test_url_general <- jsonlite::fromJSON(httr::content(resp, "text"))
  cat("finished dowloading...\n")
}


test_that("Validate content of GET request for category source", {

 expect_true(unique(test_url_general[[2]][5]) == "general")
})

test_url_general_ru <- create_url("sources", list(category = "general", country = "ru"))

resp <- httr::GET(test_url_general_ru)

if (httr::http_error(resp)) {
  stop("Newsapi request failed ", httr::status_code(resp))
  call. = FALSE
} else {
  cat("dowloading...\n")
  test_url_general_ru <- jsonlite::fromJSON(httr::content(resp, "text"))
  cat("finished dowloading...\n")
}


test_that("Validate content of GET reques for category and country", {

 expect_true(unique(test_url_general_ru[[2]][5]) == "general")
 expect_true(unique(test_url_general_ru[[2]][5]) %in% "general" && unique(test_url_general_ru[[2]][6]) %in%
    c("ru"))
})



test_url_general_ru_ru <- create_url("sources", list(category = "general", country = "ru",
  language = "ru"))
resp <- httr::GET(test_url_general_ru_ru)

if (httr::http_error(resp)) {
  stop("Newsapi request failed ", httr::status_code(resp))
  call. = FALSE
} else {
  cat("dowloading...\n")
  test_url_general_ru_ru <- jsonlite::fromJSON(httr::content(resp, "text"))
  cat("finished dowloading...\n")
}


test_that("Validate content of GET request", {

 expect_true(unique(test_url_general_ru_ru[[2]][5]) == "general")
 expect_true(unique(test_url_general_ru_ru[[2]][5]) %in% "general" &&
    unique(test_url_general_ru_ru[[2]][6]) %in% c("ru"))
 expect_true(unique(test_url_general_ru_ru[[2]][5]) %in% "general" &&
    unique(test_url_general_ru_ru[[2]][6]) %in% c("ru") && unique(test_url_general_ru_ru[[2]][7]) %in%
    c("ru"))
})
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
context("Validate functionality of the newsapi validation procedure")

test_that("Validate error", {

  expect_error(newsapi_key("somekey"), "Newsapi request failed. Please validate your apikey")
})
data-atelier/newsExploreR documentation built on May 22, 2019, 11:51 p.m.