Skip to content

Commit

Permalink
Test cases improvement (#1811)
Browse files Browse the repository at this point in the history
* adds find_with_linked test

* WIP(related test)

* mock related test done

* complete relation test

* loader update

* find_with/also_related missing test case for empty from other side

* comments fixup

* revert loader test

* related select test done

* find with/also linked test cases

* removed due to it being functionally same as the new one

* fmt, remove excess import

* improved model generation

* issue related test case #1790

* added loader test cases and slight improvement to find_related/linked

* miscellaneous changes

* added empty insert, merge load_one test case

* completed loader many to many test case, fmt

* removed empty_insert test case for now

* commented insert_test

* added Cargo.toml for issue 1790's folder

* buffed salvo version for ci(0.49 yanked)

* revert version for salvo example
  • Loading branch information
darkmmon committed Aug 18, 2023
1 parent 2a8efed commit d50312c
Show file tree
Hide file tree
Showing 13 changed files with 1,251 additions and 61 deletions.
18 changes: 18 additions & 0 deletions issues/1790/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]
# A separate workspace

[package]
name = "sea-orm-issues-1790"
version = "0.1.0"
edition = "2023"
publish = false

[dependencies]
anyhow = "1"
serde = "1"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }

[dependencies.sea-orm]
path = "../../"
default-features = false
features = ["macros", "runtime-tokio-native-tls", "sqlx-sqlite"]
58 changes: 58 additions & 0 deletions issues/1790/insert_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
mod tests {
// currently ok
#[test]
fn insert_do_nothing_postgres() {
assert_eq!(
Insert::<cake::ActiveModel>::new()
.add(cake::Model {
id: 1,
name: "Apple Pie".to_owned(),
})
.on_conflict(OnConflict::new()
.do_nothing()
.to_owned()
)
.build(DbBackend::Postgres)
.to_string(),
r#"INSERT INTO "cake" ("id", "name") VALUES (1, 'Apple Pie') ON CONFLICT DO NOTHING"#,
);
}

//failed to run
#[test]
fn insert_do_nothing_mysql() {
assert_eq!(
Insert::<cake::ActiveModel>::new()
.add(cake::Model {
id: 1,
name: "Apple Pie".to_owned(),
})
.on_conflict(OnConflict::new()
.do_nothing()
.to_owned()
)
.build(DbBackend::Mysql)
.to_string(),
r#"INSERT IGNORE INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#,
);
}

// currently ok
#[test]
fn insert_do_nothing() {
assert_eq!(
Insert::<cake::ActiveModel>::new()
.add(cake::Model {
id: 1,
name: "Apple Pie".to_owned(),
})
.on_conflict(OnConflict::new()
.do_nothing()
.to_owned()
)
.build(DbBackend::Sqlite)
.to_string(),
r#"INSERT IGNORE INTO "cake" ("id", "name") VALUES (1, 'Apple Pie')"#,
);
}
}
27 changes: 27 additions & 0 deletions src/database/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,33 @@ where
}
}

impl<M, N> IntoMockRow for (M, Option<N>)
where
M: ModelTrait,
N: ModelTrait,
{
fn into_mock_row(self) -> MockRow {
let mut mapped_join = BTreeMap::new();

for column in <<M as ModelTrait>::Entity as EntityTrait>::Column::iter() {
mapped_join.insert(
format!("{}{}", SelectA.as_str(), column.as_str()),
self.0.get(column),
);
}
if let Some(b_entity) = self.1 {
for column in <<N as ModelTrait>::Entity as EntityTrait>::Column::iter() {
mapped_join.insert(
format!("{}{}", SelectB.as_str(), column.as_str()),
b_entity.get(column),
);
}
}

mapped_join.into_mock_row()
}
}

impl<T> IntoMockRow for BTreeMap<T, Value>
where
T: Into<String>,
Expand Down
1 change: 0 additions & 1 deletion src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,6 @@ mod tests {
);
}

delete_by_id("UUID".to_string());
delete_by_id("UUID".to_string());
delete_by_id("UUID");
delete_by_id(Cow::from("UUID"));
Expand Down
Loading

0 comments on commit d50312c

Please sign in to comment.