Skip to content

Commit 615369b

Browse files
committed
add tests for expression casting
1 parent f5b1ac0 commit 615369b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/testthat/test-variable-as-methods.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,68 @@ with_mock_crunch({
173173
})
174174
})
175175

176+
test_that("cast expressions modifies zcl expression", {
177+
# simple expression, which we'll reuse even if it wouldn't actually work in
178+
# a real cast
179+
this_expr <- CrunchExpr(expression = zfunc("now"))
180+
181+
expect_equal(
182+
as.Numeric(this_expr),
183+
CrunchExpr(expression = zfunc("cast", this_expr, "numeric"))
184+
)
185+
186+
expect_equal(
187+
as.Text(this_expr),
188+
CrunchExpr(expression = zfunc("cast", this_expr, "text"))
189+
)
190+
191+
expect_equal(
192+
as.Text(this_expr, format = "%Y-%m-%d %H:%M:%S"),
193+
CrunchExpr(
194+
expression = zfunc("format_datetime", this_expr, list(value = "%Y-%m-%d %H:%M:%S"))
195+
)
196+
)
197+
198+
expect_equal(
199+
as.Categorical(this_expr),
200+
CrunchExpr(expression = zfunc("cast", this_expr, "categorical"))
201+
)
202+
203+
expect_equal(
204+
as.Categorical(this_expr, format = "%Y-%m-%d %H:%M:%S"),
205+
CrunchExpr(
206+
expression = zfunc(
207+
"cast",
208+
zfunc("format_datetime", this_expr, list(value = "%Y-%m-%d %H:%M:%S")),
209+
"categorical"
210+
)
211+
)
212+
)
213+
214+
expect_equal(
215+
as.Datetime(this_expr, format = "%Y-%m-%d %H:%M:%S"),
216+
CrunchExpr(
217+
expression = zfunc("parse_datetime", this_expr, list(value = "%Y-%m-%d %H:%M:%S"))
218+
)
219+
)
220+
221+
expect_equal(
222+
as.Datetime(this_expr, resolution = "D", offset = "1975-01-01"),
223+
CrunchExpr(
224+
expression = zfunc("numeric_to_datetime", this_expr, list(value = "D"), list(value = "1975-01-01"))
225+
)
226+
)
227+
228+
expect_error(as.Datetime(this_expr), "Invalid arguments to `as.Datetime`")
229+
230+
# R base aliases
231+
expect_equal(as.Numeric(this_expr), as.numeric(this_expr))
232+
expect_equal(as.Text(this_expr), as.character(this_expr))
233+
})
234+
235+
236+
237+
176238
with_test_authentication({
177239
ds <- newDataset(df)
178240
# make a text variable with numbers
@@ -317,4 +379,24 @@ with_test_authentication({
317379
cubify(10, 11, dims = list(v4 = list("B", "C")))
318380
)
319381
})
382+
383+
test_that("categorical expression to numeric", {
384+
ds$v3_gt_ten_num <- as.Numeric(ds$v3 > 10)
385+
expect_true(is.derived(ds$v3_gt_ten_num))
386+
expect_true(is.Numeric(ds$v3_gt_ten_num))
387+
expect_equal(
388+
as.vector(ds$v3_gt_ten_num),
389+
as.numeric(as.vector(ds$v3 > 10))
390+
)
391+
})
392+
393+
test_that("numeric expression to text", {
394+
ds$v3_plus_one_text <- as.Text(ds$v3 + 1)
395+
expect_true(is.derived(ds$v3_plus_one_text))
396+
expect_true(is.Text(ds$v3_plus_one_text))
397+
expect_equal(
398+
as.vector(ds$v3_plus_one_text),
399+
sprintf("%1.1f", as.vector(ds$v3 + 1))
400+
)
401+
})
320402
})

0 commit comments

Comments
 (0)