tests/testthat/test-unit_tests.R

context("test-unit_tests")
library(testthat)
library(magrittr)

test_data <- paste0("https://www.ngdc.noaa.gov/",
                    "nndc/struts/results?type_0=Exact&",
                    "signif.txtquery_0=$ID&t=101650&s=13&",
                    "d=189&dfn=signif.txt") %>%
    readr::read_delim(delim = "\t")

test_that("eq_datemaker makes dates", {
    expect_equal(eq_datemaker(2000, 11, 25),
                 lubridate::ymd('2000-11-25'))
})

test_that("eq_location_clean cleans locations", {
    expect_equal(eq_location_clean("Canada: Land of the free"),
                 "Land Of The Free")
})

test_that("eq_clean_data returns a dataframe", {
    expect_is(eq_clean_data(test_data),
              "data.frame")

})

test_that("geom_timeline returns a ggplot2 object", {
    expect_is(test_data %>%
                  eq_clean_data() %>%
                  ggplot2::ggplot(ggplot2::aes(x = DATE)) +
                  geom_timeline(),
              "gg")
})

test_that("geom_timeline_label returns a ggplot2 object", {
    expect_is(test_data %>%
                  eq_clean_data() %>%
                  ggplot2::ggplot(ggplot2::aes(x = DATE)) +
                  geom_timeline() +
                  geom_timeline_label(aes(label = LOCATION_NAME),
                                      n_max = 5),
              "gg")
})


test_that("eq_map returns a leaflet object", {
    expect_is(test_data %>%
                  eq_clean_data() %>%
                  dplyr::filter(COUNTRY == "JAPAN" &
                                    lubridate::year(DATE) >= 2000) %>%
                  dplyr::mutate(popup_text = eq_create_label(.)) %>%
                  eq_map(annot_col = "popup_text"),
              "leaflet")
})

test_that("eq_create_label returns an html string containing break", {
    expect_gt(test_data %>%
                  eq_clean_data() %>%
                  dplyr::filter(COUNTRY == "JAPAN" &
                                    lubridate::year(DATE) >= 2000) %>%
                  eq_create_label(.) %>%
                  grepl("<br/>",.) %>%
                  sum(),
              0)
})
jdallmann/NOAAviz documentation built on Nov. 4, 2019, 2:35 p.m.