feat: impl customization name violation for db
Some checks failed
ci/woodpecker/pr/woodpecker Pipeline failed
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-07-16 11:37:25 +05:30
parent 4fc4b612bf
commit fcb99cc9ec
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,23 @@
{
"db_name": "PostgreSQL",
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_customizations_query\n WHERE\n name = $1\n AND\n product_id = $2\n AND\n deleted = false\n );",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "exists",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Uuid"
]
},
"nullable": [
null
]
},
"hash": "78c1fa1b50afb781ed7c28c5454f43c61894b22bcde01097ca41ab691d985aea"
}

View file

@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use uuid::Uuid;
use super::InventoryDBPostgresAdapter;
use crate::inventory::application::port::output::db::{
customization_name_exists_for_product::*, errors::*,
};
use crate::inventory::domain::product_aggregate::*;
#[async_trait::async_trait]
impl CustomizationNameExistsForProductDBPort for InventoryDBPostgresAdapter {
async fn customization_name_exists_for_product(
&self,
c: &Customization,
product_id: &Uuid,
) -> InventoryDBResult<bool> {
let res = sqlx::query!(
"SELECT EXISTS (
SELECT 1
FROM cqrs_inventory_product_customizations_query
WHERE
name = $1
AND
product_id = $2
AND
deleted = false
);",
c.name(),
product_id,
)
.fetch_one(&self.pool)
.await?;
if let Some(x) = res.exists {
Ok(x)
} else {
Ok(false)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::inventory::adapters::output::db::postgres::customization_id_exists::tests::create_dummy_customization_record;
use crate::utils::uuid::tests::UUID;
#[actix_rt::test]
async fn test_postgres_customization_exists() {
let customization_name = "foo_customization";
let product_id = UUID;
let settings = crate::settings::tests::get_settings().await;
settings.create_db().await;
let db = super::InventoryDBPostgresAdapter::new(
sqlx::postgres::PgPool::connect(&settings.database.url)
.await
.unwrap(),
);
let customization = {
CustomizationBuilder::default()
.name(customization_name.into())
.customization_id(UUID)
.deleted(false)
.build()
.unwrap()
};
// state doesn't exist
assert!(!db
.customization_name_exists_for_product(&customization, &product_id)
.await
.unwrap());
create_dummy_customization_record(&customization, &db).await;
// state exists
assert!(db
.customization_name_exists_for_product(&customization, &product_id)
.await
.unwrap());
// Set customization.deleted = true; now db.customization_name_exists_for_product must return false
sqlx::query!(
"UPDATE
cqrs_inventory_product_customizations_query
SET
deleted = true
WHERE
customization_id = $1
AND
product_id = $2
AND
name = $3;",
customization.customization_id(),
&product_id,
customization.name()
)
.execute(&db.pool)
.await
.unwrap();
assert!(!db
.customization_name_exists_for_product(&customization, &product_id)
.await
.unwrap());
settings.drop_db().await;
}
}

View file

@ -11,6 +11,8 @@ use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres};
mod category_id_exists;
mod category_name_exists_for_store;
mod category_view;
mod customization_id_exists;
mod customization_name_exists_for_product;
mod errors;
mod product_id_exists;
mod product_name_exists_for_category;