Skip to main content

nautilus_core/column/
mod.rs

1//! Typed column references for type-safe query building.
2
3pub mod from_value;
4pub mod marker;
5pub mod row_access;
6pub mod typed;
7
8pub use from_value::FromValue;
9pub use marker::ColumnMarker;
10pub use row_access::RowAccess;
11pub use typed::{Column, SelectColumns};
12
13#[cfg(test)]
14mod tests {
15    use std::borrow::Cow;
16
17    use super::*;
18    use crate::expr::Expr;
19    use crate::select::OrderDir;
20    use crate::value::Value;
21
22    #[test]
23    fn test_column_new() {
24        let col: Column<i64> = Column::new("users", "id");
25        assert_eq!(col.table(), "users");
26        assert_eq!(col.name(), "id");
27
28        let col: Column<String> = Column::new("users", "email");
29        assert_eq!(col.table(), "users");
30        assert_eq!(col.name(), "email");
31    }
32
33    #[test]
34    fn test_column_alias() {
35        let col: Column<i64> = Column::new("users", "id");
36        assert_eq!(col.alias(), "users__id");
37
38        let col: Column<String> = Column::new("posts", "title");
39        assert_eq!(col.alias(), "posts__title");
40    }
41
42    #[test]
43    fn test_column_marker() {
44        let col: Column<i64> = Column::new("users", "id");
45        let marker = col.marker();
46        assert_eq!(marker.table, "users");
47        assert_eq!(marker.name, "id");
48        assert!(matches!(marker.table, Cow::Borrowed("users")));
49        assert!(matches!(marker.name, Cow::Borrowed("id")));
50    }
51
52    #[test]
53    fn test_column_to_expr() {
54        let col: Column<i64> = Column::new("users", "id");
55        let expr: Expr = col.into();
56        assert_eq!(expr, Expr::Column("users__id".to_string()));
57    }
58
59    #[test]
60    fn test_eq_operator() {
61        let col: Column<i64> = Column::new("users", "id");
62        let expr = col.eq(42i64);
63        assert_eq!(
64            expr,
65            Expr::column("users__id").eq(Expr::param(Value::I64(42)))
66        );
67    }
68
69    #[test]
70    fn test_eq_operator_string() {
71        let col: Column<String> = Column::new("users", "email");
72        let expr = col.eq("test@example.com");
73        assert_eq!(
74            expr,
75            Expr::column("users__email")
76                .eq(Expr::param(Value::String("test@example.com".to_string())))
77        );
78    }
79
80    #[test]
81    fn test_desc_order() {
82        let col: Column<String> = Column::new("users", "email");
83        let order = col.desc();
84        assert_eq!(order.column, "users__email");
85        assert_eq!(order.direction, OrderDir::Desc);
86    }
87
88    #[test]
89    fn test_asc_order() {
90        let col: Column<i64> = Column::new("users", "id");
91        let order = col.asc();
92        assert_eq!(order.column, "users__id");
93        assert_eq!(order.direction, OrderDir::Asc);
94    }
95
96    #[test]
97    fn test_ends_with() {
98        let col: Column<String> = Column::new("users", "email");
99        let expr = col.ends_with("example.com");
100        assert_eq!(
101            expr,
102            Expr::column("users__email")
103                .like(Expr::param(Value::String("%example.com".to_string())))
104        );
105    }
106
107    #[test]
108    fn test_starts_with() {
109        let col: Column<String> = Column::new("users", "email");
110        let expr = col.starts_with("admin");
111        assert_eq!(
112            expr,
113            Expr::column("users__email").like(Expr::param(Value::String("admin%".to_string())))
114        );
115    }
116
117    #[test]
118    fn test_contains() {
119        let col: Column<String> = Column::new("users", "email");
120        let expr = col.contains("example");
121        assert_eq!(
122            expr,
123            Expr::column("users__email").like(Expr::param(Value::String("%example%".to_string())))
124        );
125    }
126
127    #[test]
128    fn test_from_value_i64() {
129        assert_eq!(i64::from_value(&Value::I64(42)).unwrap(), 42);
130        assert!(i64::from_value(&Value::Null).is_err());
131        assert!(i64::from_value(&Value::String("test".to_string())).is_err());
132    }
133
134    #[test]
135    fn test_from_value_string() {
136        assert_eq!(
137            String::from_value(&Value::String("test".to_string())).unwrap(),
138            "test"
139        );
140        assert!(String::from_value(&Value::Null).is_err());
141        assert!(String::from_value(&Value::I64(42)).is_err());
142    }
143
144    #[test]
145    fn test_from_value_bool() {
146        assert!(bool::from_value(&Value::Bool(true)).unwrap());
147        assert!(!bool::from_value(&Value::Bool(false)).unwrap());
148        assert!(bool::from_value(&Value::I64(1)).unwrap());
149        assert!(!bool::from_value(&Value::I64(0)).unwrap());
150        assert!(bool::from_value(&Value::Null).is_err());
151        assert!(bool::from_value(&Value::I64(2)).is_err());
152    }
153
154    #[test]
155    fn test_select_columns_single() {
156        let selection = (Column::<i64>::new("users", "id"),);
157        let columns = selection.columns();
158        assert_eq!(columns.len(), 1);
159        assert_eq!(columns[0].table, "users");
160        assert_eq!(columns[0].name, "id");
161    }
162
163    #[test]
164    fn test_select_columns_two() {
165        let selection = (
166            Column::<i64>::new("users", "id"),
167            Column::<String>::new("users", "email"),
168        );
169        let columns = selection.columns();
170        assert_eq!(columns.len(), 2);
171        assert_eq!(columns[0].table, "users");
172        assert_eq!(columns[0].name, "id");
173        assert_eq!(columns[1].table, "users");
174        assert_eq!(columns[1].name, "email");
175    }
176
177    #[test]
178    fn test_select_columns_multiple_tables() {
179        let selection = (
180            Column::<i64>::new("users", "id"),
181            Column::<String>::new("posts", "title"),
182        );
183        let columns = selection.columns();
184        assert_eq!(columns.len(), 2);
185        assert_eq!(columns[0].table, "users");
186        assert_eq!(columns[0].name, "id");
187        assert_eq!(columns[1].table, "posts");
188        assert_eq!(columns[1].name, "title");
189    }
190
191    #[test]
192    fn test_column_copy() {
193        let col1: Column<i64> = Column::new("users", "id");
194        let col2 = col1;
195        let col3 = col1;
196        assert_eq!(col1.name(), col2.name());
197        assert_eq!(col2.name(), col3.name());
198    }
199
200    #[test]
201    fn test_column_marker_from() {
202        let col: Column<i64> = Column::new("users", "id");
203        let marker: ColumnMarker = col.into();
204        assert_eq!(marker.table, "users");
205        assert_eq!(marker.name, "id");
206    }
207
208    #[test]
209    fn test_column_marker_alias() {
210        let marker = ColumnMarker::new("users", "id");
211        assert_eq!(marker.alias(), "users__id");
212
213        let marker = ColumnMarker::new("posts", "title");
214        assert_eq!(marker.alias(), "posts__title");
215    }
216
217    #[test]
218    fn test_dynamic_column_marker_owns_runtime_strings() {
219        let table = String::from("users");
220        let name = String::from("email");
221        let marker = ColumnMarker::new(table, name);
222
223        assert!(matches!(marker.table, Cow::Owned(_)));
224        assert!(matches!(marker.name, Cow::Owned(_)));
225    }
226
227    struct MockRow {
228        values: Vec<Value>,
229    }
230
231    impl<'row> RowAccess<'row> for MockRow {
232        fn get_by_pos(&'row self, idx: usize) -> Option<&'row Value> {
233            self.values.get(idx)
234        }
235
236        fn get(&'row self, _name: &str) -> Option<&'row Value> {
237            None
238        }
239
240        fn column_name(&'row self, _idx: usize) -> Option<&'row str> {
241            None
242        }
243
244        fn len(&self) -> usize {
245            self.values.len()
246        }
247    }
248
249    #[test]
250    fn test_select_columns_decode_single() {
251        let selection = (Column::<i64>::new("users", "id"),);
252        let mock_row = MockRow {
253            values: vec![Value::I64(42)],
254        };
255
256        let result = selection.decode(&mock_row).unwrap();
257        assert_eq!(result, (42,));
258    }
259
260    #[test]
261    fn test_select_columns_decode_two() {
262        let selection = (
263            Column::<i64>::new("users", "id"),
264            Column::<String>::new("users", "email"),
265        );
266        let mock_row = MockRow {
267            values: vec![
268                Value::I64(42),
269                Value::String("test@example.com".to_string()),
270            ],
271        };
272
273        let result = selection.decode(&mock_row).unwrap();
274        assert_eq!(result, (42, "test@example.com".to_string()));
275    }
276
277    #[test]
278    fn test_select_columns_decode_three() {
279        let selection = (
280            Column::<i64>::new("users", "id"),
281            Column::<String>::new("users", "email"),
282            Column::<bool>::new("users", "active"),
283        );
284        let mock_row = MockRow {
285            values: vec![
286                Value::I64(99),
287                Value::String("admin@example.com".to_string()),
288                Value::Bool(true),
289            ],
290        };
291
292        let result = selection.decode(&mock_row).unwrap();
293        assert_eq!(result, (99, "admin@example.com".to_string(), true));
294    }
295
296    #[test]
297    fn test_select_columns_decode_missing_column() {
298        let selection = (
299            Column::<i64>::new("users", "id"),
300            Column::<String>::new("users", "email"),
301        );
302        let mock_row = MockRow {
303            values: vec![Value::I64(42)],
304        };
305
306        let result = selection.decode(&mock_row);
307        assert!(result.is_err());
308    }
309
310    #[test]
311    fn test_select_columns_decode_type_error() {
312        let selection = (Column::<i64>::new("users", "id"),);
313        let mock_row = MockRow {
314            values: vec![Value::String("not a number".to_string())],
315        };
316
317        let result = selection.decode(&mock_row);
318        assert!(result.is_err());
319    }
320}