Skip to content

Commit

Permalink
renamed Error and Result
Browse files Browse the repository at this point in the history
  • Loading branch information
eboody committed Dec 15, 2024
1 parent 16b5db8 commit b7b83c7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ pub struct Person {
age: u8,
}


pub static CREATE_TABLE_SQL: &str =
"CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";
pub static CREATE_TABLE_SQL: &str = "CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ pub struct Person {
age: u8,
}


pub static CREATE_TABLE_SQL: &str =
"CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";
pub static CREATE_TABLE_SQL: &str = "CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
14 changes: 5 additions & 9 deletions ormlite/tests/incomplete_tests/multiple_databases/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,25 @@ pub struct Person {
age: u8,
}


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let mut db = ormlite::sqlite::SqliteConnection::connect(":memory:").await.unwrap();
let migration = crate::setup::migrate_self(&[file!()]);
for s in migration.statements {
let sql = s.to_sql(sqlmo::Dialect::Sqlite);
ormlite::query(&sql)
.execute(&mut db)
.await?;
ormlite::query(&sql).execute(&mut db).await?;
}

let p = Person {
id: Uuid::new_v4(),
name: "John".to_string(),
age: 99,
}.insert(&mut db).await?;
}
.insert(&mut db)
.await?;

let p = p.update_partial()
.age(100)
.update(&mut db)
.await?;
let p = p.update_partial().age(100).update(&mut db).await?;

assert_eq!(p.age, 100);
Ok(())
Expand Down
16 changes: 6 additions & 10 deletions ormlite/tests/multifile/main.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
#[path = "../setup.rs"]
mod setup;

mod user;
mod organization;
mod user;

pub use user::User;
pub use organization::Organization;
use uuid::Uuid;
use ormlite::model::*;
use ormlite::sqlite::SqliteConnection;
use ormlite::Connection;
use sqlmo::ToSql;
pub use user::User;
use uuid::Uuid;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut conn = SqliteConnection::connect(":memory:").await?;
let migration = setup::migrate_self(&[
&std::path::Path::new(file!()).parent().unwrap().display().to_string(),
]);
let migration = setup::migrate_self(&[&std::path::Path::new(file!()).parent().unwrap().display().to_string()]);
for s in migration.statements {
let sql = s.to_sql(sqlmo::Dialect::Sqlite);
ormlite::query(&sql)
.execute(&mut conn)
.await?;
ormlite::query(&sql).execute(&mut conn).await?;
}

let org_id = Uuid::new_v4();
Expand All @@ -40,4 +36,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let user = user.insert(&mut conn).await?;
assert_eq!(user.organization.id, org_id);
Ok(())
}
}
55 changes: 31 additions & 24 deletions ormlite/tests/sqlite/03-many-to-one-join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@ pub struct Organization {
name: String,
}

pub static CREATE_PERSON_SQL: &str =
"CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER, org_id text)";
pub static CREATE_PERSON_SQL: &str = "CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER, org_id text)";

pub static CREATE_ORG_SQL: &str =
"CREATE TABLE orgs (id text PRIMARY KEY, name TEXT)";
pub static CREATE_ORG_SQL: &str = "CREATE TABLE orgs (id text PRIMARY KEY, name TEXT)";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let mut db = ormlite::sqlite::SqliteConnection::connect(":memory:").await.unwrap();
ormlite::query(CREATE_PERSON_SQL)
.execute(&mut db)
.await?;
ormlite::query(CREATE_ORG_SQL)
.execute(&mut db)
.await?;
ormlite::query(CREATE_PERSON_SQL).execute(&mut db).await?;
ormlite::query(CREATE_ORG_SQL).execute(&mut db).await?;

let org = Organization {
id: Uuid::new_v4(),
Expand All @@ -44,41 +38,54 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
name: "John".to_string(),
age: 102,
organization: Join::new(org.clone()),
}.insert(&mut db).await.unwrap();
assert_eq!(p1.organization.id, org.id, "setting the org object should overwrite the org_id field on insert.");
}
.insert(&mut db)
.await
.unwrap();
assert_eq!(
p1.organization.id, org.id,
"setting the org object should overwrite the org_id field on insert."
);
assert_eq!(p1.organization._id(), org.id);

let org = Organization::select()
.where_bind("id = ?", &org.id)
.fetch_one(&mut db)
.await.unwrap();
assert_eq!(org.name, "my org", "org gets inserted even though we didn't manually insert it.");
.await
.unwrap();
assert_eq!(
org.name, "my org",
"org gets inserted even though we didn't manually insert it."
);

let p2 = Person {
id: Uuid::new_v4(),
name: "p2".to_string(),
age: 98,
organization: Join::new(org.clone()),
}.insert(&mut db).await.unwrap();
assert_eq!(p2.organization.id, org.id, "we can do insertion with an existing join obj, and it will pass the error.");
}
.insert(&mut db)
.await
.unwrap();
assert_eq!(
p2.organization.id, org.id,
"we can do insertion with an existing join obj, and it will pass the error."
);

let orgs = Organization::select()
.fetch_all(&mut db)
.await.unwrap();
let orgs = Organization::select().fetch_all(&mut db).await.unwrap();
assert_eq!(orgs.len(), 1, "exactly 1 orgs");

let people = Person::select()
.fetch_all(&mut db)
.await.unwrap();
let people = Person::select().fetch_all(&mut db).await.unwrap();
assert_eq!(people.len(), 2, "exactly 2 people");

let people = Person::select()
.join(Person::organization())
.fetch_all(&mut db)
.await.unwrap();
.await
.unwrap();
assert_eq!(people.len(), 2, "exactly 2 people");
for person in &people {
assert_eq!(person.organization.name, "my org", "we can join on the org");
}
Ok(())
}
}
17 changes: 6 additions & 11 deletions ormlite/tests/sqlite/04-allow-clone-primary-key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,23 @@ pub struct Person {
age: u8,
}


pub static CREATE_TABLE_SQL: &str =
"CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";
pub static CREATE_TABLE_SQL: &str = "CREATE TABLE person (id text PRIMARY KEY, name TEXT, age INTEGER)";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let mut db = ormlite::sqlite::SqliteConnection::connect(":memory:").await.unwrap();
ormlite::query(CREATE_TABLE_SQL)
.execute(&mut db)
.await?;
ormlite::query(CREATE_TABLE_SQL).execute(&mut db).await?;

let p = Person {
id: Uuid::new_v4(),
name: "John".to_string(),
age: 99,
}.insert(&mut db).await?;
}
.insert(&mut db)
.await?;

let p = p.update_partial()
.age(100)
.update(&mut db)
.await?;
let p = p.update_partial().age(100).update(&mut db).await?;

assert_eq!(p.age, 100);

Expand Down

0 comments on commit b7b83c7

Please sign in to comment.