Skip to content

Commit

Permalink
fix: problem when events in log didn't have details and adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Sep 1, 2023
1 parent 6ce9bf4 commit bb5a8a9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/data-storage.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ DataStorage <- R6::R6Class( # nolint object_name_linter
checkmate::assert_data_frame(x)
checkmate::assert_string(column_name)

if (is.null(x[[column_name]])) {
return(x)
}

x[[column_name]] <- x[[column_name]] %>%
purrr::map(
function(.x) {
Expand Down
64 changes: 64 additions & 0 deletions tests/testthat/test-data_storage-logfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,67 @@ test_that("[LogFile] DataStorage should be able to insert and read", {

test_common_data_storage(data_storage)
})

test_that("[LogFile] DataStorage should be able to insert and read events without details", {
log_file_path <- tempfile(fileext = ".txt")
withr::defer(file.remove(log_file_path))

data_storage <- DataStorageLogFile$new(log_file_path = log_file_path)
withr::defer(data_storage$close())

data_storage$insert(
app_name = "app_name",
type = "without_session"
) %>% expect_silent()

data_storage$read_event_data() %>%
expect_silent() %>%
NROW() %>%
expect_equal(1)
})

test_that("[LogFile] DataStorage should be able to insert and read custom fields with length > 1", {
log_file_path <- tempfile(fileext = ".txt")
withr::defer(file.remove(log_file_path))

data_storage <- DataStorageLogFile$new(log_file_path = log_file_path)
withr::defer(data_storage$close())


data_storage$insert(
app_name = "app_name",
type = "without_session"
) %>% expect_silent()

data_storage$insert(
app_name = "app_name",
type = "click",
details = list(id = "some_button_id_2"),
session = "some_session_id"
) %>% expect_silent()

data_storage$insert(
app_name = "app_name",
type = "click",
details = list(id = "vector_selected", value = 1:10, custom = 2),
session = "some_session_id"
) %>% expect_silent()

result <- data_storage$read_event_data() %>%
expect_silent()

result %>%
dplyr::filter(
id == "vector_selected"
) %>%
purrr::pluck("value") %>%
expect_type("character")

result %>%
dplyr::filter(
id == "vector_selected"
) %>%
purrr::pluck("value") %>%
unname() %>%
expect_equal(format(paste(1:10, collapse = ", ")))
})

0 comments on commit bb5a8a9

Please sign in to comment.