Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
^RTD.Rcheck$
^.java-version$
^.github$
^SECURITY\.md$
^\.git$
^\.gitignore$
^.*\.Rcheck$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Package: RTD
Title: Simple TD API Client
Version: 0.4.1.900
Author: Aki Ariga <ariga@treasure-data.com>
Authors@R:
person(given = "Aki",
family = "Ariga",
Expand All @@ -13,7 +14,6 @@ Description:
SystemRequirements: embulk, embulk-output-td
License: Apache License 2.0 | file LICENSE
Encoding: UTF-8
LazyData: true
URL: https://github.com/treasure-data/RTD
BugReports: https://github.com/treasure-data/RTD/issues
RoxygenNote: 7.1.1
Expand Down
118 changes: 89 additions & 29 deletions tests/testthat/test-td.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,104 @@ library(mockery)
con <- Td(apikey = "xxxxx")
embulk_exec <- if (.Platform$OS.type == "windows") "embulk.bat" else "embulk"

# Setup fake embulk once for all tests
setup_fake_embulk <- function() {
# Create a temporary directory with a fake embulk
fake_embulk_dir <- tempfile()
dir.create(fake_embulk_dir, recursive = TRUE)

# Create platform-specific fake embulk
if (.Platform$OS.type == "windows") {
fake_embulk <- file.path(fake_embulk_dir, "embulk.bat")
writeLines("@echo off\necho fake embulk", fake_embulk)
} else {
fake_embulk <- file.path(fake_embulk_dir, "embulk")
writeLines("#!/bin/bash\necho 'fake embulk'", fake_embulk)
Sys.chmod(fake_embulk, mode = "0755")
}

# Temporarily modify PATH
old_path <- Sys.getenv("PATH")
path_sep <- if (.Platform$OS.type == "windows") ";" else ":"
Sys.setenv(PATH = paste(fake_embulk_dir, old_path, sep = path_sep))

# Return cleanup function
function() {
Sys.setenv(PATH = old_path)
unlink(fake_embulk_dir, recursive = TRUE)
}
}

# TODO: test for "bulk_import" mode
test_that("td_upload works with mock", {
template_path <- system.file("extdata", "tsv_upload.yml.liquid", package = "RTD")
m <- mock(0, 0)
with_mock(
exist_database = mock(FALSE),
exist_table = mock(FALSE),
create_database = mock(TRUE),
create_table = mock(TRUE),
delete_table = mock(TRUE),
tempdir = mock("/tmp"),
`readr::write_tsv` = mock(TRUE),
`Sys.which` = mock("/home/RTD/bin/embulk"),
`system2` = m, {
td_upload(con, "test", "iris", iris, mode = "embulk")
}

# Use local_mocked_bindings for functions that exist
local_mocked_bindings(
exist_database = function(...) FALSE,
exist_table = function(...) FALSE,
create_database = function(...) TRUE,
create_table = function(...) TRUE,
delete_table = function(...) TRUE,
.package = "RTD"
)

# Mock tempdir to return platform-appropriate path
temp_base <- if (.Platform$OS.type == "windows") "C:/tmp" else "/tmp"
local_mocked_bindings(
tempdir = function(...) temp_base,
system2 = m,
.package = "base"
)

local_mocked_bindings(
write_tsv = function(...) TRUE,
.package = "readr"
)
expect_args(m, 1, embulk_exec, paste("guess", template_path, "-o /tmp/load.yml"))
expect_args(m, 2, embulk_exec, "run /tmp/load.yml")

# Setup fake embulk and ensure cleanup
cleanup_embulk <- setup_fake_embulk()
on.exit(cleanup_embulk())

td_upload(con, "test", "iris", iris, mode = "embulk")
expect_args(m, 1, embulk_exec, paste("guess", template_path, paste0("-o ", temp_base, "/load.yml")))
expect_args(m, 2, embulk_exec, paste0("run ", temp_base, "/load.yml"))
})

test_that("td_upload works with mock when the table already exists", {
template_path <- system.file("extdata", "tsv_upload.yml.liquid", package = "RTD")
m <- mock(0, 0)
with_mock(
exist_database = mock(FALSE, cycle = TRUE),
exist_table = mock(TRUE, cycle = TRUE),
create_database = mock(TRUE, cycle = TRUE),
create_table = mock(TRUE, cycle = TRUE),
delete_table = mock(TRUE, cycle = TRUE),
tempdir = mock("/tmp", cycle = TRUE),
`readr::write_tsv` = mock(TRUE, cycle = TRUE),
`Sys.which` = mock("/home/RTD/bin/embulk", cycle = TRUE),
`system2` = m, {
expect_error(td_upload(con, "test", "iris", iris), ".* already exists.")
td_upload(con, "test", "iris", iris, mode = "embulk", overwrite = TRUE)
}

# Use local_mocked_bindings for functions that exist
local_mocked_bindings(
exist_database = function(...) FALSE,
exist_table = function(...) TRUE,
create_database = function(...) TRUE,
create_table = function(...) TRUE,
delete_table = function(...) TRUE,
.package = "RTD"
)

# Mock tempdir to return platform-appropriate path
temp_base <- if (.Platform$OS.type == "windows") "C:/tmp" else "/tmp"
local_mocked_bindings(
tempdir = function(...) temp_base,
system2 = m,
.package = "base"
)
expect_args(m, 1, embulk_exec, paste("guess", template_path, "-o /tmp/load.yml"))
expect_args(m, 2, embulk_exec, "run /tmp/load.yml")

local_mocked_bindings(
write_tsv = function(...) TRUE,
.package = "readr"
)

# Setup fake embulk and ensure cleanup
cleanup_embulk <- setup_fake_embulk()
on.exit(cleanup_embulk())

expect_error(td_upload(con, "test", "iris", iris), ".* already exists.")
td_upload(con, "test", "iris", iris, mode = "embulk", overwrite = TRUE)
expect_args(m, 1, embulk_exec, paste("guess", template_path, paste0("-o ", temp_base, "/load.yml")))
expect_args(m, 2, embulk_exec, paste0("run ", temp_base, "/load.yml"))
})
Loading