Skip to content

Commit 69b9b46

Browse files
GavenKamtyt2y3
andauthored
Fix DeriveIntoActiveModel (#2926)
* Fix IntoActiveModel * Add test cases for IntoActiveModel * Modify custom_active_model test * Modify custom_active_model test * Fix integration tests * Fix integration tests * Fix sync --------- Co-authored-by: Chris Tsang <[email protected]>
1 parent ed9b8e8 commit 69b9b46

File tree

8 files changed

+160
-20
lines changed

8 files changed

+160
-20
lines changed

sea-orm-sync/src/entity/active_model.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,12 +1127,12 @@ mod tests {
11271127

11281128
#[derive(DeriveIntoActiveModel)]
11291129
#[sea_orm(active_model = "fruit::ActiveModel")]
1130-
struct FruitName {
1130+
struct RequiredFruitName {
11311131
name: String,
11321132
}
11331133

11341134
assert_eq!(
1135-
FruitName {
1135+
RequiredFruitName {
11361136
name: "Apple Pie".to_owned(),
11371137
}
11381138
.into_active_model(),
@@ -1143,14 +1143,80 @@ mod tests {
11431143
}
11441144
);
11451145

1146+
#[derive(DeriveIntoActiveModel)]
1147+
#[sea_orm(active_model = "fruit::ActiveModel")]
1148+
struct OptionalFruitName {
1149+
name: Option<String>,
1150+
}
1151+
1152+
assert_eq!(
1153+
OptionalFruitName {
1154+
name: Some("Apple Pie".to_owned()),
1155+
}
1156+
.into_active_model(),
1157+
fruit::ActiveModel {
1158+
id: NotSet,
1159+
name: Set("Apple Pie".to_owned()),
1160+
cake_id: NotSet,
1161+
}
1162+
);
1163+
1164+
assert_eq!(
1165+
OptionalFruitName { name: None }.into_active_model(),
1166+
fruit::ActiveModel {
1167+
id: NotSet,
1168+
name: NotSet,
1169+
cake_id: NotSet,
1170+
}
1171+
);
1172+
1173+
#[derive(DeriveIntoActiveModel)]
1174+
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1175+
struct RequiredAndNotNullFruitCake {
1176+
cake_id: i32,
1177+
}
1178+
1179+
assert_eq!(
1180+
RequiredAndNotNullFruitCake { cake_id: 1 }.into_active_model(),
1181+
fruit::ActiveModel {
1182+
id: NotSet,
1183+
name: NotSet,
1184+
cake_id: Set(Some(1)),
1185+
}
1186+
);
1187+
1188+
#[derive(DeriveIntoActiveModel)]
1189+
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1190+
struct OptionalAndNotNullFruitCake {
1191+
cake_id: Option<i32>,
1192+
}
1193+
1194+
assert_eq!(
1195+
OptionalAndNotNullFruitCake { cake_id: Some(1) }.into_active_model(),
1196+
fruit::ActiveModel {
1197+
id: NotSet,
1198+
name: NotSet,
1199+
cake_id: Set(Some(1)),
1200+
}
1201+
);
1202+
1203+
assert_eq!(
1204+
OptionalAndNotNullFruitCake { cake_id: None }.into_active_model(),
1205+
fruit::ActiveModel {
1206+
id: NotSet,
1207+
name: NotSet,
1208+
cake_id: NotSet,
1209+
}
1210+
);
1211+
11461212
#[derive(DeriveIntoActiveModel)]
11471213
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1148-
struct FruitCake {
1214+
struct OptionalAndNullableFruitCake {
11491215
cake_id: Option<Option<i32>>,
11501216
}
11511217

11521218
assert_eq!(
1153-
FruitCake {
1219+
OptionalAndNullableFruitCake {
11541220
cake_id: Some(Some(1)),
11551221
}
11561222
.into_active_model(),
@@ -1162,7 +1228,7 @@ mod tests {
11621228
);
11631229

11641230
assert_eq!(
1165-
FruitCake {
1231+
OptionalAndNullableFruitCake {
11661232
cake_id: Some(None),
11671233
}
11681234
.into_active_model(),
@@ -1174,7 +1240,7 @@ mod tests {
11741240
);
11751241

11761242
assert_eq!(
1177-
FruitCake { cake_id: None }.into_active_model(),
1243+
OptionalAndNullableFruitCake { cake_id: None }.into_active_model(),
11781244
fruit::ActiveModel {
11791245
id: NotSet,
11801246
name: NotSet,

sea-orm-sync/src/entity/active_value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ where
107107
fn into_active_value(self) -> ActiveValue<V>;
108108
}
109109

110-
impl<V> IntoActiveValue<Option<V>> for Option<V>
110+
impl<V> IntoActiveValue<V> for Option<V>
111111
where
112112
V: IntoActiveValue<V> + Into<Value> + Nullable,
113113
{
114-
fn into_active_value(self) -> ActiveValue<Option<V>> {
114+
fn into_active_value(self) -> ActiveValue<V> {
115115
match self {
116-
Some(value) => Set(Some(value)),
116+
Some(value) => Set(value),
117117
None => NotSet,
118118
}
119119
}

sea-orm-sync/tests/common/features/custom_active_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use sea_orm::{ActiveValue, IntoActiveValue};
88
pub struct Model {
99
#[sea_orm(primary_key)]
1010
pub id: i32,
11+
pub age: i32,
1112
pub weight: Option<f32>,
1213
pub amount: Option<i32>,
14+
pub tea: Tea,
1315
pub category: Option<Category>,
1416
pub color: Option<Color>,
1517
}
@@ -21,8 +23,10 @@ impl ActiveModelBehavior for ActiveModel {}
2123

2224
#[derive(Clone, Debug, PartialEq, DeriveIntoActiveModel)]
2325
pub struct CustomActiveModel {
26+
pub age: Option<i32>,
2427
pub weight: Option<f32>,
2528
pub amount: Option<Option<i32>>,
29+
pub tea: Option<Tea>,
2630
pub category: Option<Category>,
2731
pub color: Option<Option<Color>>,
2832
}

sea-orm-sync/tests/type_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn it_impl_try_from_u64<T: TryFromU64>() {}
2626
macro_rules! it_impl_traits {
2727
( $ty: ty ) => {
2828
it_impl_into_active_value::<$ty, $ty>();
29-
it_impl_into_active_value::<Option<$ty>, Option<$ty>>();
29+
it_impl_into_active_value::<Option<$ty>, $ty>();
3030
it_impl_into_active_value::<Option<Option<$ty>>, Option<$ty>>();
3131

3232
it_impl_try_getable::<$ty>();

src/entity/active_model.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,12 @@ mod tests {
11461146

11471147
#[derive(DeriveIntoActiveModel)]
11481148
#[sea_orm(active_model = "fruit::ActiveModel")]
1149-
struct FruitName {
1149+
struct RequiredFruitName {
11501150
name: String,
11511151
}
11521152

11531153
assert_eq!(
1154-
FruitName {
1154+
RequiredFruitName {
11551155
name: "Apple Pie".to_owned(),
11561156
}
11571157
.into_active_model(),
@@ -1162,14 +1162,80 @@ mod tests {
11621162
}
11631163
);
11641164

1165+
#[derive(DeriveIntoActiveModel)]
1166+
#[sea_orm(active_model = "fruit::ActiveModel")]
1167+
struct OptionalFruitName {
1168+
name: Option<String>,
1169+
}
1170+
1171+
assert_eq!(
1172+
OptionalFruitName {
1173+
name: Some("Apple Pie".to_owned()),
1174+
}
1175+
.into_active_model(),
1176+
fruit::ActiveModel {
1177+
id: NotSet,
1178+
name: Set("Apple Pie".to_owned()),
1179+
cake_id: NotSet,
1180+
}
1181+
);
1182+
1183+
assert_eq!(
1184+
OptionalFruitName { name: None }.into_active_model(),
1185+
fruit::ActiveModel {
1186+
id: NotSet,
1187+
name: NotSet,
1188+
cake_id: NotSet,
1189+
}
1190+
);
1191+
1192+
#[derive(DeriveIntoActiveModel)]
1193+
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1194+
struct RequiredAndNotNullFruitCake {
1195+
cake_id: i32,
1196+
}
1197+
1198+
assert_eq!(
1199+
RequiredAndNotNullFruitCake { cake_id: 1 }.into_active_model(),
1200+
fruit::ActiveModel {
1201+
id: NotSet,
1202+
name: NotSet,
1203+
cake_id: Set(Some(1)),
1204+
}
1205+
);
1206+
1207+
#[derive(DeriveIntoActiveModel)]
1208+
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1209+
struct OptionalAndNotNullFruitCake {
1210+
cake_id: Option<i32>,
1211+
}
1212+
1213+
assert_eq!(
1214+
OptionalAndNotNullFruitCake { cake_id: Some(1) }.into_active_model(),
1215+
fruit::ActiveModel {
1216+
id: NotSet,
1217+
name: NotSet,
1218+
cake_id: Set(Some(1)),
1219+
}
1220+
);
1221+
1222+
assert_eq!(
1223+
OptionalAndNotNullFruitCake { cake_id: None }.into_active_model(),
1224+
fruit::ActiveModel {
1225+
id: NotSet,
1226+
name: NotSet,
1227+
cake_id: NotSet,
1228+
}
1229+
);
1230+
11651231
#[derive(DeriveIntoActiveModel)]
11661232
#[sea_orm(active_model = "<fruit::Entity as EntityTrait>::ActiveModel")]
1167-
struct FruitCake {
1233+
struct OptionalAndNullableFruitCake {
11681234
cake_id: Option<Option<i32>>,
11691235
}
11701236

11711237
assert_eq!(
1172-
FruitCake {
1238+
OptionalAndNullableFruitCake {
11731239
cake_id: Some(Some(1)),
11741240
}
11751241
.into_active_model(),
@@ -1181,7 +1247,7 @@ mod tests {
11811247
);
11821248

11831249
assert_eq!(
1184-
FruitCake {
1250+
OptionalAndNullableFruitCake {
11851251
cake_id: Some(None),
11861252
}
11871253
.into_active_model(),
@@ -1193,7 +1259,7 @@ mod tests {
11931259
);
11941260

11951261
assert_eq!(
1196-
FruitCake { cake_id: None }.into_active_model(),
1262+
OptionalAndNullableFruitCake { cake_id: None }.into_active_model(),
11971263
fruit::ActiveModel {
11981264
id: NotSet,
11991265
name: NotSet,

src/entity/active_value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ where
107107
fn into_active_value(self) -> ActiveValue<V>;
108108
}
109109

110-
impl<V> IntoActiveValue<Option<V>> for Option<V>
110+
impl<V> IntoActiveValue<V> for Option<V>
111111
where
112112
V: IntoActiveValue<V> + Into<Value> + Nullable,
113113
{
114-
fn into_active_value(self) -> ActiveValue<Option<V>> {
114+
fn into_active_value(self) -> ActiveValue<V> {
115115
match self {
116-
Some(value) => Set(Some(value)),
116+
Some(value) => Set(value),
117117
None => NotSet,
118118
}
119119
}

tests/common/features/custom_active_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use sea_orm::{ActiveValue, IntoActiveValue};
88
pub struct Model {
99
#[sea_orm(primary_key)]
1010
pub id: i32,
11+
pub age: i32,
1112
pub weight: Option<f32>,
1213
pub amount: Option<i32>,
14+
pub tea: Tea,
1315
pub category: Option<Category>,
1416
pub color: Option<Color>,
1517
}
@@ -21,8 +23,10 @@ impl ActiveModelBehavior for ActiveModel {}
2123

2224
#[derive(Clone, Debug, PartialEq, DeriveIntoActiveModel)]
2325
pub struct CustomActiveModel {
26+
pub age: Option<i32>,
2427
pub weight: Option<f32>,
2528
pub amount: Option<Option<i32>>,
29+
pub tea: Option<Tea>,
2630
pub category: Option<Category>,
2731
pub color: Option<Option<Color>>,
2832
}

tests/type_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn it_impl_try_from_u64<T: TryFromU64>() {}
2626
macro_rules! it_impl_traits {
2727
( $ty: ty ) => {
2828
it_impl_into_active_value::<$ty, $ty>();
29-
it_impl_into_active_value::<Option<$ty>, Option<$ty>>();
29+
it_impl_into_active_value::<Option<$ty>, $ty>();
3030
it_impl_into_active_value::<Option<Option<$ty>>, Option<$ty>>();
3131

3232
it_impl_try_getable::<$ty>();

0 commit comments

Comments
 (0)