Skip to content

Commit 779925f

Browse files
committed
chore: add test
1 parent 9953756 commit 779925f

File tree

11 files changed

+869
-100
lines changed

11 files changed

+869
-100
lines changed

collab-database/src/fields/type_option/checkbox_type_option.rs

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl TypeOptionCellWriter for CheckboxTypeOption {
4949
Value::Number(n) => Some(n.to_string()),
5050
_ => None,
5151
} {
52-
cell.insert(CELL_DATA.into(), bool_from_str(&data).into());
52+
cell.insert(CELL_DATA.into(), bool_from_str(&data).to_string().into());
5353
}
5454
cell
5555
}
@@ -75,3 +75,85 @@ fn bool_from_str(s: &str) -> bool {
7575
_ => false,
7676
}
7777
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
#[test]
84+
fn test_checkbox_type_option_json_cell() {
85+
let option = CheckboxTypeOption::new();
86+
let mut cell = new_cell_builder(FieldType::Checkbox);
87+
cell.insert(CELL_DATA.into(), "true".into());
88+
89+
// Convert cell to JSON
90+
let value = option.json_cell(&cell);
91+
assert_eq!(value, Value::String("true".to_string()));
92+
93+
// Test with empty data
94+
let empty_cell = new_cell_builder(FieldType::Checkbox);
95+
let empty_value = option.json_cell(&empty_cell);
96+
assert_eq!(empty_value, Value::String("".to_string()));
97+
}
98+
99+
#[test]
100+
fn test_checkbox_type_option_numeric_cell() {
101+
let option = CheckboxTypeOption::new();
102+
103+
let mut true_cell = new_cell_builder(FieldType::Checkbox);
104+
true_cell.insert(CELL_DATA.into(), "true".into());
105+
assert_eq!(option.numeric_cell(&true_cell), Some(1.0));
106+
107+
let mut false_cell = new_cell_builder(FieldType::Checkbox);
108+
false_cell.insert(CELL_DATA.into(), "false".into());
109+
assert_eq!(option.numeric_cell(&false_cell), Some(0.0));
110+
111+
let mut invalid_cell = new_cell_builder(FieldType::Checkbox);
112+
invalid_cell.insert(CELL_DATA.into(), "invalid".into());
113+
assert_eq!(option.numeric_cell(&invalid_cell), Some(0.0));
114+
}
115+
116+
#[test]
117+
fn test_checkbox_type_option_write_json() {
118+
let option = CheckboxTypeOption::new();
119+
120+
// Write a string
121+
let value = Value::String("true".to_string());
122+
let cell = option.write_json(value);
123+
assert_eq!(cell.get_as::<String>(CELL_DATA).unwrap(), "true");
124+
125+
// Write a boolean
126+
let value = Value::Bool(true);
127+
let cell = option.write_json(value);
128+
assert_eq!(cell.get_as::<String>(CELL_DATA).unwrap(), "true");
129+
130+
// Write a number
131+
let value = Value::Number(1.into());
132+
let cell = option.write_json(value);
133+
assert_eq!(cell.get_as::<String>(CELL_DATA).unwrap(), "true");
134+
}
135+
136+
#[test]
137+
fn test_checkbox_type_option_raw_conversion() {
138+
let option = CheckboxTypeOption::new();
139+
assert_eq!(
140+
option.convert_raw_cell_data("raw data"),
141+
"raw data".to_string()
142+
);
143+
}
144+
145+
#[test]
146+
fn test_bool_from_str() {
147+
assert!(bool_from_str("true"));
148+
assert!(bool_from_str("1"));
149+
assert!(bool_from_str("yes"));
150+
151+
assert!(!bool_from_str("false"));
152+
assert!(!bool_from_str("0"));
153+
assert!(!bool_from_str("no"));
154+
155+
// Invalid inputs default to false
156+
assert!(!bool_from_str("invalid"));
157+
assert!(!bool_from_str(""));
158+
}
159+
}

collab-database/src/fields/type_option/checklist_type_option.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,73 @@ impl From<ChecklistCellData> for Cell {
6969
cell
7070
}
7171
}
72+
#[cfg(test)]
73+
mod checklist_type_option_tests {
74+
use super::*;
75+
use serde_json::json;
76+
77+
#[test]
78+
fn test_json_cell_conversion() {
79+
let checklist_option = ChecklistTypeOption;
80+
81+
let cell_data = ChecklistCellData::from((
82+
vec!["Opt1".to_string(), "Opt2".to_string()],
83+
vec!["Opt1".to_string()],
84+
));
85+
let cell: Cell = cell_data.clone().into();
86+
87+
let json_value = checklist_option.json_cell(&cell);
88+
let restored_data: ChecklistCellData =
89+
serde_json::from_value(json_value).expect("Valid JSON value");
90+
91+
assert_eq!(restored_data.options.len(), 2);
92+
assert_eq!(restored_data.selected_option_ids.len(), 1);
93+
}
94+
95+
#[test]
96+
fn test_numeric_cell_conversion() {
97+
let checklist_option = ChecklistTypeOption;
98+
99+
let cell_data = ChecklistCellData::from((
100+
vec!["Opt1".to_string(), "Opt2".to_string()],
101+
vec!["Opt1".to_string()],
102+
));
103+
let cell: Cell = cell_data.clone().into();
104+
105+
let numeric_value = checklist_option.numeric_cell(&cell);
106+
assert!(numeric_value.is_none());
107+
}
108+
109+
#[test]
110+
fn test_raw_cell_data_conversion() {
111+
let checklist_option = ChecklistTypeOption;
112+
113+
let cell_data = ChecklistCellData::from((
114+
vec!["OptA".to_string(), "OptB".to_string()],
115+
vec!["OptA".to_string()],
116+
));
117+
let cell_data_json = serde_json::to_string(&cell_data).expect("Valid serialization");
118+
119+
let converted_data = checklist_option.convert_raw_cell_data(&cell_data_json);
120+
assert_eq!(converted_data, "OptA,OptB");
121+
}
122+
123+
#[test]
124+
fn test_write_json_to_cell() {
125+
let checklist_option = ChecklistTypeOption;
126+
127+
let json_value = json!({
128+
"options": [
129+
{ "id": "1", "name": "Option1", "color": 0 },
130+
{ "id": "2", "name": "Option2", "color": 1 }
131+
],
132+
"selected_option_ids": ["1"]
133+
});
134+
135+
let cell = checklist_option.write_json(json_value);
136+
let restored_data = ChecklistCellData::from(&cell);
137+
138+
assert_eq!(restored_data.options.len(), 2);
139+
assert_eq!(restored_data.selected_option_ids.len(), 1);
140+
}
141+
}

0 commit comments

Comments
 (0)