Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iganev committed Feb 24, 2024
1 parent c491fc6 commit 36f8033
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
68 changes: 68 additions & 0 deletions tests/datatypes_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,98 @@ fn test_datatypes() {
SqlDatatype::try_from("tinyint(1) unsigned"),
Ok(SqlDatatype::Tinyint(1, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("tinyint(1) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("tinyint(1) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(1)
);
assert_eq!(
SqlDatatype::try_from("int(10) unsigned"),
Ok(SqlDatatype::Int(10, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("int(10) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("int(10) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(10)
);
assert_eq!(
SqlDatatype::try_from("smallint(5) unsigned"),
Ok(SqlDatatype::Smallint(5, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("smallint(5) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("smallint(5) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(5)
);
assert_eq!(
SqlDatatype::try_from("mediumint(15) unsigned"),
Ok(SqlDatatype::Mediumint(15, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("mediumint(15) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("mediumint(15) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(15)
);
assert_eq!(
SqlDatatype::try_from("bigint(32)"),
Ok(SqlDatatype::Bigint(32, SqlSigned::Signed))
);
assert_eq!(
SqlDatatype::try_from("bigint(32)").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Signed)
);
assert_eq!(
SqlDatatype::try_from("bigint(32)").ok().map(|t| t.len()).unwrap_or_default(),
Some(32)
);
assert_eq!(
SqlDatatype::try_from("float(4,2)"),
Ok(SqlDatatype::Float(4, 2, SqlSigned::Signed))
);
assert_eq!(
SqlDatatype::try_from("float(4,2)").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Signed)
);
assert_eq!(
SqlDatatype::try_from("float(4,2)").ok().map(|t| t.len()).unwrap_or_default(),
Some(4)
);
assert_eq!(
SqlDatatype::try_from("double(10,2) unsigned"),
Ok(SqlDatatype::Double(10, 2, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("double(10,2) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("double(10,2) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(10)
);
assert_eq!(
SqlDatatype::try_from("decimal(10,2) unsigned"),
Ok(SqlDatatype::Decimal(10, 2, SqlSigned::Unsigned))
);
assert_eq!(
SqlDatatype::try_from("decimal(10,2) unsigned").ok().map(|t| t.sign()).unwrap_or_default(),
Some(SqlSigned::Unsigned)
);
assert_eq!(
SqlDatatype::try_from("decimal(10,2) unsigned").ok().map(|t| t.len()).unwrap_or_default(),
Some(10)
);

assert_eq!(SqlDatatype::try_from("date"), Ok(SqlDatatype::Date));
assert_eq!(SqlDatatype::try_from("time"), Ok(SqlDatatype::Time));
Expand Down
49 changes: 46 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ fn get_mock_db() -> Database {

let products_table_name = "products";
let mut products_table = Table::new(products_table_name);

products_table
.set_meta(METADATA_CHARSET, "utf8mb4")
.set_meta(METADATA_COLLATION, "utf8mb4_unicode_ci");

products_table
.set_column(
Column::new(
Expand Down Expand Up @@ -369,9 +374,10 @@ fn get_mock_db() -> Database {
false,
));

client_products_table
.set_meta(METADATA_CHARSET, "utf8mb4")
.set_meta(METADATA_COLLATION, "utf8mb4_unicode_ci");
// we expect that the table will inherit the database charset and collation
// client_products_table
// .set_meta(METADATA_CHARSET, "utf8mb4")
// .set_meta(METADATA_COLLATION, "utf8mb4_unicode_ci");

db.set_table(client_products_table);

Expand Down Expand Up @@ -487,6 +493,19 @@ fn construction() {
"clients"
);

assert_eq!(
db.constraints_by_column_name(
String::from("client_id").into(),
Some(ConstraintSide::Local)
)
.first()
.unwrap()
.foreign()
.table()
.as_str(),
"clients"
);

assert_eq!(
db.constraints_by_column(
db.table("client_products")
Expand Down Expand Up @@ -566,6 +585,30 @@ fn construction() {

//

assert_eq!(
db.table("client_products").unwrap().meta(METADATA_CHARSET),
Some("utf8mb4".to_string())
);

assert_eq!(
db.table("client_products").unwrap().meta(METADATA_COLLATION),
Some("utf8mb4_unicode_ci".to_string())
);

//

assert_eq!(
db.table("products").unwrap().column("name").unwrap().meta(METADATA_CHARSET),
Some("utf8mb4".to_string())
);

assert_eq!(
db.table("products").unwrap().column("name").unwrap().meta(METADATA_COLLATION),
Some("utf8mb4_unicode_ci".to_string())
);

//

assert_eq!(db.table("client_products").unwrap().indexes().len(), 3);

let idx_list = vec![
Expand Down

0 comments on commit 36f8033

Please sign in to comment.