11
2- # ' Helper that copy edc_example() to csv files (run manually)
2+ # Utils ---------------------------------------------------------------------------------------
3+
4+ # ' Helper that copy edc_example() to csv files
35example_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+
2429test_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