Merge pull request 'feat&chore: impl cqrs::Querey for category and cleanup tests' (#35) from chore-category into master
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Reviewed-on: #35
This commit is contained in:
commit
0bf0238aa5
4 changed files with 43 additions and 34 deletions
|
@ -32,6 +32,8 @@ impl CategoryIDExistsDBPort for InventoryDBPostgresAdapter {
|
||||||
mod tests {
|
mod tests {
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::inventory::adapters::output::db::postgres::category_name_exists_for_store::tests::create_dummy_category_record;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
@ -57,19 +59,7 @@ mod tests {
|
||||||
// state doesn't exist
|
// state doesn't exist
|
||||||
assert!(!db.category_id_exists(&category).await.unwrap());
|
assert!(!db.category_id_exists(&category).await.unwrap());
|
||||||
|
|
||||||
sqlx::query!(
|
create_dummy_category_record(&category, &db).await;
|
||||||
"INSERT INTO cqrs_inventory_category_query
|
|
||||||
(version, name, description, category_id, store_id)
|
|
||||||
VALUES ($1, $2, $3, $4, $5);",
|
|
||||||
1,
|
|
||||||
category.name(),
|
|
||||||
category.description().as_ref().unwrap(),
|
|
||||||
category.category_id(),
|
|
||||||
category.store_id(),
|
|
||||||
)
|
|
||||||
.execute(&db.pool)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// state exists
|
// state exists
|
||||||
assert!(db.category_id_exists(&category).await.unwrap());
|
assert!(db.category_id_exists(&category).await.unwrap());
|
||||||
|
|
|
@ -34,11 +34,28 @@ impl CategoryNameExistsForStoreDBPort for InventoryDBPostgresAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
pub mod tests {
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
pub async fn create_dummy_category_record(c: &Category, db: &InventoryDBPostgresAdapter) {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_inventory_category_query
|
||||||
|
(version, name, description, category_id, store_id)
|
||||||
|
VALUES ($1, $2, $3, $4, $5);",
|
||||||
|
1,
|
||||||
|
c.name(),
|
||||||
|
c.description().as_ref().unwrap(),
|
||||||
|
c.category_id(),
|
||||||
|
c.store_id(),
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_postgres_category_exists() {
|
async fn test_postgres_category_exists() {
|
||||||
let category_id = Uuid::new_v4();
|
let category_id = Uuid::new_v4();
|
||||||
|
@ -62,19 +79,7 @@ mod tests {
|
||||||
// state doesn't exist
|
// state doesn't exist
|
||||||
assert!(!db.category_name_exists_for_store(&category).await.unwrap());
|
assert!(!db.category_name_exists_for_store(&category).await.unwrap());
|
||||||
|
|
||||||
sqlx::query!(
|
create_dummy_category_record(&category, &db).await;
|
||||||
"INSERT INTO cqrs_inventory_category_query
|
|
||||||
(version, name, description, category_id, store_id)
|
|
||||||
VALUES ($1, $2, $3, $4, $5);",
|
|
||||||
1,
|
|
||||||
category.name(),
|
|
||||||
category.description().as_ref().unwrap(),
|
|
||||||
category.category_id(),
|
|
||||||
category.store_id(),
|
|
||||||
)
|
|
||||||
.execute(&db.pool)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// state exists
|
// state exists
|
||||||
assert!(db.category_name_exists_for_store(&category).await.unwrap());
|
assert!(db.category_name_exists_for_store(&category).await.unwrap());
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use cqrs_es::persist::GenericQuery;
|
|
||||||
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
||||||
use cqrs_es::{EventEnvelope, Query, View};
|
use cqrs_es::{EventEnvelope, Query, View};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -179,7 +178,22 @@ impl Query<Category> for SimpleLoggingQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our second query, this one will be handled with Postgres `GenericQuery`
|
#[async_trait]
|
||||||
// which will serialize and persist our view after it is updated. It also
|
impl Query<Category> for InventoryDBPostgresAdapter {
|
||||||
// provides a `load` method to deserialize the view on request.
|
async fn dispatch(&self, category_id: &str, events: &[EventEnvelope<Category>]) {
|
||||||
pub type CategoryQuery = GenericQuery<InventoryDBPostgresAdapter, CategoryView, Category>;
|
let res = self
|
||||||
|
.load_with_context(&category_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
Some((
|
||||||
|
CategoryView::default(),
|
||||||
|
ViewContext::new(category_id.into(), 0),
|
||||||
|
))
|
||||||
|
});
|
||||||
|
let (mut view, view_context): (CategoryView, ViewContext) = res.unwrap();
|
||||||
|
for event in events {
|
||||||
|
view.update(event);
|
||||||
|
}
|
||||||
|
self.update_view(view, view_context).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ mod aggregate_tests {
|
||||||
type CategoryTestFramework = TestFramework<Category>;
|
type CategoryTestFramework = TestFramework<Category>;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_store() {
|
fn test_create_category() {
|
||||||
let name = "category_name";
|
let name = "category_name";
|
||||||
let description = Some("category_description".to_string());
|
let description = Some("category_description".to_string());
|
||||||
let adding_by = UUID;
|
let adding_by = UUID;
|
||||||
|
|
Loading…
Reference in a new issue