use super::{MorphMany, MorphTo};
pub fn default_morph_type_column(morph: &str) -> String {
format!("{}_type", morph)
}
pub fn default_morph_id_column(morph: &str) -> String {
format!("{}_id", morph)
}
pub fn php_morph_many(
parent_class: &str,
child_table: &str,
morph: &str,
morph_type_value: Option<&str>,
morph_type_col: Option<&str>,
morph_id_col: Option<&str>,
) -> MorphMany {
MorphMany {
child_model: child_table.to_string(),
morph_type_column: morph_type_col
.map(String::from)
.unwrap_or_else(|| default_morph_type_column(morph)),
morph_id_column: morph_id_col
.map(String::from)
.unwrap_or_else(|| default_morph_id_column(morph)),
morph_type_value: morph_type_value
.map(String::from)
.unwrap_or_else(|| parent_class.to_string()),
}
}
pub fn php_morph_to(
morph: &str,
morph_type_col: Option<&str>,
morph_id_col: Option<&str>,
) -> MorphTo {
MorphTo {
morph_type_column: morph_type_col
.map(String::from)
.unwrap_or_else(|| default_morph_type_column(morph)),
morph_id_column: morph_id_col
.map(String::from)
.unwrap_or_else(|| default_morph_id_column(morph)),
}
}
pub fn morph_many_sql(
child_table: &str,
morph_type_col: &str,
morph_type_val: &str,
morph_id_col: &str,
parent_pk_value: &str,
) -> String {
format!(
"SELECT * FROM {} WHERE {} = '{}' AND {} = {}",
child_table, morph_type_col, morph_type_val, morph_id_col, parent_pk_value
)
}
pub fn morph_many_in_sql(
child_table: &str,
morph_type_col: &str,
morph_type_val: &str,
morph_id_col: &str,
parent_pk_values: &[&str],
) -> String {
if parent_pk_values.is_empty() {
return format!(
"SELECT * FROM {} WHERE {} IN (NULL) AND {} = '{}'",
child_table, morph_id_col, morph_type_col, morph_type_val
);
}
format!(
"SELECT * FROM {} WHERE {} IN ({}) AND {} = '{}'",
child_table,
morph_id_col,
parent_pk_values.join(", "),
morph_type_col,
morph_type_val
)
}
pub fn morph_to_sql(parent_table: &str, parent_pk_col: &str, morph_id_value: &str) -> String {
format!(
"SELECT * FROM {} WHERE {} = {}",
parent_table, parent_pk_col, morph_id_value
)
}
pub fn group_by_morph_type(
rows: &[serde_json::Value],
morph_type_field: &str,
morph_id_field: &str,
) -> std::collections::HashMap<String, Vec<String>> {
let mut grouped: std::collections::HashMap<String, Vec<String>> =
std::collections::HashMap::new();
for row in rows {
let obj = match row.as_object() {
Some(o) => o,
None => continue,
};
let morph_type = match obj.get(morph_type_field).and_then(|v| v.as_str()) {
Some(t) => t.to_string(),
None => continue,
};
let morph_id = match obj.get(morph_id_field) {
Some(v) => value_to_string(v),
None => continue,
};
if morph_id.is_empty() {
continue;
}
grouped.entry(morph_type).or_default().push(morph_id);
}
grouped
}
fn value_to_string(v: &serde_json::Value) -> String {
match v {
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => s.clone(),
serde_json::Value::Bool(b) => {
if *b {
"1".to_string()
} else {
"0".to_string()
}
}
_ => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_default_morph_type_column_simple() {
assert_eq!(default_morph_type_column("commentable"), "commentable_type");
assert_eq!(default_morph_type_column("imageable"), "imageable_type");
assert_eq!(default_morph_type_column("taggable"), "taggable_type");
}
#[test]
fn test_default_morph_type_column_multi_word() {
assert_eq!(
default_morph_type_column("comment_able"),
"comment_able_type"
);
}
#[test]
fn test_default_morph_id_column_simple() {
assert_eq!(default_morph_id_column("commentable"), "commentable_id");
assert_eq!(default_morph_id_column("imageable"), "imageable_id");
assert_eq!(default_morph_id_column("taggable"), "taggable_id");
}
#[test]
fn test_default_morph_id_column_multi_word() {
assert_eq!(default_morph_id_column("comment_able"), "comment_able_id");
}
#[test]
fn test_default_columns_consistent_with_php_convention() {
let morph = "commentable";
let type_col = default_morph_type_column(morph);
let id_col = default_morph_id_column(morph);
assert_eq!(
type_col.strip_suffix("_type").unwrap(),
id_col.strip_suffix("_id").unwrap()
);
}
#[test]
fn test_php_morph_many_all_defaults() {
let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
assert_eq!(rel.child_model, "comments");
assert_eq!(rel.morph_type_column, "commentable_type");
assert_eq!(rel.morph_id_column, "commentable_id");
assert_eq!(rel.morph_type_value, "Post");
}
#[test]
fn test_php_morph_many_explicit_morph_type_value() {
let rel = php_morph_many(
"Post",
"comments",
"commentable",
Some("CustomPost"),
None,
None,
);
assert_eq!(rel.morph_type_value, "CustomPost");
assert_eq!(rel.morph_type_column, "commentable_type"); assert_eq!(rel.morph_id_column, "commentable_id"); }
#[test]
fn test_php_morph_many_explicit_columns() {
let rel = php_morph_many(
"Post",
"comments",
"commentable",
None,
Some("c_type"),
Some("c_id"),
);
assert_eq!(rel.morph_type_column, "c_type");
assert_eq!(rel.morph_id_column, "c_id");
assert_eq!(rel.morph_type_value, "Post"); }
#[test]
fn test_php_morph_many_all_explicit() {
let rel = php_morph_many(
"Post",
"comments",
"commentable",
Some("CustomType"),
Some("c_type"),
Some("c_id"),
);
assert_eq!(rel.child_model, "comments");
assert_eq!(rel.morph_type_column, "c_type");
assert_eq!(rel.morph_id_column, "c_id");
assert_eq!(rel.morph_type_value, "CustomType");
}
#[test]
fn test_php_morph_many_with_namespace_parent() {
let rel = php_morph_many(
"app\\model\\Post",
"comments",
"commentable",
None,
None,
None,
);
assert_eq!(rel.morph_type_value, "app\\model\\Post");
}
#[test]
fn test_php_morph_many_multi_word_morph() {
let rel = php_morph_many("Post", "comments", "comment_able", None, None, None);
assert_eq!(rel.morph_type_column, "comment_able_type");
assert_eq!(rel.morph_id_column, "comment_able_id");
}
#[test]
fn test_php_morph_to_all_defaults() {
let rel = php_morph_to("commentable", None, None);
assert_eq!(rel.morph_type_column, "commentable_type");
assert_eq!(rel.morph_id_column, "commentable_id");
}
#[test]
fn test_php_morph_to_explicit_columns() {
let rel = php_morph_to("commentable", Some("c_type"), Some("c_id"));
assert_eq!(rel.morph_type_column, "c_type");
assert_eq!(rel.morph_id_column, "c_id");
}
#[test]
fn test_php_morph_to_only_type_col_explicit() {
let rel = php_morph_to("commentable", Some("c_type"), None);
assert_eq!(rel.morph_type_column, "c_type");
assert_eq!(rel.morph_id_column, "commentable_id"); }
#[test]
fn test_php_morph_to_only_id_col_explicit() {
let rel = php_morph_to("commentable", None, Some("c_id"));
assert_eq!(rel.morph_type_column, "commentable_type"); assert_eq!(rel.morph_id_column, "c_id");
}
#[test]
fn test_php_morph_to_multi_word_morph() {
let rel = php_morph_to("comment_able", None, None);
assert_eq!(rel.morph_type_column, "comment_able_type");
assert_eq!(rel.morph_id_column, "comment_able_id");
}
#[test]
fn test_php_morph_to_no_type_value_field() {
let rel = php_morph_to("commentable", None, None);
let _morph_type_column: &String = &rel.morph_type_column;
let _morph_id_column: &String = &rel.morph_id_column;
}
#[test]
fn test_morph_many_sql_numeric_pk() {
let sql = morph_many_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
"1",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
);
}
#[test]
fn test_morph_many_sql_string_pk() {
let sql = morph_many_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
"'abc-123'",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 'abc-123'"
);
}
#[test]
fn test_morph_many_sql_custom_columns() {
let sql = morph_many_sql("comments", "c_type", "Post", "c_id", "1");
assert_eq!(
sql,
"SELECT * FROM comments WHERE c_type = 'Post' AND c_id = 1"
);
}
#[test]
fn test_morph_many_sql_custom_type_value() {
let sql = morph_many_sql(
"comments",
"commentable_type",
"app\\model\\Post",
"commentable_id",
"1",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'app\\model\\Post' AND commentable_id = 1"
);
}
#[test]
fn test_morph_many_sql_aligns_sz_orm_core_pattern() {
let sql = morph_many_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
"1",
);
assert!(sql.starts_with(
"SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = "
));
}
#[test]
fn test_morph_many_in_sql_numeric_pks() {
let sql = morph_many_in_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
&["1", "2", "3"],
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
);
}
#[test]
fn test_morph_many_in_sql_single_pk() {
let sql = morph_many_in_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
&["1"],
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_id IN (1) AND commentable_type = 'Post'"
);
}
#[test]
fn test_morph_many_in_sql_empty_list() {
let sql = morph_many_in_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
&[],
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_id IN (NULL) AND commentable_type = 'Post'"
);
}
#[test]
fn test_morph_many_in_sql_custom_columns() {
let sql = morph_many_in_sql("comments", "c_type", "Post", "c_id", &["1", "2"]);
assert_eq!(
sql,
"SELECT * FROM comments WHERE c_id IN (1, 2) AND c_type = 'Post'"
);
}
#[test]
fn test_morph_many_in_sql_aligns_php_eagerly_result_set() {
let sql = morph_many_in_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
&["1", "2"],
);
let in_pos = sql.find("IN (1, 2)").unwrap();
let eq_pos = sql.find("= 'Post'").unwrap();
assert!(in_pos < eq_pos, "IN 条件应在 = 条件之前,对齐 PHP 顺序");
}
#[test]
fn test_morph_to_sql_numeric_id() {
let sql = morph_to_sql("posts", "id", "1");
assert_eq!(sql, "SELECT * FROM posts WHERE id = 1");
}
#[test]
fn test_morph_to_sql_string_id() {
let sql = morph_to_sql("posts", "id", "'abc-123'");
assert_eq!(sql, "SELECT * FROM posts WHERE id = 'abc-123'");
}
#[test]
fn test_morph_to_sql_custom_pk_col() {
let sql = morph_to_sql("posts", "pk", "1");
assert_eq!(sql, "SELECT * FROM posts WHERE pk = 1");
}
#[test]
fn test_morph_to_sql_dynamic_parent_table() {
let sql_post = morph_to_sql("posts", "id", "1");
let sql_video = morph_to_sql("videos", "id", "10");
let sql_image = morph_to_sql("images", "id", "100");
assert_eq!(sql_post, "SELECT * FROM posts WHERE id = 1");
assert_eq!(sql_video, "SELECT * FROM videos WHERE id = 10");
assert_eq!(sql_image, "SELECT * FROM images WHERE id = 100");
}
#[test]
fn test_morph_to_sql_aligns_sz_orm_core_pattern() {
let sql = morph_to_sql("posts", "id", "1");
assert!(sql.starts_with("SELECT * FROM posts WHERE id = "));
}
#[test]
fn test_group_by_morph_type_single_type() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Post", "commentable_id": 2}),
json!({"commentable_type": "Post", "commentable_id": 3}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 1);
assert_eq!(
grouped.get("Post").unwrap(),
&vec!["1".to_string(), "2".to_string(), "3".to_string()]
);
}
#[test]
fn test_group_by_morph_type_multiple_types() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Video", "commentable_id": 10}),
json!({"commentable_type": "Post", "commentable_id": 2}),
json!({"commentable_type": "Image", "commentable_id": 100}),
json!({"commentable_type": "Video", "commentable_id": 20}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 3);
assert_eq!(
grouped.get("Post").unwrap(),
&vec!["1".to_string(), "2".to_string()]
);
assert_eq!(
grouped.get("Video").unwrap(),
&vec!["10".to_string(), "20".to_string()]
);
assert_eq!(grouped.get("Image").unwrap(), &vec!["100".to_string()]);
}
#[test]
fn test_group_by_morph_type_skip_empty_id() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Post", "commentable_id": null}),
json!({"commentable_type": "Video", "commentable_id": 10}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 2);
assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
assert_eq!(grouped.get("Video").unwrap(), &vec!["10".to_string()]);
}
#[test]
fn test_group_by_morph_type_skip_missing_fields() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Post"}), json!({"commentable_id": 10}), json!({}), ];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 1);
assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
}
#[test]
fn test_group_by_morph_type_empty_rows() {
let rows: Vec<serde_json::Value> = vec![];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert!(grouped.is_empty());
}
#[test]
fn test_group_by_morph_type_string_ids() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": "abc-123"}),
json!({"commentable_type": "Post", "commentable_id": "def-456"}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(
grouped.get("Post").unwrap(),
&vec!["abc-123".to_string(), "def-456".to_string()]
);
}
#[test]
fn test_group_by_morph_type_non_object_rows() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!([1, 2, 3]), json!("string"), json!(42), json!(null), ];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 1);
assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
}
#[test]
fn test_group_by_morph_type_preserves_insertion_order_within_group() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 3}),
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Post", "commentable_id": 2}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(
grouped.get("Post").unwrap(),
&vec!["3".to_string(), "1".to_string(), "2".to_string()]
);
}
#[test]
fn test_value_to_string_integer() {
assert_eq!(value_to_string(&json!(42)), "42");
assert_eq!(value_to_string(&json!(0)), "0");
assert_eq!(value_to_string(&json!(-5)), "-5");
}
#[test]
fn test_value_to_string_float() {
assert_eq!(value_to_string(&json!(2.5)), "2.5");
}
#[test]
fn test_value_to_string_string() {
assert_eq!(value_to_string(&json!("abc")), "abc");
assert_eq!(value_to_string(&json!("")), "");
}
#[test]
fn test_value_to_string_bool() {
assert_eq!(value_to_string(&json!(true)), "1");
assert_eq!(value_to_string(&json!(false)), "0");
}
#[test]
fn test_value_to_string_null_and_complex() {
assert_eq!(value_to_string(&json!(null)), "");
assert_eq!(value_to_string(&json!({"a": 1})), "");
assert_eq!(value_to_string(&json!([1, 2])), "");
}
#[test]
fn test_r5_php_morph_many_default_column_convention() {
assert_eq!(default_morph_type_column("commentable"), "commentable_type");
assert_eq!(default_morph_id_column("commentable"), "commentable_id");
assert_eq!(default_morph_type_column("imageable"), "imageable_type");
assert_eq!(default_morph_id_column("imageable"), "imageable_id");
}
#[test]
fn test_r5_php_morph_many_default_type_is_parent_class() {
let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
assert_eq!(rel.morph_type_value, "Post");
}
#[test]
fn test_r5_php_morph_many_explicit_overrides_default() {
let rel = php_morph_many(
"Post",
"comments",
"commentable",
Some("CustomType"),
Some("c_type"),
Some("c_id"),
);
assert_eq!(rel.morph_type_value, "CustomType");
assert_eq!(rel.morph_type_column, "c_type");
assert_eq!(rel.morph_id_column, "c_id");
}
#[test]
fn test_r5_php_morph_to_no_type_parameter() {
let rel = php_morph_to("commentable", None, None);
let _type_col: String = rel.morph_type_column.clone();
let _id_col: String = rel.morph_id_column.clone();
}
#[test]
fn test_r5_php_morph_many_sql_pattern_matches_sz_orm_core() {
let sql = morph_many_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
"1",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
);
}
#[test]
fn test_r5_php_morph_to_sql_pattern_matches_sz_orm_core() {
let sql = morph_to_sql("posts", "id", "1");
assert_eq!(sql, "SELECT * FROM posts WHERE id = 1");
}
#[test]
fn test_r5_php_morph_many_in_sql_eagerly_pattern() {
let sql = morph_many_in_sql(
"comments",
"commentable_type",
"Post",
"commentable_id",
&["1", "2", "3"],
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
);
}
#[test]
fn test_r5_php_morph_to_group_by_morph_type_pattern() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Video", "commentable_id": 10}),
json!({"commentable_type": "Post", "commentable_id": 2}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 2);
assert_eq!(
grouped.get("Post").unwrap(),
&vec!["1".to_string(), "2".to_string()]
);
assert_eq!(grouped.get("Video").unwrap(), &vec!["10".to_string()]);
}
#[test]
fn test_r5_php_morph_to_empty_morph_key_skipped() {
let rows = vec![
json!({"commentable_type": "Post", "commentable_id": 1}),
json!({"commentable_type": "Post", "commentable_id": null}),
json!({"commentable_type": "Post", "commentable_id": ""}),
];
let grouped = group_by_morph_type(&rows, "commentable_type", "commentable_id");
assert_eq!(grouped.get("Post").unwrap(), &vec!["1".to_string()]);
}
#[test]
fn test_r5_php_morph_many_delegates_to_sz_orm_core() {
let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
let _: &MorphMany = &rel;
let relation = sz_orm_core::Relation::MorphMany(rel.clone());
assert!(matches!(relation, sz_orm_core::Relation::MorphMany(_)));
}
#[test]
fn test_r5_php_morph_to_delegates_to_sz_orm_core() {
let rel = php_morph_to("commentable", None, None);
let _: &MorphTo = &rel;
let relation = sz_orm_core::Relation::MorphTo(rel.clone());
assert!(matches!(relation, sz_orm_core::Relation::MorphTo(_)));
}
#[test]
fn test_integration_post_morph_many_comments() {
let rel = php_morph_many("Post", "comments", "commentable", None, None, None);
assert_eq!(rel.child_model, "comments");
assert_eq!(rel.morph_type_column, "commentable_type");
assert_eq!(rel.morph_id_column, "commentable_id");
assert_eq!(rel.morph_type_value, "Post");
let sql = morph_many_sql(
&rel.child_model,
&rel.morph_type_column,
&rel.morph_type_value,
&rel.morph_id_column,
"1",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'Post' AND commentable_id = 1"
);
let in_sql = morph_many_in_sql(
&rel.child_model,
&rel.morph_type_column,
&rel.morph_type_value,
&rel.morph_id_column,
&["1", "2", "3"],
);
assert_eq!(
in_sql,
"SELECT * FROM comments WHERE commentable_id IN (1, 2, 3) AND commentable_type = 'Post'"
);
}
#[test]
fn test_integration_video_morph_many_comments() {
let rel = php_morph_many("Video", "comments", "commentable", None, None, None);
assert_eq!(rel.morph_type_value, "Video");
assert_eq!(rel.morph_type_column, "commentable_type");
let sql = morph_many_sql(
&rel.child_model,
&rel.morph_type_column,
&rel.morph_type_value,
&rel.morph_id_column,
"10",
);
assert_eq!(
sql,
"SELECT * FROM comments WHERE commentable_type = 'Video' AND commentable_id = 10"
);
}
#[test]
fn test_integration_comment_morph_to_post_or_video() {
let rel = php_morph_to("commentable", None, None);
assert_eq!(rel.morph_type_column, "commentable_type");
assert_eq!(rel.morph_id_column, "commentable_id");
let sql_post = morph_to_sql("posts", "id", "1");
assert_eq!(sql_post, "SELECT * FROM posts WHERE id = 1");
let sql_video = morph_to_sql("videos", "id", "10");
assert_eq!(sql_video, "SELECT * FROM videos WHERE id = 10");
}
#[test]
fn test_integration_morph_to_batch_loading() {
let comments = vec![
json!({"id": 1, "commentable_type": "Post", "commentable_id": 1}),
json!({"id": 2, "commentable_type": "Video", "commentable_id": 10}),
json!({"id": 3, "commentable_type": "Post", "commentable_id": 2}),
json!({"id": 4, "commentable_type": "Image", "commentable_id": 100}),
];
let grouped = group_by_morph_type(&comments, "commentable_type", "commentable_id");
assert_eq!(grouped.len(), 3);
let post_ids: Vec<&str> = grouped
.get("Post")
.unwrap()
.iter()
.map(|s| s.as_str())
.collect();
let post_sql = format!("SELECT * FROM posts WHERE id IN ({})", post_ids.join(", "));
assert_eq!(post_sql, "SELECT * FROM posts WHERE id IN (1, 2)");
let video_ids: Vec<&str> = grouped
.get("Video")
.unwrap()
.iter()
.map(|s| s.as_str())
.collect();
let video_sql = format!(
"SELECT * FROM videos WHERE id IN ({})",
video_ids.join(", ")
);
assert_eq!(video_sql, "SELECT * FROM videos WHERE id IN (10)");
let image_ids: Vec<&str> = grouped
.get("Image")
.unwrap()
.iter()
.map(|s| s.as_str())
.collect();
let image_sql = format!(
"SELECT * FROM images WHERE id IN ({})",
image_ids.join(", ")
);
assert_eq!(image_sql, "SELECT * FROM images WHERE id IN (100)");
}
#[test]
fn test_integration_image_morph_many_tags() {
let rel = php_morph_many(
"Image",
"tags",
"taggable",
None,
Some("tag_type"),
Some("tag_id"),
);
assert_eq!(rel.child_model, "tags");
assert_eq!(rel.morph_type_column, "tag_type"); assert_eq!(rel.morph_id_column, "tag_id"); assert_eq!(rel.morph_type_value, "Image");
let sql = morph_many_sql(
&rel.child_model,
&rel.morph_type_column,
&rel.morph_type_value,
&rel.morph_id_column,
"100",
);
assert_eq!(
sql,
"SELECT * FROM tags WHERE tag_type = 'Image' AND tag_id = 100"
);
}
}