1+ # --------------------------------------------------------------------------------------------
2+ # Copyright (c) Microsoft Corporation. All rights reserved.
3+ # Licensed under the MIT License. See LICENSE.txt in the project root for license information.
4+ # --------------------------------------------------------------------------------------------
5+
6+ test_that(" create_dt show_rows parameter works correctly" , {
7+ # Create a simple test dataframe
8+ test_data <- data.frame (
9+ PersonId = 1 : 100 ,
10+ Organization = paste0(" Org" , 1 : 100 ),
11+ Value = rnorm(100 ),
12+ stringsAsFactors = FALSE
13+ )
14+
15+ # Test default behavior (show_rows = 10)
16+ dt_default <- create_dt(test_data )
17+ expect_s3_class(dt_default , " datatables" )
18+
19+ # Test show_rows = 25
20+ dt_25 <- create_dt(test_data , show_rows = 25 )
21+ expect_s3_class(dt_25 , " datatables" )
22+
23+ # Test show_rows = "All"
24+ dt_all <- create_dt(test_data , show_rows = " All" )
25+ expect_s3_class(dt_all , " datatables" )
26+
27+ # Test show_rows = -1 (equivalent to "All")
28+ dt_all_numeric <- create_dt(test_data , show_rows = - 1 )
29+ expect_s3_class(dt_all_numeric , " datatables" )
30+ })
31+
32+ test_that(" create_dt show_rows parameter constructs correct lengthMenu" , {
33+ # Create a simple test dataframe
34+ test_data <- data.frame (
35+ A = 1 : 5 ,
36+ B = letters [1 : 5 ],
37+ stringsAsFactors = FALSE
38+ )
39+
40+ # Test that function runs without error for different show_rows values
41+ expect_no_error(create_dt(test_data , show_rows = 10 ))
42+ expect_no_error(create_dt(test_data , show_rows = 25 ))
43+ expect_no_error(create_dt(test_data , show_rows = 50 ))
44+ expect_no_error(create_dt(test_data , show_rows = " All" ))
45+ expect_no_error(create_dt(test_data , show_rows = - 1 ))
46+ })
47+
48+ test_that(" create_dt rounding parameter works with numeric values (backward compatibility)" , {
49+ # Create test data with numeric columns
50+ test_data <- data.frame (
51+ Name = c(" A" , " B" , " C" ),
52+ Value1 = c(1.23456 , 2.78901 , 3.45678 ),
53+ Value2 = c(0.1234 , 0.5678 , 0.9012 ),
54+ Count = c(10L , 20L , 30L ),
55+ stringsAsFactors = FALSE
56+ )
57+
58+ # Test with numeric rounding values
59+ expect_no_error(create_dt(test_data , rounding = 1 ))
60+ expect_no_error(create_dt(test_data , rounding = 2 ))
61+ expect_no_error(create_dt(test_data , rounding = 0 ))
62+
63+ # Should return datatables object
64+ result <- create_dt(test_data , rounding = 2 )
65+ expect_s3_class(result , " datatables" )
66+ })
67+
68+ test_that(" create_dt rounding parameter works with named lists" , {
69+ # Create test data with numeric columns
70+ test_data <- data.frame (
71+ Name = c(" A" , " B" , " C" ),
72+ Value1 = c(1.23456 , 2.78901 , 3.45678 ),
73+ Value2 = c(0.1234 , 0.5678 , 0.9012 ),
74+ Count = c(10L , 20L , 30L ),
75+ stringsAsFactors = FALSE
76+ )
77+
78+ # Test with list rounding
79+ expect_no_error(create_dt(test_data , rounding = list (" Value1" = 1 , " Value2" = 3 )))
80+ expect_no_error(create_dt(test_data , rounding = list (" Count" = 0 )))
81+ expect_no_error(create_dt(test_data , rounding = list (" Value1" = 2 , " Value2" = 1 , " Count" = 0 )))
82+
83+ # Should return datatables object
84+ result <- create_dt(test_data , rounding = list (" Value1" = 1 , " Value2" = 3 ))
85+ expect_s3_class(result , " datatables" )
86+ })
87+
88+ test_that(" create_dt handles edge cases for rounding parameter" , {
89+ # Test data with only non-numeric columns
90+ non_numeric_data <- data.frame (
91+ Name = c(" A" , " B" , " C" ),
92+ Category = c(" X" , " Y" , " Z" ),
93+ stringsAsFactors = FALSE
94+ )
95+
96+ # Should work with non-numeric data regardless of rounding parameter
97+ expect_no_error(create_dt(non_numeric_data , rounding = 2 ))
98+ expect_no_error(create_dt(non_numeric_data , rounding = list (" Name" = 1 )))
99+
100+ # Test data with numeric columns
101+ numeric_data <- data.frame (
102+ Value1 = c(1.23 , 2.34 ),
103+ Value2 = c(3.45 , 4.56 )
104+ )
105+
106+ # Test with empty list
107+ expect_no_error(create_dt(numeric_data , rounding = list ()))
108+
109+ # Test with list containing non-existent column names
110+ expect_no_error(create_dt(numeric_data , rounding = list (" NonExistent" = 2 , " Value1" = 1 )))
111+
112+ # Should still return datatables object
113+ result <- create_dt(numeric_data , rounding = list (" NonExistent" = 2 , " Value1" = 1 ))
114+ expect_s3_class(result , " datatables" )
115+ })
116+
117+ test_that(" create_dt rounding works with percentage formatting" , {
118+ # Create test data
119+ test_data <- data.frame (
120+ Category = c(" A" , " B" ),
121+ Rate1 = c(0.234 , 0.567 ),
122+ Rate2 = c(0.123 , 0.789 ),
123+ stringsAsFactors = FALSE
124+ )
125+
126+ # Test numeric rounding with percentages
127+ expect_no_error(create_dt(test_data , rounding = 2 , percent = TRUE ))
128+
129+ # Test list rounding with percentages
130+ expect_no_error(create_dt(test_data , rounding = list (" Rate1" = 1 , " Rate2" = 3 ), percent = TRUE ))
131+
132+ # Should return datatables object
133+ result <- create_dt(test_data , rounding = list (" Rate1" = 1 , " Rate2" = 3 ), percent = TRUE )
134+ expect_s3_class(result , " datatables" )
135+ })
136+
137+ test_that(" create_dt preserves all other functionality with new rounding feature" , {
138+ # Create test data
139+ test_data <- data.frame (
140+ Name = paste0(" Item" , 1 : 20 ),
141+ Value1 = runif(20 , 1 , 10 ),
142+ Value2 = runif(20 , 0 , 1 ),
143+ stringsAsFactors = FALSE
144+ )
145+
146+ # Test that all combinations work
147+ expect_no_error(create_dt(test_data ,
148+ rounding = list (" Value1" = 2 , " Value2" = 3 ),
149+ freeze = 1 ,
150+ percent = FALSE ,
151+ show_rows = 15 ))
152+
153+ expect_no_error(create_dt(test_data ,
154+ rounding = list (" Value2" = 4 ),
155+ freeze = 2 ,
156+ percent = TRUE ,
157+ show_rows = " All" ))
158+
159+ # All should return datatables objects
160+ result1 <- create_dt(test_data , rounding = list (" Value1" = 2 ), freeze = 1 )
161+ result2 <- create_dt(test_data , rounding = 2 , freeze = 1 ) # backward compatibility
162+
163+ expect_s3_class(result1 , " datatables" )
164+ expect_s3_class(result2 , " datatables" )
165+ })
0 commit comments