Add #244 as test cases

This commit is contained in:
Billy Chan 2021-10-15 12:38:17 +08:00
parent 80d3ddc030
commit b48f1251b0
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
5 changed files with 131 additions and 0 deletions

View File

@ -7,6 +7,7 @@ pub mod customer;
pub mod lineitem;
pub mod metadata;
pub mod order;
pub mod repository;
pub use super::applog::Entity as Applog;
pub use super::baker::Entity as Baker;
@ -17,3 +18,4 @@ pub use super::customer::Entity as Customer;
pub use super::lineitem::Entity as Lineitem;
pub use super::metadata::Entity as Metadata;
pub use super::order::Entity as Order;
pub use super::repository::Entity as Repository;

View File

@ -0,0 +1,16 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "repository")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: String,
pub owner: String,
pub name: String,
pub description: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -54,6 +54,7 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
schema::create_lineitem_table(&db).await.unwrap();
schema::create_metadata_table(&db).await.unwrap();
schema::create_log_table(&db).await.unwrap();
schema::create_repository_table(&db).await.unwrap();
db
}

View File

@ -317,3 +317,24 @@ pub async fn create_log_table(db: &DbConn) -> Result<ExecResult, DbErr> {
create_table(db, &stmt, Applog).await
}
pub async fn create_repository_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(repository::Entity)
.col(
ColumnDef::new(repository::Column::Id)
.string()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(repository::Column::Owner)
.string()
.not_null(),
)
.col(ColumnDef::new(repository::Column::Name).string().not_null())
.col(ColumnDef::new(repository::Column::Description).string())
.to_owned();
create_table(db, &stmt, Repository).await
}

View File

@ -0,0 +1,91 @@
pub mod common;
pub use common::{bakery_chain::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("bakery_chain_schema_string_primary_key_tests").await;
create_and_update_repository(&ctx.db).await?;
insert_repository(&ctx.db).await?;
ctx.delete().await;
Ok(())
}
pub async fn insert_repository(db: &DatabaseConnection) -> Result<(), DbErr> {
let repository = repository::Model {
id: "unique-id-001".to_owned(),
owner: "GC".to_owned(),
name: "G.C.".to_owned(),
description: None,
}
.into_active_model();
let result = repository.clone().insert(db).await?;
assert_eq!(repository, result);
Ok(())
}
pub async fn create_and_update_repository(db: &DatabaseConnection) -> Result<(), DbErr> {
let repository = repository::Model {
id: "unique-id-002".to_owned(),
owner: "GC".to_owned(),
name: "G.C.".to_owned(),
description: None,
};
let res = Repository::insert(repository.clone().into_active_model())
.exec(db)
.await?;
assert_eq!(Repository::find().one(db).await?, Some(repository.clone()));
assert_eq!(res.last_insert_id, repository.id);
let updated_active_model = repository::ActiveModel {
description: Set(Some("description...".to_owned())),
..repository.clone().into_active_model()
};
let update_res = Repository::update(updated_active_model.clone())
.filter(repository::Column::Id.eq("not-exists-id".to_owned()))
.exec(db)
.await;
assert_eq!(
update_res,
Err(DbErr::RecordNotFound(
"None of the database rows are affected".to_owned()
))
);
let update_res = Repository::update(updated_active_model.clone())
.filter(repository::Column::Id.eq("unique-id-002".to_owned()))
.exec(db)
.await?;
assert_eq!(update_res, updated_active_model);
let updated_active_model = repository::ActiveModel {
description: Set(None),
..repository.clone().into_active_model()
};
let update_res = Repository::update(updated_active_model.clone())
.filter(repository::Column::Id.eq("unique-id-002".to_owned()))
.exec(db)
.await?;
assert_eq!(update_res, updated_active_model);
Ok(())
}