fix: simple ORM for one struct and foreign references work

Currently, all methods declared in the Georm trait are available.

If a struct has an ID pointing towards another entity, the user can
create a get method to get the entity pointed at from the database
too (local one-to-one relationship).

I still need to implement remote one-to-one relationships (one-to-one
relationships when the ID of the remote object is not available
locally).

I still need to also test and debug one-to-many relationships (ID of
the remote entiies not available locally) and many-to-many
relationships (declared in a dedicated table).

For now, IDs in all cases are simple types recognized by SQLx that are
not arrays. Options are only supported when explicitely specified for
one-to-one relationships.
This commit is contained in:
2025-01-31 21:58:36 +01:00
parent 96ac2aa979
commit bca0619f30
19 changed files with 511 additions and 107 deletions

10
tests/fixtures/o2o.sql vendored Normal file
View File

@@ -0,0 +1,10 @@
INSERT INTO books (title, author_id)
VALUES ('The Lord of the Rings: The Fellowship of the Ring', 1),
('The Lord of the Rings: The Two Towers', 1),
('The Lord of the Rings: The Return of the King', 1),
('To Build a Fire', 3);
INSERT INTO reviews (book_id, review)
VALUES (1, 'Great book'),
(3, 'Awesome book'),
(2, 'Greatest book');

8
tests/fixtures/simple_struct.sql vendored Normal file
View File

@@ -0,0 +1,8 @@
INSERT INTO biographies (content)
VALUES ('Some text'),
('Some other text');
INSERT INTO authors (name, biography_id)
VALUES ('J.R.R. Tolkien', 2),
('George Orwell', NULL),
('Jack London', 1);

63
tests/models.rs Normal file
View File

@@ -0,0 +1,63 @@
use georm::Georm;
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
#[georm(table = "biographies")]
pub struct Biography {
#[georm(id)]
pub id: i32,
pub content: String,
}
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
#[georm(table = "authors")]
pub struct Author {
#[georm(id)]
pub id: i32,
pub name: String,
#[georm(relation = {entity = Biography, table = "biographies", name = "biography", nullable = true})]
pub biography_id: Option<i32>,
}
impl PartialOrd for Author {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.id.cmp(&other.id))
}
}
impl Ord for Author {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq, Default)]
#[georm(table = "books")]
pub struct Book {
#[georm(id)]
ident: i32,
title: String,
#[georm(relation = {entity = Author, table = "authors", name = "author"})]
author_id: i32,
}
impl PartialOrd for Book {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.ident.cmp(&other.ident))
}
}
impl Ord for Book {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.ident.cmp(&other.ident)
}
}
#[derive(Debug, sqlx::FromRow, Georm, PartialEq, Eq)]
#[georm(table = "reviews")]
pub struct Review {
#[georm(id)]
pub id: i32,
#[georm(relation = {entity = Book, table = "books", remote_id = "ident", name = "book"})]
pub book_id: i32,
pub review: String
}

55
tests/o2o_relationship.rs Normal file
View File

