Skip to content

Commit b5c4208

Browse files
committed
chore: fix media type option
1 parent 95edca3 commit b5c4208

File tree

3 files changed

+130
-28
lines changed

3 files changed

+130
-28
lines changed

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::fields::{
55
};
66
use crate::rows::{new_cell_builder, Cell};
77

8+
use crate::error::DatabaseError;
89
use crate::template::entity::CELL_DATA;
910
use crate::template::util::TypeOptionCellData;
1011
use collab::util::AnyMapExt;
@@ -13,6 +14,7 @@ use serde_json::{json, Value};
1314
use serde_repr::Serialize_repr;
1415
use std::fmt::{Display, Formatter};
1516
use std::path::Path;
17+
use std::str::FromStr;
1618
use std::sync::Arc;
1719
use yrs::Any;
1820

@@ -144,6 +146,22 @@ impl ToString for MediaCellData {
144146
}
145147
}
146148

149+
impl FromStr for MediaCellData {
150+
type Err = DatabaseError;
151+
152+
fn from_str(s: &str) -> Result<Self, Self::Err> {
153+
if s.is_empty() {
154+
return Ok(MediaCellData { files: vec![] });
155+
}
156+
let files = s
157+
.split(", ")
158+
.map(|file: &str| serde_json::from_str::<MediaFile>(file).unwrap_or_default())
159+
.collect::<Vec<_>>();
160+
161+
Ok(MediaCellData { files })
162+
}
163+
}
164+
147165
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
148166
pub struct MediaFile {
149167
pub id: String,
@@ -348,6 +366,44 @@ impl<'de> Deserialize<'de> for MediaUploadType {
348366
mod tests {
349367
use super::*;
350368
use serde_json::json;
369+
#[test]
370+
fn test_is_cell_empty() {
371+
let empty_media_cell_data = MediaCellData { files: vec![] };
372+
assert!(empty_media_cell_data.is_cell_empty());
373+
374+
let non_empty_media_cell_data = MediaCellData {
375+
files: vec![MediaFile::new(
376+
"file1.jpg".to_string(),
377+
"http://example.com/file1.jpg".to_string(),
378+
MediaUploadType::Local,
379+
MediaFileType::Image,
380+
)],
381+
};
382+
assert!(!non_empty_media_cell_data.is_cell_empty());
383+
}
384+
385+
#[test]
386+
fn test_media_file_rename() {
387+
let original = MediaFile::new(
388+
"original_name.jpg".to_string(),
389+
"http://example.com/file.jpg".to_string(),
390+
MediaUploadType::Local,
391+
MediaFileType::Image,
392+
);
393+
394+
let renamed = original.rename("new_name.jpg".to_string());
395+
assert_eq!(renamed.name, "new_name.jpg");
396+
assert_eq!(renamed.url, original.url);
397+
assert_eq!(renamed.upload_type, original.upload_type);
398+
assert_eq!(renamed.file_type, original.file_type);
399+
}
400+
401+
#[test]
402+
fn test_invalid_json_deserialization() {
403+
let invalid_json = json!("InvalidType");
404+
assert!(serde_json::from_value::<MediaUploadType>(invalid_json.clone()).is_err());
405+
assert!(serde_json::from_value::<MediaFileType>(invalid_json).is_err());
406+
}
351407

352408
#[test]
353409
fn test_media_cell_data_to_string() {
@@ -467,4 +523,56 @@ mod tests {
467523
MediaFileType::Text
468524
);
469525
}
526+
527+
#[test]
528+
fn test_convert_raw_cell_data() {
529+
let media_type_option = MediaTypeOption::default();
530+
531+
// Test with valid JSON data
532+
let valid_data =
533+
r#"{"files":[{"id":"1","name":"file1","url":"url1","upload_type":0,"file_type":1}]}"#;
534+
assert_eq!(
535+
media_type_option.convert_raw_cell_data(valid_data),
536+
"file1".to_string()
537+
);
538+
539+
// Test with invalid JSON data
540+
let invalid_data = "invalid_json";
541+
assert_eq!(
542+
media_type_option.convert_raw_cell_data(invalid_data),
543+
"".to_string()
544+
);
545+
546+
// Test with empty string
547+
let empty_data = "";
548+
assert_eq!(
549+
media_type_option.convert_raw_cell_data(empty_data),
550+
"".to_string()
551+
);
552+
553+
// Test with valid JSON but missing "files" field
554+
let missing_field_data = r#"{"other_field":[{"id":"1"}]}"#;
555+
assert_eq!(
556+
media_type_option.convert_raw_cell_data(missing_field_data),
557+
"".to_string()
558+
);
559+
560+
// Test with valid JSON but incorrect structure
561+
let incorrect_structure_data = r#"{"files":"not_an_array"}"#;
562+
assert_eq!(
563+
media_type_option.convert_raw_cell_data(incorrect_structure_data),
564+
"".to_string()
565+
);
566+
}
567+
568+
#[test]
569+
fn test_numeric_cell_conversion() {
570+
let mut cell = new_cell_builder(FieldType::Media);
571+
cell.insert(CELL_DATA.into(), "123.45".to_string().into());
572+
573+
let media_type_option = MediaTypeOption::default();
574+
let numeric_value = media_type_option.numeric_cell(&cell);
575+
576+
assert_eq!(numeric_value, Some(123.45));
577+
}
470578
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::template::relation_parse::RelationCellData;
55
use collab::util::AnyMapExt;
66
use serde::{Deserialize, Serialize};
77
use serde_json::{json, Value};
8+
use std::str::FromStr;
89

910
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1011
pub struct RelationTypeOption {
@@ -35,13 +36,8 @@ impl TypeOptionCellReader for RelationTypeOption {
3536
}
3637

3738
fn convert_raw_cell_data(&self, cell_data: &str) -> String {
38-
let cell_data = RelationCellData::from(cell_data);
39-
cell_data
40-
.row_ids
41-
.into_iter()
42-
.map(|id| id.to_string())
43-
.collect::<Vec<_>>()
44-
.join(", ")
39+
let cell_data = RelationCellData::from_str(cell_data).unwrap_or_default();
40+
cell_data.to_string()
4541
}
4642
}
4743

collab-database/src/template/relation_parse.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::entity::FieldType;
2+
use std::str::FromStr;
23

4+
use crate::error::DatabaseError;
35
use crate::rows::{new_cell_builder, Cell, RowId};
46
use crate::template::entity::CELL_DATA;
57
use crate::template::util::TypeOptionCellData;
@@ -12,6 +14,23 @@ pub struct RelationCellData {
1214
pub row_ids: Vec<RowId>,
1315
}
1416

17+
impl FromStr for RelationCellData {
18+
type Err = DatabaseError;
19+
20+
fn from_str(s: &str) -> Result<Self, Self::Err> {
21+
if s.is_empty() {
22+
return Ok(RelationCellData { row_ids: vec![] });
23+
}
24+
25+
let ids = s
26+
.split(", ")
27+
.map(|id| id.to_string().into())
28+
.collect::<Vec<_>>();
29+
30+
Ok(RelationCellData { row_ids: ids })
31+
}
32+
}
33+
1534
impl TypeOptionCellData for RelationCellData {
1635
fn is_cell_empty(&self) -> bool {
1736
self.row_ids.is_empty()
@@ -52,27 +71,6 @@ impl From<RelationCellData> for Cell {
5271
}
5372
}
5473

55-
impl From<&str> for RelationCellData {
56-
fn from(s: &str) -> Self {
57-
if s.is_empty() {
58-
return RelationCellData { row_ids: vec![] };
59-
}
60-
61-
let ids = s
62-
.split(", ")
63-
.map(|id| id.to_string().into())
64-
.collect::<Vec<_>>();
65-
66-
RelationCellData { row_ids: ids }
67-
}
68-
}
69-
70-
impl From<String> for RelationCellData {
71-
fn from(s: String) -> Self {
72-
RelationCellData::from(s.as_str())
73-
}
74-
}
75-
7674
impl ToString for RelationCellData {
7775
fn to_string(&self) -> String {
7876
self

0 commit comments

Comments
 (0)