Skip to main content

jellytau_lib/storage/
models.rs

1//! Database model structs
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Playlist item entry
7#[allow(dead_code)]
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct PlaylistItem {
10    pub id: Option<i64>,
11    pub playlist_id: String,
12    pub item_id: String,
13    pub sort_order: i32,
14    pub added_at: Option<DateTime<Utc>>,
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn test_playlist_item_creation() {
23        let item = PlaylistItem {
24            id: Some(1),
25            playlist_id: "playlist-123".to_string(),
26            item_id: "item-456".to_string(),
27            sort_order: 0,
28            added_at: None,
29        };
30
31        assert_eq!(item.id, Some(1));
32        assert_eq!(item.playlist_id, "playlist-123");
33        assert_eq!(item.item_id, "item-456");
34        assert_eq!(item.sort_order, 0);
35        assert!(item.added_at.is_none());
36    }
37
38    #[test]
39    fn test_playlist_item_without_id() {
40        let item = PlaylistItem {
41            id: None,
42            playlist_id: "playlist-789".to_string(),
43            item_id: "item-999".to_string(),
44            sort_order: 5,
45            added_at: None,
46        };
47
48        assert!(item.id.is_none());
49        assert_eq!(item.sort_order, 5);
50    }
51
52    #[test]
53    fn test_playlist_item_serialization() {
54        let item = PlaylistItem {
55            id: Some(42),
56            playlist_id: "pl-001".to_string(),
57            item_id: "it-001".to_string(),
58            sort_order: 10,
59            added_at: None,
60        };
61
62        let json = serde_json::to_string(&item);
63        assert!(json.is_ok());
64        let serialized = json.unwrap();
65        assert!(serialized.contains("pl-001"));
66        assert!(serialized.contains("it-001"));
67        assert!(serialized.contains("10"));
68    }
69
70    #[test]
71    fn test_playlist_item_clone() {
72        let item = PlaylistItem {
73            id: Some(99),
74            playlist_id: "clone-playlist".to_string(),
75            item_id: "clone-item".to_string(),
76            sort_order: 3,
77            added_at: None,
78        };
79
80        let cloned = item.clone();
81        assert_eq!(item.id, cloned.id);
82        assert_eq!(item.playlist_id, cloned.playlist_id);
83        assert_eq!(item.item_id, cloned.item_id);
84        assert_eq!(item.sort_order, cloned.sort_order);
85    }
86
87    #[test]
88    fn test_playlist_item_with_timestamp() {
89        let now = Utc::now();
90        let item = PlaylistItem {
91            id: Some(100),
92            playlist_id: "time-playlist".to_string(),
93            item_id: "time-item".to_string(),
94            sort_order: 7,
95            added_at: Some(now),
96        };
97
98        assert!(item.added_at.is_some());
99        assert_eq!(item.added_at, Some(now));
100    }
101
102    #[test]
103    fn test_playlist_item_multiple_sort_orders() {
104        let sort_orders = vec![0, 1, 5, 10, 100, 999];
105
106        for order in sort_orders {
107            let item = PlaylistItem {
108                id: Some(1),
109                playlist_id: "pl".to_string(),
110                item_id: "it".to_string(),
111                sort_order: order,
112                added_at: None,
113            };
114
115            assert_eq!(item.sort_order, order);
116        }
117    }
118
119    #[test]
120    fn test_playlist_item_debug() {
121        let item = PlaylistItem {
122            id: Some(200),
123            playlist_id: "debug-pl".to_string(),
124            item_id: "debug-it".to_string(),
125            sort_order: 15,
126            added_at: None,
127        };
128
129        let debug_str = format!("{:?}", item);
130        assert!(debug_str.contains("PlaylistItem"));
131        assert!(debug_str.contains("debug-pl"));
132    }
133}