tests/testthat/test-courseraeq.R

context("courseraeq functions testing")

data <-  data_example

#-------------------------------------------------------------------------------
# process_data
# eq_location_clean
test_that("Test eq_location_clean with different strings", {
  expect_equal(eq_location_clean("ABCDEF"), "Abcdef")
  expect_equal(eq_location_clean("ABCDEF ABCDEF"), "Abcdef Abcdef")
  expect_equal(eq_location_clean("ABCDEF: ABCDEF"), "Abcdef")
  expect_equal(eq_location_clean("ABCDEF: ABCDEF: ABCDEF"), "Abcdef: Abcdef")
})

# eq_clean_data
test_that("Test eq_clean_data to check class of columns", {
  # check classes
  expect_true(is.data.frame(data))
  expect_true(lubridate::is.Date(data$DATE))
  expect_true(is.numeric(data$LONGITUDE))
  expect_true(is.numeric(data$LATITUDE))
  expect_true(is.numeric(data$EQ_PRIMARY))
  expect_true(is.numeric(data$DEATHS))
})


#-------------------------------------------------------------------------------
# map
# eq_create_label
test_that("Test eq_create_label creates a character vector", {
  # check that the output is a character
  expect_true(is.character(eq_create_label(data)))

  # remove DEATHS column
  data_test <- data %>%
    dplyr::select(-DEATHS)

  # check that even without one column the output is a character
  expect_true(is.character(eq_create_label(data_test)))

})

# eq_map
test_that("Test eq_map creates a leaflet", {

  expect_is(eq_map(data), "leaflet")
  expect_is(eq_map(data, annot_col = "LATITUDE"), "leaflet")

})


#-------------------------------------------------------------------------------
# timeline plot
# geom_timeline
test_that("Test geom_timeline creates a ggplot object", {

  p <- ggplot2::ggplot(data) +
    geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS))

  expect_is(p, "ggplot")
  expect_is(p$layers[[1]]$geom, "GeomTimeLine")

})

# geom_timeline_label
test_that("Test geom_timeline creates a ggplot object", {

  p <- ggplot2::ggplot(data) +
    geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
    geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 5)

  expect_is(p, "ggplot")
  expect_is(p$layers[[2]]$geom, "GeomTimeLineLabel")

  p <- ggplot2::ggplot(data) +
    geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
    geom_timeline_label(aes(x = DATE, y = COUNTRY, label = LOCATION_NAME, size = EQ_PRIMARY), n_max = 8)

  expect_is(p$layers[[2]]$geom, "GeomTimeLineLabel")
})

# geom_theme
test_that("Test geom_timeline creates a ggplot object", {

  p <- ggplot2::ggplot(data) +
    geom_timeline(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, color = DEATHS)) +
    theme_timeline()

  expect_is(p, "ggplot")
  expect_is(p$layers[[1]]$geom, "GeomTimeLine")
  expect_equal(p$theme$line$colour, "black")

})
frenkg/courseraeq documentation built on May 22, 2019, 12:42 p.m.