@@ -0,0 +1,55 @@
use georm::Georm;
mod models;
use models::*;
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn book_should_have_working_get_author_method(pool: sqlx::PgPool) -> sqlx::Result<()> {
let book = Book::find(&pool, &1).await?;
assert!(book.is_some());
let book = book.unwrap();
let author = book.get_author(&pool).await?;
let expected_author = Author {
id: 1,
name: "J.R.R. Tolkien".into(),
biography_id: Some(2),
};
assert_eq!(expected_author, author);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn author_should_have_working_get_biography_method(pool: sqlx::PgPool) -> sqlx::Result<()> {
let author = Author::find(&pool, &1).await?;
assert!(author.is_some());
let author = author.unwrap();
let biography = author.get_biography(&pool).await?;
assert!(biography.is_some());
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn author_should_have_optional_biographies(pool: sqlx::PgPool) -> sqlx::Result<()> {
let tolkien = Author::find(&pool, &1).await?;
assert!(tolkien.is_some());
let tolkien_biography = tolkien.unwrap().get_biography(&pool).await?;
assert!(tolkien_biography.is_some());
let biography = Biography {
id: 2,
content: "Some other text".into(),
};
assert_eq!(biography, tolkien_biography.unwrap());
let orwell = Author::find(&pool, &2).await?;
assert!(orwell.is_some());
assert!(orwell.unwrap().get_biography(&pool).await?.is_none());
Ok(())
}
#[sqlx::test(fixtures("simple_struct", "o2o"))]
async fn books_are_found_despite_nonstandard_id_name(pool: sqlx::PgPool) -> sqlx::Result<()> {
let review = Review::find(&pool, &1).await?.unwrap();
let book = review.get_book(&pool).await?;
let tolkien = Author::find(&pool, &1).await?.unwrap();
assert_eq!(tolkien, book.get_author(&pool).await?);
Ok(())
}

164
tests/simple_struct.rs Normal file
View File

@@ -0,0 +1,164 @@
use georm::Georm;
use rand::seq::SliceRandom;
use models::Author;
mod models;
#[sqlx::test(fixtures("simple_struct"))]
async fn find_all_query_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
let result = Author::find_all(&pool).await?;
assert_eq!(3, result.len());
Ok(())
}
#[sqlx::test]
async fn find_all_returns_empty_vec_on_empty_table(pool: sqlx::PgPool) -> sqlx::Result<()> {
let result = Author::find_all(&pool).await?;
assert_eq!(0, result.len());
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn find_query_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
let id = 1;
let res = Author::find(&pool, &id).await?;
assert!(res.is_some());
let res = res.unwrap();
assert_eq!(String::from("J.R.R. Tolkien"), res.name);
assert_eq!(1, res.id);
Ok(())
}
#[sqlx::test]
async fn find_returns_none_if_not_found(pool: sqlx::PgPool) -> sqlx::Result<()> {
let res = Author::find(&pool, &420).await?;
assert!(res.is_none());
Ok(())
}
#[sqlx::test]
async fn create_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
let author = Author {
id: 1,
name: "J.R.R. Tolkien".into(),
..Default::default()
};
author.create(&pool).await?;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(1, all_authors.len());
assert_eq!(vec![author], all_authors);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn create_fails_if_already_exists(pool: sqlx::PgPool) -> sqlx::Result<()> {
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
let result = author.create(&pool).await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!("error returned from database: duplicate key value violates unique constraint \"authors_pkey\"", error.to_string());
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn update_works(pool: sqlx::PgPool) -> sqlx::Result<()> {
let expected_initial = Author {
name: "J.R.R. Tolkien".into(),
id: 1,
biography_id: Some(2),
};
let expected_final = Author {
name: "Jolkien Rolkien Rolkien Tolkien".into(),
id: 1,
biography_id: Some(2),
};
let tolkien = Author::find(&pool, &1).await?;
assert!(tolkien.is_some());
let mut tolkien = tolkien.unwrap();
assert_eq!(expected_initial, tolkien);
tolkien.name = expected_final.name.clone();
let updated = tolkien.update(&pool).await?;
assert_eq!(expected_final, updated);
Ok(())
}
#[sqlx::test]
async fn update_fails_if_not_already_exists(pool: sqlx::PgPool) -> sqlx::Result<()> {
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
let result = author.update(&pool).await;
assert!(result.is_err());
let error = result.err().unwrap();
assert_eq!(
"no rows returned by a query that expected to return at least one row",
error.to_string()
);
Ok(())
}
#[sqlx::test]
async fn should_create_if_does_not_exist(pool: sqlx::PgPool) -> sqlx::Result<()> {
let all_authors = Author::find_all(&pool).await?;
assert_eq!(0, all_authors.len());
let author = Author {
id: 4,
name: "Miura Kentaro".into(),
..Default::default()
};
author.create_or_update(&pool).await?;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(1, all_authors.len());
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn should_update_if_exist(pool: sqlx::PgPool) -> sqlx::Result<()> {
let all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
let author = Author {
id: 2,
name: "Miura Kentaro".into(),
..Default::default()
};
author.create_or_update(&pool).await?;
let mut all_authors = Author::find_all(&pool).await?;
all_authors.sort();
assert_eq!(3, all_authors.len());
assert_eq!(author, all_authors[1]);
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn delete_by_id_should_delete_only_one_entry(pool: sqlx::PgPool) -> sqlx::Result<()> {
let id = 2;
let all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
assert!(all_authors.iter().any(|author| author.get_id() == &id));
let result = Author::delete_by_id(&pool, &id).await?;
assert_eq!(1, result);
let all_authors = Author::find_all(&pool).await?;
assert_eq!(2, all_authors.len());
assert!(all_authors.iter().all(|author| author.get_id() != &id));
Ok(())
}
#[sqlx::test(fixtures("simple_struct"))]
async fn delete_should_delete_current_entity_from_db(pool: sqlx::PgPool) -> sqlx::Result<()> {
let mut all_authors = Author::find_all(&pool).await?;
assert_eq!(3, all_authors.len());
all_authors.shuffle(&mut rand::rng());
let author = all_authors.first().unwrap();
let result = author.delete(&pool).await?;
assert_eq!(1, result);
let all_authors = Author::find_all(&pool).await?;
assert_eq!(2, all_authors.len());
assert!(all_authors.iter().all(|a| a.get_id() != author.get_id()));
Ok(())
}