tests/testthat/test-sqlite-cache.R

context("SqliteCache")

test_that("Simple get/set/delete works correctly", {
  sc <- SqliteCache$new()

  sc$set(1L, mtcars)
  expect_equal(sc$get(1L), mtcars)

  sc$delete(1L)
  expect_null(sc$get(1L))

  expect_null(sc$get(2))
  expect_identical(sc$get(3, default = 1), 1)

  sc$destroy()
})

test_that("Keys correctly removed after expiration", {
  sc <- SqliteCache$new()

  all_equal <- TRUE
  for (i in seq(10)) {
    # expire after 0.1sec
    sc$set(i, i, expire_in = 0.1)

    # at this point the keys are not expired
    all_equal <- (sc$get(i) == i)
  }

  # before expiration, all should be correctly cached
  expect_true(all_equal)

  # sleep for 0.2sec, all keys should expire in that time
  Sys.sleep(time = 0.2)

  # key should be expired now and return default (=NULL)
  expect_null(sc$get(2L))

  # trim should remove all rows from the cache
  sc$trim()
  expect_equal(nrow(sc$print_table()), 0)

  sc$destroy()
})
skubicius/cashmere documentation built on May 22, 2019, 2:46 p.m.