diff --git a/charybdis-macros/src/lib.rs b/charybdis-macros/src/lib.rs index ef50ff7..00cc520 100644 --- a/charybdis-macros/src/lib.rs +++ b/charybdis-macros/src/lib.rs @@ -187,6 +187,7 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea // Current model rules let find_model_query_rule = find_model_query_rule(struct_name, &args, fields); + let find_model_rule = find_model_rule(struct_name, &args, fields); // Associated functions let find_by_primary_keys_functions = find_by_primary_keys_functions(struct_name, &args, fields); @@ -195,8 +196,6 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea CharybdisFields::strip_charybdis_attributes(&mut input); let expanded = quote! { - use charybdis::scylla::serialize::SerializationError; - #[derive(charybdis::macros::scylla::SerializeRow)] #[derive(charybdis::macros::scylla::DeserializeRow)] #input @@ -225,6 +224,7 @@ pub fn charybdis_view_model(args: TokenStream, input: TokenStream) -> TokenStrea impl charybdis::model::MaterializedView for #struct_name {} #find_model_query_rule + #find_model_rule }; TokenStream::from(expanded) diff --git a/charybdis/tests/integrations/model.rs b/charybdis/tests/integrations/model.rs index e2ff257..a56b68c 100644 --- a/charybdis/tests/integrations/model.rs +++ b/charybdis/tests/integrations/model.rs @@ -8,6 +8,8 @@ use charybdis::stream::CharybdisModelStream; use charybdis::types::{Boolean, Int, Text, Uuid}; use charybdis_macros::{charybdis_model, charybdis_udt_model, charybdis_view_model}; +pub const SAMPLE_MODEL_COUNT: usize = 32; + #[derive(Debug, Default, Clone, PartialEq)] #[charybdis_udt_model(type_name = address)] pub struct Address { @@ -46,7 +48,7 @@ partial_user!(UpdateUsernameUser, id, username); impl User { pub async fn populate_sample_users() { let db_session = db_session().await; - let users = (0..32) + let users = (0..SAMPLE_MODEL_COUNT) .map(|i| { let id = Uuid::new_v4(); let mut new_user = User::homer(id); @@ -157,15 +159,15 @@ pub struct Post { } impl Post { - pub async fn populate_sample_posts_per_partition(category_id: Uuid) { + pub async fn populate_sample_posts_per_partition(category_id: Uuid, author_id: Option) { let db_session = db_session().await; - let posts = (0..32) + let posts = (0..SAMPLE_MODEL_COUNT) .map(|i| Post { category_id, - order_idx: i, + order_idx: i as Int, title: format!("Post {}", i), content: "Lorem ipsum dolor sit amet".to_string(), - author_id: Uuid::new_v4(), + author_id: author_id.unwrap_or_else(Uuid::new_v4), }) .collect::>(); @@ -227,10 +229,10 @@ async fn find_various() -> Result<(), CharybdisError> { let category_id = Uuid::new_v4(); let db_session = &db_session().await; - Post::populate_sample_posts_per_partition(category_id).await; + Post::populate_sample_posts_per_partition(category_id, None).await; let posts: CharybdisModelStream = Post::find_by_category_id(category_id).execute(db_session).await?; - assert_eq!(posts.try_collect().await?.len(), 32); + assert_eq!(posts.try_collect().await?.len(), SAMPLE_MODEL_COUNT); let posts: CharybdisModelStream = Post::find_by_category_id_and_order_idx(category_id, 1) .execute(db_session) @@ -299,3 +301,45 @@ async fn find_various() -> Result<(), CharybdisError> { Ok(()) } + +#[charybdis_view_model( + table_name=posts_by_author, + base_table = posts, + partition_keys = [author_id], + clustering_keys = [category_id, order_idx, title], +)] +#[derive(Debug, Default, Clone, PartialEq)] +pub struct PostByAuthor { + pub category_id: Uuid, + pub order_idx: Int, + pub title: Text, + pub content: Text, + pub author_id: Uuid, +} + +#[tokio::test] +async fn post_by_author_find() -> Result<(), CharybdisError> { + let category_id = Uuid::new_v4(); + let author_id = Uuid::new_v4(); + let db_session = &db_session().await; + + Post::populate_sample_posts_per_partition(category_id, Some(author_id)).await; + + let posts_by_author = PostByAuthor::find_by_author_id(author_id) + .execute(db_session) + .await? + .try_collect() + .await?; + + assert_eq!(posts_by_author.len(), SAMPLE_MODEL_COUNT); + + let posts_by_author_with_limit = find_post_by_author!("author_id = ? LIMIT 10", (author_id,)) + .execute(db_session) + .await? + .try_collect() + .await?; + + assert_eq!(posts_by_author_with_limit.len(), 10); + + Ok(()) +} diff --git a/charybdis/tests/integrations/query.rs b/charybdis/tests/integrations/query.rs index 3966727..6178089 100644 --- a/charybdis/tests/integrations/query.rs +++ b/charybdis/tests/integrations/query.rs @@ -1,6 +1,6 @@ use crate::common::db_session; use crate::custom_fields::AddressTypeCustomField; -use crate::model::{Post, User}; +use crate::model::{Post, User, SAMPLE_MODEL_COUNT}; use charybdis::batch::ModelBatch; use charybdis::errors::CharybdisError; use charybdis::operations::{Delete, Find, Insert, Update}; @@ -97,7 +97,7 @@ async fn model_stream() { .expect("Failed to find users"); let users_vec = users.try_collect().await.expect("Failed to collect users"); - assert_eq!(users_vec.len(), 32); + assert_eq!(users_vec.len(), SAMPLE_MODEL_COUNT); User::delete_batch() .chunked_delete(&db_session, &users_vec, 100) @@ -110,7 +110,7 @@ async fn model_paged() { let db_session = db_session().await; let category_id = uuid::Uuid::new_v4(); - Post::populate_sample_posts_per_partition(category_id).await; + Post::populate_sample_posts_per_partition(category_id, None).await; let (posts, paging_state_response) = Post::find_by_partition_key_value_paged((category_id,)) .page_size(3)