Skip to content

Commit 7394ede

Browse files
authored
Merge pull request #24 from treasure-data/fix-deprecated-with-mock
2 parents 86e86ba + 908c6b7 commit 7394ede

File tree

3 files changed

+94
-30
lines changed

3 files changed

+94
-30
lines changed

.Rbuildignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212
^RTD.Rcheck$
1313
^.java-version$
1414
^.github$
15+
^SECURITY\.md$
16+
^\.git$
17+
^\.gitignore$
18+
^.*\.Rcheck$

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Package: RTD
22
Title: Simple TD API Client
33
Version: 0.4.1.900
4+
Author: Aki Ariga <[email protected]>
45
Authors@R:
56
person(given = "Aki",
67
family = "Ariga",
@@ -13,7 +14,6 @@ Description:
1314
SystemRequirements: embulk, embulk-output-td
1415
License: Apache License 2.0 | file LICENSE
1516
Encoding: UTF-8
16-
LazyData: true
1717
URL: https://github.com/treasure-data/RTD
1818
BugReports: https://github.com/treasure-data/RTD/issues
1919
RoxygenNote: 7.1.1

tests/testthat/test-td.R

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,104 @@ library(mockery)
55
con <- Td(apikey = "xxxxx")
66
embulk_exec <- if (.Platform$OS.type == "windows") "embulk.bat" else "embulk"
77

8+
# Setup fake embulk once for all tests
9+
setup_fake_embulk <- function() {
10+
# Create a temporary directory with a fake embulk
11+
fake_embulk_dir <- tempfile()
12+
dir.create(fake_embulk_dir, recursive = TRUE)
13+
14+
# Create platform-specific fake embulk
15+
if (.Platform$OS.type == "windows") {
16+
fake_embulk <- file.path(fake_embulk_dir, "embulk.bat")
17+
writeLines("@echo off\necho fake embulk", fake_embulk)
18+
} else {
19+
fake_embulk <- file.path(fake_embulk_dir, "embulk")
20+
writeLines("#!/bin/bash\necho 'fake embulk'", fake_embulk)
21+
Sys.chmod(fake_embulk, mode = "0755")
22+
}
23+
24+
# Temporarily modify PATH
25+
old_path <- Sys.getenv("PATH")
26+
path_sep <- if (.Platform$OS.type == "windows") ";" else ":"
27+
Sys.setenv(PATH = paste(fake_embulk_dir, old_path, sep = path_sep))
28+
29+
# Return cleanup function
30+
function() {
31+
Sys.setenv(PATH = old_path)
32+
unlink(fake_embulk_dir, recursive = TRUE)
33+
}
34+
}
35+
836
# TODO: test for "bulk_import" mode
937
test_that("td_upload works with mock", {
1038
template_path <- system.file("extdata", "tsv_upload.yml.liquid", package = "RTD")
1139
m <- mock(0, 0)
12-
with_mock(
13-
exist_database = mock(FALSE),
14-
exist_table = mock(FALSE),
15-
create_database = mock(TRUE),
16-
create_table = mock(TRUE),
17-
delete_table = mock(TRUE),
18-
tempdir = mock("/tmp"),
19-
`readr::write_tsv` = mock(TRUE),
20-
`Sys.which` = mock("/home/RTD/bin/embulk"),
21-
`system2` = m, {
22-
td_upload(con, "test", "iris", iris, mode = "embulk")
23-
}
40+
41+
# Use local_mocked_bindings for functions that exist
42+
local_mocked_bindings(
43+
exist_database = function(...) FALSE,
44+
exist_table = function(...) FALSE,
45+
create_database = function(...) TRUE,
46+
create_table = function(...) TRUE,
47+
delete_table = function(...) TRUE,
48+
.package = "RTD"
49+
)
50+
51+
# Mock tempdir to return platform-appropriate path
52+
temp_base <- if (.Platform$OS.type == "windows") "C:/tmp" else "/tmp"
53+
local_mocked_bindings(
54+
tempdir = function(...) temp_base,
55+
system2 = m,
56+
.package = "base"
57+
)
58+
59+
local_mocked_bindings(
60+
write_tsv = function(...) TRUE,
61+
.package = "readr"
2462
)
25-
expect_args(m, 1, embulk_exec, paste("guess", template_path, "-o /tmp/load.yml"))
26-
expect_args(m, 2, embulk_exec, "run /tmp/load.yml")
63+
64+
# Setup fake embulk and ensure cleanup
65+
cleanup_embulk <- setup_fake_embulk()
66+
on.exit(cleanup_embulk())
67+
68+
td_upload(con, "test", "iris", iris, mode = "embulk")
69+
expect_args(m, 1, embulk_exec, paste("guess", template_path, paste0("-o ", temp_base, "/load.yml")))
70+
expect_args(m, 2, embulk_exec, paste0("run ", temp_base, "/load.yml"))
2771
})
2872

2973
test_that("td_upload works with mock when the table already exists", {
3074
template_path <- system.file("extdata", "tsv_upload.yml.liquid", package = "RTD")
3175
m <- mock(0, 0)
32-
with_mock(
33-
exist_database = mock(FALSE, cycle = TRUE),
34-
exist_table = mock(TRUE, cycle = TRUE),
35-
create_database = mock(TRUE, cycle = TRUE),
36-
create_table = mock(TRUE, cycle = TRUE),
37-
delete_table = mock(TRUE, cycle = TRUE),
38-
tempdir = mock("/tmp", cycle = TRUE),
39-
`readr::write_tsv` = mock(TRUE, cycle = TRUE),
40-
`Sys.which` = mock("/home/RTD/bin/embulk", cycle = TRUE),
41-
`system2` = m, {
42-
expect_error(td_upload(con, "test", "iris", iris), ".* already exists.")
43-
td_upload(con, "test", "iris", iris, mode = "embulk", overwrite = TRUE)
44-
}
76+
77+
# Use local_mocked_bindings for functions that exist
78+
local_mocked_bindings(
79+
exist_database = function(...) FALSE,
80+
exist_table = function(...) TRUE,
81+
create_database = function(...) TRUE,
82+
create_table = function(...) TRUE,
83+
delete_table = function(...) TRUE,
84+
.package = "RTD"
85+
)
86+
87+
# Mock tempdir to return platform-appropriate path
88+
temp_base <- if (.Platform$OS.type == "windows") "C:/tmp" else "/tmp"
89+
local_mocked_bindings(
90+
tempdir = function(...) temp_base,
91+
system2 = m,
92+
.package = "base"
4593
)
46-
expect_args(m, 1, embulk_exec, paste("guess", template_path, "-o /tmp/load.yml"))
47-
expect_args(m, 2, embulk_exec, "run /tmp/load.yml")
94+
95+
local_mocked_bindings(
96+
write_tsv = function(...) TRUE,
97+
.package = "readr"
98+
)
99+
100+
# Setup fake embulk and ensure cleanup
101+
cleanup_embulk <- setup_fake_embulk()
102+
on.exit(cleanup_embulk())
103+
104+
expect_error(td_upload(con, "test", "iris", iris), ".* already exists.")
105+
td_upload(con, "test", "iris", iris, mode = "embulk", overwrite = TRUE)
106+
expect_args(m, 1, embulk_exec, paste("guess", template_path, paste0("-o ", temp_base, "/load.yml")))
107+
expect_args(m, 2, embulk_exec, paste0("run ", temp_base, "/load.yml"))
48108
})

0 commit comments

Comments
 (0)