Skip to content

Commit

Permalink
Merge pull request #67 from ECDAR-AAU-SW-P5/delete-model-tests
Browse files Browse the repository at this point in the history
Add delete_model tests
  • Loading branch information
sabotack authored Nov 28, 2023
2 parents 09439e8 + eef0eab commit 9965a1d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ uuid = { version = "1.5.0", features = ["v4"] }
regex = "1.10.2"
mockall = "0.11.4"
bcrypt = "0.15.0"
serde_json = "1.0.108"

[build-dependencies]
tonic-build = "0.10.2"
Expand Down
4 changes: 4 additions & 0 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,7 @@ mod tests;
#[cfg(test)]
#[path = "../tests/api/query_logic.rs"]
mod query_logic;

#[cfg(test)]
#[path = "../tests/api/model_logic.rs"]
mod model_logic;
108 changes: 108 additions & 0 deletions src/tests/api/model_logic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::str::FromStr;

use mockall::predicate;
use tonic::{metadata, Code, Request};

use crate::{
api::server::server::{
ecdar_api_server::EcdarApi, Component, ComponentsInfo, DeleteModelRequest,
},
entities::model,
tests::api::helpers::{get_mock_concrete_ecdar_api, get_mock_services},
};

#[tokio::test]
async fn delete_not_owner_returns_err() {
let mut mock_services = get_mock_services();

mock_services
.model_context_mock
.expect_get_by_id()
.with(predicate::eq(1))
.returning(move |_| {
Ok(Some(model::Model {
id: 1,
name: Default::default(),
components_info: Default::default(),
owner_id: 2,
}))
});

let mut request = Request::new(DeleteModelRequest { id: 1 });

request
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());

let api = get_mock_concrete_ecdar_api(mock_services);

let res = api.delete_model(request).await.unwrap_err();

assert_eq!(res.code(), Code::PermissionDenied);
}

#[tokio::test]
async fn delete_invalid_model_returns_err() {
let mut mock_services = get_mock_services();

mock_services
.model_context_mock
.expect_get_by_id()
.with(predicate::eq(2))
.returning(move |_| Ok(None));

let mut request = Request::new(DeleteModelRequest { id: 2 });

request
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());

let api = get_mock_concrete_ecdar_api(mock_services);

let res = api.delete_model(request).await.unwrap_err();

assert_eq!(res.code(), Code::NotFound);
}

#[tokio::test]
async fn delete_model_returns_ok() {
let mut mock_services = get_mock_services();

mock_services
.model_context_mock
.expect_get_by_id()
.with(predicate::eq(1))
.returning(move |_| {
Ok(Some(model::Model {
id: 1,
name: Default::default(),
components_info: Default::default(),
owner_id: 1,
}))
});

mock_services
.model_context_mock
.expect_delete()
.with(predicate::eq(1))
.returning(move |_| {
Ok(model::Model {
id: 1,
name: Default::default(),
components_info: Default::default(),
owner_id: 1,
})
});

let mut request = Request::new(DeleteModelRequest { id: 1 });

request
.metadata_mut()
.insert("uid", metadata::MetadataValue::from_str("1").unwrap());

let api = get_mock_concrete_ecdar_api(mock_services);

let res = api.delete_model(request).await;

assert!(res.is_ok());
}

0 comments on commit 9965a1d

Please sign in to comment.