Skip to content

Commit 6c11185

Browse files
committed
adapt tests
fixes #75
1 parent dbeeb48 commit 6c11185

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

tests/testthat/_snaps/read_all_csv.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# read_all_csv() works
22

33
Code
4-
a %>% keep_at(~ str_detect(.x, "data")) %>% map(head)
4+
db %>% keep_at(~ str_detect(.x, "data")) %>% map(head)
55
Output
66
$data1
77
# A tibble: 6 x 7
@@ -40,7 +40,7 @@
4040
---
4141

4242
Code
43-
a %>% keep_at(~ str_detect(.x, "data")) %>% map(get_label)
43+
db %>% keep_at(~ str_detect(.x, "data")) %>% map(get_label)
4444
Output
4545
$data1
4646
$data1$subjid
@@ -112,8 +112,8 @@
112112
---
113113

114114
Code
115-
a = read_all_csv(path, labels_from = "external/external_labels.csv", verbose = 0)
116-
head(a$labels)
115+
db = read_all_csv(path, labels_from = "external/external_labels.csv", verbose = 0)
116+
head(db$labels)
117117
Output
118118
# A tibble: 6 x 2
119119
name label
@@ -125,7 +125,7 @@
125125
5 x <NA>
126126
6 crfname <NA>
127127
Code
128-
a %>% keep_at(~ str_detect(.x, "data")) %>% map(get_label)
128+
db %>% keep_at(~ str_detect(.x, "data")) %>% map(get_label)
129129
Output
130130
$data1
131131
$data1$subjid

tests/testthat/test-read_all_csv.R

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

2-
#' Helper that copy edc_example() to csv files (run manually)
2+
# Utils ---------------------------------------------------------------------------------------
3+
4+
#' Helper that copy edc_example() to csv files
35
example_to_csv = function(dir_path){
46
clean_lookup()
57
dir_create(path(dir_path, "external"), recurse=TRUE)
6-
8+
79
data_labels = edc_example() %>%
810
keep_at(~str_detect(.x, "data")) %>%
911
imap(~{
@@ -16,41 +18,51 @@ example_to_csv = function(dir_path){
1618
tibble::enframe(name="name", value="label") %>%
1719
filter(!is.na(label)) %>%
1820
distinct()
19-
21+
2022
write.csv2(data_labels, glue("{dir_path}/labels.csv"), row.names=FALSE)
2123
write.csv2(data_labels, glue("{dir_path}/external/external_labels.csv"), row.names=FALSE)
2224
}
2325

26+
27+
# Test ----------------------------------------------------------------------------------------
28+
2429
test_that("read_all_csv() works", {
30+
local_options(edc_lookup_overwrite_warn=FALSE)
31+
2532
path = path_temp("csv")
33+
unlink(path)
2634
example_to_csv(path)
27-
clean_lookup()
2835

29-
a = read_all_csv(path, labels_from="labels.csv", verbose=0)
36+
#Read from files
37+
db = read_all_csv(path, labels_from="labels.csv", verbose=0)
38+
expect_edc_database(db)
39+
expect_identical(attr(db, "source"), "files")
40+
expect_null(db$labels) #labels.csv is not a dataset
3041

31-
expect_s3_class(a, "edc_database")
32-
expect_s3_class(a$datetime_extraction, "POSIXlt")
33-
expect_type(a$date_extraction, "character")
34-
expect_null(a$labels)
42+
#Read from cache
43+
db2 = read_all_csv(path, format_file=NULL, verbose=0, use_cache=TRUE)
44+
expect_edc_database(db2)
45+
expect_identical(attr(db2, "source"), "cache")
46+
expect_setequal(c(names(db), "labels"), names(db2))
3547

3648
expect_snapshot({
37-
a %>%
49+
db %>%
3850
keep_at(~str_detect(.x, "data")) %>%
3951
map(head)
4052
})
4153

4254
expect_snapshot({
43-
a %>%
55+
db %>%
4456
keep_at(~str_detect(.x, "data")) %>%
4557
map(get_label)
4658
})
4759

4860
#read_all_csv() works with external labels
4961
clean_lookup()
5062
expect_snapshot({
51-
a = read_all_csv(path, labels_from="external/external_labels.csv", verbose=0)
52-
head(a$labels) #not NULL, considered as a normal dataset
53-
a %>%
63+
db = read_all_csv(path, labels_from="external/external_labels.csv", verbose=0)
64+
head(db$labels) #not NULL, considered as a normal dataset
65+
db %>%
5466
keep_at(~str_detect(.x, "data")) %>%
5567
map(get_label)
5668
})
@@ -59,3 +71,5 @@ test_that("read_all_csv() works", {
5971
})
6072

6173

74+
75+

0 commit comments

Comments
 (0)