Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
darkmmon committed Aug 22, 2023
1 parent b3002f0 commit a58d39a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/query/on_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,51 @@ impl OnConflict {
}
}

/// Set ON CONFLICT do nothing
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::insert()
/// .into_table(Glyph::Table)
/// .columns([Glyph::Aspect, Glyph::Image])
/// .values_panic(["abcd".into(), 3.1415.into()])
/// .on_conflict(
/// OnConflict::columns([Glyph::Id, Glyph::Aspect])
/// .do_nothing()
/// .to_owned(),
/// )
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// [
/// r#"INSERT IGNORE INTO `glyph` (`aspect`, `image`)"#,
/// r#"VALUES ('abcd', 3.1415)"#,
/// ]
/// .join(" ")
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// [
/// r#"INSERT INTO "glyph" ("aspect", "image")"#,
/// r#"VALUES ('abcd', 3.1415)"#,
/// r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
/// ]
/// .join(" ")
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// [
/// r#"INSERT INTO "glyph" ("aspect", "image")"#,
/// r#"VALUES ('abcd', 3.1415)"#,
/// r#"ON CONFLICT ("id", "aspect") DO NOTHING"#,
/// ]
/// .join(" ")
/// );
/// ```
pub fn do_nothing(&mut self) -> &mut Self {
self.action = Some(OnConflictAction::DoNothing);
self
Expand Down
21 changes: 21 additions & 0 deletions tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,27 @@ fn insert_from_select() {
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_do_nothing() {
assert_eq!(
Query::insert()
.into_table(Glyph::Table)
.columns([Glyph::Aspect, Glyph::Image])
.values_panic([
"04108048005887010020060000204E0180400400".into(),
3.1415.into(),
])
.on_conflict(OnConflict::new().do_nothing().to_owned())
.to_string(MysqlQueryBuilder),
[
r#"INSERT IGNORE INTO `glyph` (`aspect`, `image`)"#,
r#"VALUES ('04108048005887010020060000204E0180400400', 3.1415)"#,
]
.join(" ")
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_0() {
Expand Down
22 changes: 22 additions & 0 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,28 @@ fn insert_10() {
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_do_nothing() {
assert_eq!(
Query::insert()
.into_table(Glyph::Table)
.columns([Glyph::Aspect, Glyph::Image])
.values_panic([
"04108048005887010020060000204E0180400400".into(),
3.1415.into(),
])
.on_conflict(OnConflict::column(Glyph::Id).do_nothing().to_owned())
.to_string(PostgresQueryBuilder),
[
r#"INSERT INTO "glyph" ("aspect", "image")"#,
r#"VALUES ('04108048005887010020060000204E0180400400', 3.1415)"#,
r#"ON CONFLICT ("id") DO NOTHING"#,
]
.join(" ")
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_1() {
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,28 @@ fn insert_7() {
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_do_nothing() {
assert_eq!(
Query::insert()
.into_table(Glyph::Table)
.columns([Glyph::Aspect, Glyph::Image])
.values_panic([
"04108048005887010020060000204E0180400400".into(),
3.1415.into(),
])
.on_conflict(OnConflict::column(Glyph::Id).do_nothing().to_owned())
.to_string(SqliteQueryBuilder),
[
r#"INSERT INTO "glyph" ("aspect", "image")"#,
r#"VALUES ('04108048005887010020060000204E0180400400', 3.1415)"#,
r#"ON CONFLICT ("id") DO NOTHING"#,
]
.join(" ")
);
}

#[test]
#[allow(clippy::approx_constant)]
fn insert_on_conflict_1() {
Expand Down

0 comments on commit a58d39a

Please sign in to comment.