|
| 1 | +use collab::database::entity::{FieldType, default_type_option_data_from_type}; |
| 2 | +use collab::database::fields::rollup_type_option::{RollupDisplayMode, RollupTypeOption}; |
| 3 | +use collab::database::fields::{TypeOptionCellReader, TypeOptionCellWriter, TypeOptionData}; |
| 4 | +use collab::database::rows::Cell; |
| 5 | +use collab::database::template::entity::CELL_DATA; |
| 6 | +use collab::util::AnyMapExt; |
| 7 | +use serde_json::json; |
| 8 | +use yrs::Any; |
| 9 | + |
| 10 | +// ==================== RollupDisplayMode Tests ==================== |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn rollup_display_mode_from_i64_calculated() { |
| 14 | + assert_eq!(RollupDisplayMode::from(0), RollupDisplayMode::Calculated); |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn rollup_display_mode_from_i64_original_list() { |
| 19 | + assert_eq!(RollupDisplayMode::from(1), RollupDisplayMode::OriginalList); |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn rollup_display_mode_from_i64_unique_list() { |
| 24 | + assert_eq!(RollupDisplayMode::from(2), RollupDisplayMode::UniqueList); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn rollup_display_mode_from_i64_unknown_defaults_to_calculated() { |
| 29 | + assert_eq!(RollupDisplayMode::from(99), RollupDisplayMode::Calculated); |
| 30 | + assert_eq!(RollupDisplayMode::from(-1), RollupDisplayMode::Calculated); |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn rollup_display_mode_to_i64() { |
| 35 | + assert_eq!(i64::from(RollupDisplayMode::Calculated), 0); |
| 36 | + assert_eq!(i64::from(RollupDisplayMode::OriginalList), 1); |
| 37 | + assert_eq!(i64::from(RollupDisplayMode::UniqueList), 2); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn rollup_display_mode_default() { |
| 42 | + assert_eq!(RollupDisplayMode::default(), RollupDisplayMode::Calculated); |
| 43 | +} |
| 44 | + |
| 45 | +// ==================== RollupTypeOption Default Tests ==================== |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn rollup_type_option_default_values() { |
| 49 | + let option = RollupTypeOption::default(); |
| 50 | + assert_eq!(option.relation_field_id, ""); |
| 51 | + assert_eq!(option.target_field_id, ""); |
| 52 | + assert_eq!(option.calculation_type, 5); // Default to Count |
| 53 | + assert_eq!(option.show_as, RollupDisplayMode::Calculated); |
| 54 | + assert_eq!(option.condition_value, ""); |
| 55 | +} |
| 56 | + |
| 57 | +// ==================== RollupTypeOption Serialization Tests ==================== |
| 58 | + |
| 59 | +#[test] |
| 60 | +fn rollup_type_option_to_type_option_data() { |
| 61 | + let option = RollupTypeOption { |
| 62 | + relation_field_id: "rel_field_1".to_string(), |
| 63 | + target_field_id: "target_field_1".to_string(), |
| 64 | + calculation_type: 3, |
| 65 | + show_as: RollupDisplayMode::OriginalList, |
| 66 | + condition_value: "some_condition".to_string(), |
| 67 | + }; |
| 68 | + |
| 69 | + let data: TypeOptionData = option.into(); |
| 70 | + |
| 71 | + assert_eq!( |
| 72 | + data.get_as::<String>("relation_field_id").unwrap(), |
| 73 | + "rel_field_1" |
| 74 | + ); |
| 75 | + assert_eq!( |
| 76 | + data.get_as::<String>("target_field_id").unwrap(), |
| 77 | + "target_field_1" |
| 78 | + ); |
| 79 | + assert_eq!(data.get_as::<i64>("calculation_type").unwrap(), 3); |
| 80 | + assert_eq!(data.get_as::<i64>("show_as").unwrap(), 1); |
| 81 | + assert_eq!( |
| 82 | + data.get_as::<String>("condition_value").unwrap(), |
| 83 | + "some_condition" |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn rollup_type_option_from_type_option_data() { |
| 89 | + let mut data = TypeOptionData::new(); |
| 90 | + data.insert( |
| 91 | + "relation_field_id".to_string(), |
| 92 | + Any::String("rel_field_2".into()), |
| 93 | + ); |
| 94 | + data.insert( |
| 95 | + "target_field_id".to_string(), |
| 96 | + Any::String("target_field_2".into()), |
| 97 | + ); |
| 98 | + data.insert("calculation_type".to_string(), Any::BigInt(7)); |
| 99 | + data.insert("show_as".to_string(), Any::BigInt(2)); |
| 100 | + data.insert( |
| 101 | + "condition_value".to_string(), |
| 102 | + Any::String("condition_2".into()), |
| 103 | + ); |
| 104 | + |
| 105 | + let option = RollupTypeOption::from(data); |
| 106 | + |
| 107 | + assert_eq!(option.relation_field_id, "rel_field_2"); |
| 108 | + assert_eq!(option.target_field_id, "target_field_2"); |
| 109 | + assert_eq!(option.calculation_type, 7); |
| 110 | + assert_eq!(option.show_as, RollupDisplayMode::UniqueList); |
| 111 | + assert_eq!(option.condition_value, "condition_2"); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn rollup_type_option_from_empty_type_option_data() { |
| 116 | + let data = TypeOptionData::new(); |
| 117 | + let option = RollupTypeOption::from(data); |
| 118 | + |
| 119 | + // Should use default values when data is missing |
| 120 | + assert_eq!(option.relation_field_id, ""); |
| 121 | + assert_eq!(option.target_field_id, ""); |
| 122 | + assert_eq!(option.calculation_type, 5); // Default to Count |
| 123 | + assert_eq!(option.show_as, RollupDisplayMode::Calculated); |
| 124 | + assert_eq!(option.condition_value, ""); |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn rollup_type_option_roundtrip() { |
| 129 | + let original = RollupTypeOption { |
| 130 | + relation_field_id: "rel_123".to_string(), |
| 131 | + target_field_id: "target_456".to_string(), |
| 132 | + calculation_type: 10, |
| 133 | + show_as: RollupDisplayMode::UniqueList, |
| 134 | + condition_value: "my_condition".to_string(), |
| 135 | + }; |
| 136 | + |
| 137 | + let data: TypeOptionData = original.clone().into(); |
| 138 | + let restored = RollupTypeOption::from(data); |
| 139 | + |
| 140 | + assert_eq!(restored.relation_field_id, original.relation_field_id); |
| 141 | + assert_eq!(restored.target_field_id, original.target_field_id); |
| 142 | + assert_eq!(restored.calculation_type, original.calculation_type); |
| 143 | + assert_eq!(restored.show_as, original.show_as); |
| 144 | + assert_eq!(restored.condition_value, original.condition_value); |
| 145 | +} |
| 146 | + |
| 147 | +// ==================== TypeOptionCellReader Tests ==================== |
| 148 | + |
| 149 | +#[test] |
| 150 | +fn rollup_type_option_json_cell() { |
| 151 | + let option = RollupTypeOption::default(); |
| 152 | + let mut cell = Cell::new(); |
| 153 | + cell.insert(CELL_DATA.to_string(), Any::String("test_value".into())); |
| 154 | + |
| 155 | + let json = option.json_cell(&cell); |
| 156 | + assert_eq!(json, json!("test_value")); |
| 157 | +} |
| 158 | + |
| 159 | +#[test] |
| 160 | +fn rollup_type_option_json_cell_empty() { |
| 161 | + let option = RollupTypeOption::default(); |
| 162 | + let cell = Cell::new(); |
| 163 | + |
| 164 | + let json = option.json_cell(&cell); |
| 165 | + assert_eq!(json, json!("")); |
| 166 | +} |
| 167 | + |
| 168 | +#[test] |
| 169 | +fn rollup_type_option_numeric_cell_valid() { |
| 170 | + let option = RollupTypeOption::default(); |
| 171 | + let mut cell = Cell::new(); |
| 172 | + cell.insert(CELL_DATA.to_string(), Any::String("42.5".into())); |
| 173 | + |
| 174 | + let numeric = option.numeric_cell(&cell); |
| 175 | + assert_eq!(numeric, Some(42.5)); |
| 176 | +} |
| 177 | + |
| 178 | +#[test] |
| 179 | +fn rollup_type_option_numeric_cell_invalid() { |
| 180 | + let option = RollupTypeOption::default(); |
| 181 | + let mut cell = Cell::new(); |
| 182 | + cell.insert(CELL_DATA.to_string(), Any::String("not_a_number".into())); |
| 183 | + |
| 184 | + let numeric = option.numeric_cell(&cell); |
| 185 | + assert_eq!(numeric, None); |
| 186 | +} |
| 187 | + |
| 188 | +#[test] |
| 189 | +fn rollup_type_option_convert_raw_cell_data() { |
| 190 | + let option = RollupTypeOption::default(); |
| 191 | + let result = option.convert_raw_cell_data("raw_data_test"); |
| 192 | + assert_eq!(result, "raw_data_test"); |
| 193 | +} |
| 194 | + |
| 195 | +// ==================== TypeOptionCellWriter Tests ==================== |
| 196 | + |
| 197 | +#[test] |
| 198 | +fn rollup_type_option_convert_json_string_to_cell() { |
| 199 | + let option = RollupTypeOption::default(); |
| 200 | + let json_value = json!("hello world"); |
| 201 | + |
| 202 | + let cell = option.convert_json_to_cell(json_value); |
| 203 | + |
| 204 | + assert_eq!( |
| 205 | + cell.get(CELL_DATA).unwrap(), |
| 206 | + &Any::String("hello world".into()) |
| 207 | + ); |
| 208 | +} |
| 209 | + |
| 210 | +#[test] |
| 211 | +fn rollup_type_option_convert_json_number_to_cell() { |
| 212 | + let option = RollupTypeOption::default(); |
| 213 | + let json_value = json!(123); |
| 214 | + |
| 215 | + let cell = option.convert_json_to_cell(json_value); |
| 216 | + |
| 217 | + assert_eq!(cell.get(CELL_DATA).unwrap(), &Any::String("123".into())); |
| 218 | +} |
| 219 | + |
| 220 | +#[test] |
| 221 | +fn rollup_type_option_convert_json_object_to_cell() { |
| 222 | + let option = RollupTypeOption::default(); |
| 223 | + let json_value = json!({"key": "value"}); |
| 224 | + |
| 225 | + let cell = option.convert_json_to_cell(json_value); |
| 226 | + |
| 227 | + assert_eq!( |
| 228 | + cell.get(CELL_DATA).unwrap(), |
| 229 | + &Any::String("{\"key\":\"value\"}".into()) |
| 230 | + ); |
| 231 | +} |
| 232 | + |
| 233 | +#[test] |
| 234 | +fn rollup_type_option_convert_json_array_to_cell() { |
| 235 | + let option = RollupTypeOption::default(); |
| 236 | + let json_value = json!([1, 2, 3]); |
| 237 | + |
| 238 | + let cell = option.convert_json_to_cell(json_value); |
| 239 | + |
| 240 | + assert_eq!( |
| 241 | + cell.get(CELL_DATA).unwrap(), |
| 242 | + &Any::String("[1,2,3]".into()) |
| 243 | + ); |
| 244 | +} |
| 245 | + |
| 246 | +// ==================== FieldType::Rollup Tests ==================== |
| 247 | + |
| 248 | +#[test] |
| 249 | +fn field_type_rollup_value() { |
| 250 | + assert_eq!(FieldType::Rollup as i64, 16); |
| 251 | +} |
| 252 | + |
| 253 | +#[test] |
| 254 | +fn field_type_from_i64_rollup() { |
| 255 | + let field_type = FieldType::from(16_i64); |
| 256 | + assert_eq!(field_type, FieldType::Rollup); |
| 257 | +} |
| 258 | + |
| 259 | +#[test] |
| 260 | +fn field_type_rollup_is_rollup() { |
| 261 | + assert!(FieldType::Rollup.is_rollup()); |
| 262 | + assert!(!FieldType::RichText.is_rollup()); |
| 263 | + assert!(!FieldType::Relation.is_rollup()); |
| 264 | +} |
| 265 | + |
| 266 | +#[test] |
| 267 | +fn field_type_rollup_default_name() { |
| 268 | + assert_eq!(FieldType::Rollup.default_name(), "Rollup"); |
| 269 | +} |
| 270 | + |
| 271 | +#[test] |
| 272 | +fn field_type_rollup_default_type_option_data() { |
| 273 | + let data = default_type_option_data_from_type(FieldType::Rollup); |
| 274 | + let option = RollupTypeOption::from(data); |
| 275 | + |
| 276 | + // Verify it creates a valid default RollupTypeOption |
| 277 | + assert_eq!(option.relation_field_id, ""); |
| 278 | + assert_eq!(option.target_field_id, ""); |
| 279 | + assert_eq!(option.calculation_type, 5); |
| 280 | + assert_eq!(option.show_as, RollupDisplayMode::Calculated); |
| 281 | + assert_eq!(option.condition_value, ""); |
| 282 | +} |
0 commit comments