tests/testthat/test-get_events.R

with_mock_api(
  test_that("get events works as expected", {
    expect_error(get_events(baseurl, program_id = "Ymn7R"))
    expect_error(get_events())
    d <- get_events(baseurl, program_id = "Ymn7RO0lkf7")
    expect_s3_class(d, class = "psi-mis_api")
    expect_identical(class(d$content$events), "data.frame")
    expect_type(d$endpoint, "character")
    expect_identical(class(d$response), "response")
    expect_equal(d$response$status, 200L)
  })
)




test_that("events endpoint correctly generates the url", {
  expect_error(modify_events_endpoint())
  expect_error(modify_events_endpoint(startDate = "2018-01-01", ))
  expect_error(modify_events_endpoint(baseurl = baseurl))
  expect_equal(
    modify_events_endpoint(baseurl = baseurl, program_id = "Ymn7RO0lkf7"),
    "https://clone.psi-mis.org/api/events.json?paging=true&fields=*&totalPages=true&program=Ymn7RO0lkf7&pageSize=50&page=1"
  )
  expect_equal(
    modify_events_endpoint(baseurl = baseurl, program_id = "Ymn7RO0lkf7", paging = F),
    "https://clone.psi-mis.org/api/events.json?paging=false&fields=*&program=Ymn7RO0lkf7"
  )
  expect_equal(
    modify_events_endpoint(baseurl = baseurl, program_id = "Ymn7RO0lkf7", startDate = "2018-01-01"),
    paste0(baseurl, "api/events.json?paging=true&fields=*&totalPages=true&program=Ymn7RO0lkf7&pageSize=50&page=1&startDate=2018-01-01")
  )
  expect_equal(
    modify_events_endpoint(baseurl = baseurl, program_id = "Ymn7RO0lkf7", endDate = "2018-01-01"),
    paste0(baseurl, "api/events.json?paging=true&fields=*&totalPages=true&program=Ymn7RO0lkf7&pageSize=50&page=1&endDate=2018-01-01")
  )
  expect_equal(
    modify_events_endpoint(baseurl = baseurl, program_id = "Ymn7RO0lkf7", startDate = "2018-01-01", pageSize = 100),
    paste0(baseurl, "api/events.json?paging=true&fields=*&totalPages=true&program=Ymn7RO0lkf7&pageSize=100&page=1&startDate=2018-01-01")
  )
})
psi-mis/a360connect documentation built on Nov. 22, 2022, 12:36 p.m.