Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases improvement #1811

Merged
merged 23 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions issues/1790/insert_test.rs
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mod tests {
#[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"#,
);
}

#[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')"#,
);
}

#[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