-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dashboard_cmd.rs
More file actions
298 lines (220 loc) · 9.72 KB
/
Copy pathtest_dashboard_cmd.rs
File metadata and controls
298 lines (220 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::helpers::{
create_test_intake_entry, create_test_intake_target, create_test_user,
create_test_weight_entry, create_test_weight_target, setup_test_pool,
};
use librefit_lib::service::{dashboard::daily_dashboard, weight::create_weight_tracker_entry};
use tauri::Manager;
#[test]
fn test_daily_dashboard_success() {
let pool = setup_test_pool();
// Setup: Create user
create_test_user(&pool, "Test User", "avatar.png");
// Setup: Create targets
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
// Setup: Create some tracker entries
create_test_intake_entry(&pool, "2025-01-15", 500, "b", Some("Breakfast".to_string()));
create_test_intake_entry(&pool, "2025-01-15", 700, "l", Some("Lunch".to_string()));
create_test_weight_entry(&pool, "2025-01-15", 83.0);
create_test_weight_entry(&pool, "2025-01-15", 82.5);
// Test command using mock app
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// Verify user data
assert!(dashboard.user_data.is_some());
let user = dashboard.user_data.unwrap();
assert_eq!(user.name, "Test User");
// Verify calorie target
assert_eq!(dashboard.intake_target.target_calories, 2000);
assert_eq!(dashboard.intake_target.maximum_calories, 2500);
// Verify weight target
assert_eq!(dashboard.weight_target.target_weight, 75.0);
// Verify tracker entries
assert_eq!(dashboard.intake_today_list.len(), 2);
assert_eq!(dashboard.weight_today_list.len(), 2);
// Verify current day calculation (Jan 15 is day 14 completed, so current_day = 14)
assert_eq!(dashboard.current_day, 14);
// Verify food categories are loaded
assert!(dashboard.food_categories.len() > 0);
}
#[test]
fn test_daily_dashboard_first_day() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
create_test_weight_entry(&pool, "2025-01-01", 85.0);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-01".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// First day should be day 0 (0 days completed on day 1)
assert_eq!(dashboard.current_day, 0);
assert_eq!(dashboard.intake_today_list.len(), 0);
assert_eq!(dashboard.weight_today_list.len(), 1);
}
#[test]
fn test_daily_dashboard_last_day_of_target() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-01-31", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-01-31", 85.0, 75.0);
create_test_weight_entry(&pool, "2025-01-31", 75.0);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-31".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// Last day should be day 31 (when querying on the last day, it shows 31)
assert_eq!(dashboard.current_day, 31);
}
#[test]
fn test_daily_dashboard_date_beyond_target() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-01-31", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-01-31", 85.0, 75.0);
// Add entries in the last week of the target period
create_test_intake_entry(&pool, "2025-01-25", 2000, "l", None);
create_test_intake_entry(&pool, "2025-01-26", 2000, "l", None);
create_test_intake_entry(&pool, "2025-01-27", 2000, "l", None);
create_test_intake_entry(&pool, "2025-01-31", 2000, "l", None);
create_test_weight_entry(&pool, "2025-01-31", 85.0);
let app = tauri::test::mock_app();
app.manage(pool);
// Query date after target end date
let result = daily_dashboard(app.state(), "2025-02-15".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// When querying beyond target, it should still return successfully
// The current_day should be based on the end date, not the query date
assert_eq!(dashboard.current_day, 31);
}
#[test]
fn test_daily_dashboard_with_week_data() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
// Add entries over the past week
for day in 8..=15 {
create_test_intake_entry(&pool, &format!("2025-01-{:02}", day), 1800, "l", None);
create_test_weight_entry(&pool, &format!("2025-01-{:02}", day), 85.4);
}
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// Should have 8 days of data (Jan 8-15)
assert_eq!(dashboard.intake_week_list.len(), 8);
assert_eq!(dashboard.weight_month_list.len(), 8);
}
#[test]
fn test_daily_dashboard_with_month_weight_data() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
// Add weight entries over the past month (4 weeks)
for day in 1..=28 {
if day % 3 == 0 {
// Every 3 days
create_test_weight_entry(
&pool,
&format!("2025-01-{:02}", day),
85.0 - (day as f32 * 0.1),
);
}
}
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-28".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
// Should have weight data for the days we added
assert!(dashboard.weight_month_list.len() > 0);
}
#[test]
fn test_daily_dashboard_invalid_date_format() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "invalid-date".to_string());
assert!(result.is_err());
assert_eq!(result.err().unwrap(), "Invalid date format.");
}
#[test]
fn test_daily_dashboard_no_intake_target() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
// Only create weight target, no intake target
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_err());
assert_eq!(result.err().unwrap(), "No calorie target found");
}
#[test]
fn test_daily_dashboard_no_weight_target() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
// Only create intake target, no weight target
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_err());
assert_eq!(result.err().unwrap(), "No weight target found");
}
#[test]
fn test_daily_dashboard_empty_trackers() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
// No tracker entries
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_err());
}
#[test]
fn test_daily_dashboard_multiple_categories() {
let pool = setup_test_pool();
create_test_user(&pool, "User", "avatar.png");
create_test_intake_target(&pool, "2025-01-01", "2025-06-01", 2000, 2500);
create_test_weight_target(&pool, "2025-01-01", "2025-06-01", 85.0, 75.0);
// Add entries with different categories
create_test_intake_entry(&pool, "2025-01-15", 400, "b", Some("Breakfast".to_string()));
create_test_intake_entry(&pool, "2025-01-15", 600, "l", Some("Lunch".to_string()));
create_test_intake_entry(&pool, "2025-01-15", 700, "d", Some("Dinner".to_string()));
create_test_intake_entry(&pool, "2025-01-15", 200, "s", Some("Snack".to_string()));
create_test_intake_entry(&pool, "2025-01-15", 100, "t", Some("Treat".to_string()));
create_test_weight_entry(&pool, "2025-01-15", 85.0);
let app = tauri::test::mock_app();
app.manage(pool);
let result = daily_dashboard(app.state(), "2025-01-15".to_string());
assert!(result.is_ok());
let dashboard = result.unwrap();
assert_eq!(dashboard.intake_today_list.len(), 5);
// Verify all categories are represented
let categories: Vec<String> = dashboard
.intake_today_list
.iter()
.map(|e| e.category.clone())
.collect();
assert!(categories.contains(&"b".to_string()));
assert!(categories.contains(&"l".to_string()));
assert!(categories.contains(&"d".to_string()));
assert!(categories.contains(&"s".to_string()));
assert!(categories.contains(&"t".to_string()));
}