Skip to content

Commit

Permalink
test: Add test for convert_to_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
MEO265 committed May 3, 2024
1 parent 041fc66 commit 11e9ed3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ find_call <- function() {
convert_to_csv <- function (file, logfile = get_logfile(), remove_message_lf = TRUE, ...) {
log <- read_logs(logfile = logfile)

if(!remove_message_lf) {
if(remove_message_lf) {
msg_flag <- log$log_lvl == "INFO"
msg <- log$log_msg[msg_flag]
log$log_msg[msg_flag] <- gsub("\n$", "", msg)
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/utils/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"timestamp" "log_lvl" "log_msg" "extra"
"1" "2024-05-01T20:09:27+0200" "INFO" "log_1" "extra
"
"2" "2024-05-01T20:09:27+0200" "WARN" "log_96
" NA
"3" "2024-05-01T20:09:27+0200" "INFO" "log_97" NA
"4" "2024-05-01T20:09:27+0200" "INFO" "log_98" NA
"5" "2024-05-01T20:09:27+0200" "INFO" "log_99" NA
"6" "2024-05-01T20:09:27+0200" "INFO" "log_100 " "extra"
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/utils/test_with_lf.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"timestamp" "log_lvl" "log_msg" "extra"
"1" "2024-05-01T20:09:27+0200" "INFO" "log_1" "extra
"
"2" "2024-05-01T20:09:27+0200" "WARN" "log_96
" NA
"3" "2024-05-01T20:09:27+0200" "INFO" "log_97" NA
"4" "2024-05-01T20:09:27+0200" "INFO" "log_98" NA
"5" "2024-05-01T20:09:27+0200" "INFO" "log_99" NA
"6" "2024-05-01T20:09:27+0200" "INFO" "log_100
" "extra"
20 changes: 19 additions & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ test_that("rotate_logs preserves sanitization", {
rotate_logs(rotate_lines = rotate_lines, logfile = tmp_log)
expect_snapshot_file(tmp_log)

})
})

test_that("read_logs on empty log", {
set_logfile(get_logfile(), confirm = FALSE, create = TRUE)
log_df <- read_logs()
expect_identical(log_df, data.frame(timestamp = character(), log_lvl = character(), log_msg = character()))
})
cleanup()

test_that("convert_to_csv", {
tmp_dir <- tempdir()
convert_to_csv(file = file.path(tmp_dir, "test.csv"), logfile = "testdata/test.loggit")
convert_to_csv(
file = file.path(tmp_dir, "test_with_lf.csv"), remove_message_lf = FALSE, logfile = "testdata/test.loggit"
)

expect_snapshot_file(file.path(tmp_dir, "test.csv"))
expect_snapshot_file(file.path(tmp_dir, "test_with_lf.csv"))
})
4 changes: 2 additions & 2 deletions tests/testthat/testdata/test.loggit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_1", "extra": "extra"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_96"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_1", "extra": "extra__LF__"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "WARN", "log_msg": "log_96 __LF__"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_97"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_98"}
{"timestamp": "2024-05-01T20:09:27+0200", "log_lvl": "INFO", "log_msg": "log_99"}
Expand Down

0 comments on commit 11e9ed3

Please sign in to comment.