Debian Codegraph @

tests/testthat/test-reshape.R

raw
1 test_that("You can parition a skim_df", {
2 skimmed <- skim(iris)
3 input <- partition(skimmed)
4 expect_s3_class(input, "skim_list")
5 expect_length(input, 2)
6 expect_named(input, c("factor", "numeric"))
7 attrs <- attributes(input)
8 expect_equal(attrs$data_rows, 150)
9 expect_equal(attrs$data_cols, 5)
10 expect_identical(attrs$df_name, "`iris`")
11 expect_identical(attrs$base_skimmers, c("n_missing", "complete_rate"))
12 expect_identical(
13 attrs$skimmers_used,
14 list(
15 numeric = c("mean", "sd", "p0", "p25", "p50", "p75", "p100", "hist"),
16 factor = c("ordered", "n_unique", "top_counts")
17 )
18 )
19
20 # Subtables
21 expect_s3_class(input$factor, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
22 expect_n_rows(input$factor, 1)
23 expect_n_columns(input$factor, 6)
24 expect_named(input$factor, c(
25 "skim_variable", "n_missing", "complete_rate", "ordered", "n_unique",
26 "top_counts"
27 ))
28
29 expect_s3_class(input$numeric, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
30 expect_n_rows(input$numeric, 4)
31 expect_n_columns(input$numeric, 11)
32 expect_named(input$numeric, c(
33 "skim_variable", "n_missing", "complete_rate", "mean",
34 "sd", "p0", "p25", "p50", "p75", "p100",
35 "hist"
36 ))
37 })
38
39 test_that("Partitioning works in a round trip", {
40 skimmed <- skim(iris)
41 partitioned <- partition(skimmed)
42 input <- bind(partitioned)
43 expect_identical(input, skimmed)
44 })
45
46 test_that("You can yank a subtable from a skim_df", {
47 skimmed <- skim(iris)
48 input <- yank(skimmed, "numeric")
49 expect_s3_class(input, c("one_skim_df", "tbl_df", "tbl", "data.frame"))
50 expect_n_rows(input, 4)
51 expect_n_columns(input, 11)
52 expect_named(input, c(
53 "skim_variable", "n_missing", "complete_rate", "mean",
54 "sd", "p0", "p25", "p50", "p75", "p100",
55 "hist"
56 ))
57 })
58
59 test_that("Partition is safe if some skimmers are missing", {
60 skimmed <- skim(iris)
61 reduced <- dplyr::select(skimmed, skim_variable, skim_type, n_missing)
62 partitioned <- partition(reduced)
63 expect_length(partitioned, 2)
64 expect_named(partitioned, c("factor", "numeric"))
65 expect_named(partitioned$numeric, c("skim_variable", "n_missing"))
66 })
67
68 test_that("Partition handles new columns", {
69 skimmed <- skim(iris)
70 expanded <- dplyr::mutate(
71 skimmed,
72 mean2 = numeric.mean^2,
73 complete2 = complete_rate^2
74 )
75 partitioned <- partition(expanded)
76 expect_named(partitioned$numeric, c(
77 "skim_variable", "n_missing", "complete_rate", "mean",
78 "sd", "p0", "p25", "p50", "p75", "p100",
79 "hist", "mean2", "complete2"
80 ))
81 })
82
83 test_that("focus() matches select(data, skim_type, skim_variable, ...)", {
84 skimmed <- skim(iris)
85 expected <- dplyr::select(
86 skimmed, skim_type, skim_variable, n_missing
87 )
88 expect_equal(focus(skimmed, n_missing), expected, ignore_attr = "skimmers_used")
89 })
90
91 test_that("focus() does not allow dropping skim metadata columns", {
92 skimmed <- skim(iris)
93 expect_error(focus(skimmed, -skim_variable), "Cannot drop")
94 expect_error(focus(skimmed, -skim_type), "Cannot drop")
95 })
96
97 test_that("skim_to_wide() returns a deprecation warning", {
98 expect_warning(skim_to_wide(iris))
99 })
100
101 test_that("skim_to_list() returns a deprecation warning", {
102 expect_warning(skim_to_list(iris))
103 })
104
105 test_that("to_long() returns a long tidy data frame with 4 columns", {
106 skimmed_long <- to_long(iris)
107 # Statistics from the skim_df with values of NA are not included
108 expect_n_rows(skimmed_long, 45)
109 expect_equal(
110 names(skimmed_long),
111 c("skim_type", "skim_variable", "stat", "formatted")
112 )
113 expect_length(unique(skimmed_long$stat), 13)
114 expect_length(unique(skimmed_long$skim_type), 2)
115 expect_length(unique(skimmed_long$skim_variable), 5)
116 })
117
118 test_that("to_long() on a skim_df returns a long tidy df with 4 columns", {
119 skimmed_long <- to_long(skim(iris))
120 # Statistics from the skim_df with values of NA are not included
121 expect_n_rows(skimmed_long, 45)
122 expect_named(
123 skimmed_long,
124 c("skim_type", "skim_variable", "stat", "formatted")
125 )
126 expect_length(unique(skimmed_long$stat), 13)
127 expect_length(unique(skimmed_long$skim_type), 2)
128 expect_length(unique(skimmed_long$skim_variable), 5)
129 })
130
131 test_that("to_long() on a df and a skim_df from same df are identical", {
132 expect_identical(to_long(skim(chickwts)), to_long(chickwts))
133 })