Skip to content

Commit c403674

Browse files
authored
Merge pull request #66 from ArunaStorage/test/object-group-tests
Add tests for ObjectGroupService
2 parents 3b40415 + 2ffc2b5 commit c403674

8 files changed

Lines changed: 1646 additions & 45 deletions

File tree

src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ impl ArunaServerConfig {
103103
/// If the parameter is mandatory the
104104
///
105105
fn load_config_env() -> Result<EnvConfig, VarError> {
106-
//ToDo: Implement loading of mandatory/optional environmental variables
107106
//let env_var = env::var("ENV_VAR")?; //Note: Mandatory
108107

109108
/* Note: Optional

src/database/crud/collection.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,11 @@ impl Database {
395395
.version
396396
.clone()
397397
.map(|v| from_grpc_version(v, uuid::Uuid::new_v4()))
398-
.ok_or(ArunaError::InvalidRequest(
399-
"Unable to create collection version".to_string(),
400-
))?;
398+
.ok_or_else(|| {
399+
ArunaError::InvalidRequest(
400+
"Unable to create collection version".to_string(),
401+
)
402+
})?;
401403

402404
// Create new Uuid for collection
403405
let new_coll_uuid = uuid::Uuid::new_v4();
@@ -1222,9 +1224,11 @@ fn pin_paths_to_version(
12221224
path: path_parts.join("/").to_string(),
12231225
shared_revision_id: *revision_id_mapping
12241226
.get(&old_path.shared_revision_id)
1225-
.ok_or(ArunaError::InvalidRequest(
1226-
"Could not map old object to newly created.".to_string(),
1227-
))?,
1227+
.ok_or_else(|| {
1228+
ArunaError::InvalidRequest(
1229+
"Could not map old object to newly created.".to_string(),
1230+
)
1231+
})?,
12281232
collection_id: pin_collection.coll.id,
12291233
created_at: Local::now().naive_local(),
12301234
active: true,

src/database/crud/object.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -971,14 +971,14 @@ impl Database {
971971
.get()?
972972
.transaction::<(Option<ObjectDto>, Vec<ProtoPath>), ArunaError, _>(|conn| {
973973
// Check if object exists in collection
974-
if !object_exists_in_collection(conn, &object_uuid, &collection_uuid, true)? {
974+
if !object_exists_in_collection(conn, object_uuid, collection_uuid, true)? {
975975
return Err(ArunaError::InvalidRequest(format!(
976976
"Object {object_uuid} does not exist in collection {collection_uuid}."
977977
)));
978978
}
979979

980980
// Try to get object without reference needed and its associated paths
981-
let object_dto_option = get_object_ignore_coll(&object_uuid, conn)?;
981+
let object_dto_option = get_object_ignore_coll(object_uuid, conn)?;
982982

983983
let proto_paths = if let Some(object_dto) = object_dto_option.clone() {
984984
get_paths_proto(&object_dto.object.shared_revision_id, conn)?
@@ -2172,7 +2172,7 @@ pub fn object_exists_in_collection(
21722172
}
21732173
};
21742174

2175-
Ok(references.len() > 0)
2175+
Ok(!references.is_empty())
21762176
}
21772177

21782178
/// This is a general helper function that can be use inside already open transactions
@@ -2523,14 +2523,12 @@ pub fn delete_staging_object(
25232523
.execute(conn)?;
25242524

25252525
// Get lowest object revision number
2526-
println!("{:#?}", staging_object);
25272526
let lowest_object_revision: Option<i64> = objects
25282527
.select(min(database::schema::objects::revision_number))
25292528
.filter(
25302529
database::schema::objects::shared_revision_id.eq(&staging_object.shared_revision_id),
25312530
)
25322531
.first::<Option<i64>>(conn)?;
2533-
println!("{:#?}", lowest_object_revision);
25342532

25352533
// Update object status to TRASH
25362534
match lowest_object_revision {
@@ -3190,12 +3188,14 @@ pub fn delete_multiple_objects(
31903188
for rel_col in relevant_collections {
31913189
for reference in references.clone() {
31923190
if reference.collection_id == rel_col {
3193-
let sh_rev_id = object_id_shared_rev_id.get(&reference.object_id).ok_or(
3194-
ArunaError::InvalidRequest(format!(
3195-
"Shared revision can not be found for object_id {} ",
3196-
reference.object_id
3197-
)),
3198-
)?; // Should never occur
3191+
let sh_rev_id = object_id_shared_rev_id
3192+
.get(&reference.object_id)
3193+
.ok_or_else(|| {
3194+
ArunaError::InvalidRequest(format!(
3195+
"Shared revision can not be found for object_id {} ",
3196+
reference.object_id
3197+
))
3198+
})?; // Should never occur
31993199
if let Some(sr_id) = shared_revisions_per_collection.get_mut(&rel_col) {
32003200
if let Some(sr_count) = sr_id.get_mut(sh_rev_id) {
32013201
*sr_count += 1;
@@ -3211,9 +3211,11 @@ pub fn delete_multiple_objects(
32113211

32123212
for reference in references_to_delete.clone() {
32133213
if reference.collection_id == rel_col {
3214-
let sh_rev_id = object_id_shared_rev_id.get(&reference.object_id).ok_or(
3215-
ArunaError::InvalidRequest("Shared revision does not exist".to_string()),
3216-
)?; // Should never occur
3214+
let sh_rev_id = object_id_shared_rev_id
3215+
.get(&reference.object_id)
3216+
.ok_or_else(|| {
3217+
ArunaError::InvalidRequest("Shared revision does not exist".to_string())
3218+
})?; // Should never occur
32173219
if let Some(sr_id) = shared_revision_per_collection_to_delete.get_mut(&rel_col) {
32183220
if let Some(sr_count) = sr_id.get_mut(sh_rev_id) {
32193221
*sr_count += 1;
@@ -3233,9 +3235,9 @@ pub fn delete_multiple_objects(
32333235
let shared_counts_before =
32343236
shared_revisions_per_collection
32353237
.get(&col_id)
3236-
.ok_or(ArunaError::InvalidRequest(
3237-
"Shared revision does not exist".to_string(),
3238-
))?;
3238+
.ok_or_else(|| {
3239+
ArunaError::InvalidRequest("Shared revision does not exist".to_string())
3240+
})?;
32393241

32403242
for (sh_rev_id, counts_deleted) in shared_counts_deleted {
32413243
if let Some(counts_before) = shared_counts_before.get(&sh_rev_id) {

src/database/crud/objectgroups.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ impl Database {
640640
// Deletion transaction
641641
self.pg_connection.get()?.transaction::<_, Error, _>(|conn| {
642642
// Query the relevant object groups from the database
643-
let queried_groups = if true { // request.with_revisions {
643+
let queried_groups = if request.with_revisions {
644644
// If with_revisions == true:
645645
// - Query all revisions of provided object group and delete them descending
646646
let og_shared_revision_id = object_groups
@@ -673,7 +673,7 @@ impl Database {
673673
delete(object_group_key_value)
674674
.filter(
675675
crate::database::schema::object_group_key_value::object_group_id.eq(
676-
object_group_uuid
676+
&queried_group.id
677677
)
678678
)
679679
.execute(conn)?;
@@ -682,7 +682,7 @@ impl Database {
682682
delete(collection_object_groups)
683683
.filter(
684684
crate::database::schema::collection_object_groups::object_group_id.eq(
685-
object_group_uuid
685+
&queried_group.id
686686
)
687687
)
688688
.execute(conn)?;
@@ -691,15 +691,15 @@ impl Database {
691691
delete(object_group_objects)
692692
.filter(
693693
crate::database::schema::object_group_objects::object_group_id.eq(
694-
object_group_uuid
694+
&queried_group.id
695695
)
696696
)
697697
.execute(conn)?;
698698

699699
// Rename ObjectGroup revision name and description to "DELETED"
700700
update(object_groups)
701701
.filter(
702-
crate::database::schema::object_groups::id.eq(object_group_uuid)
702+
crate::database::schema::object_groups::id.eq(&queried_group.id)
703703
)
704704
.set((
705705
crate::database::schema::object_groups::name.eq(

tests/common/functions.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use aruna_rust_api::api::internal::v1::{Location, LocationType};
2-
use aruna_rust_api::api::storage::models::v1::{Permission, ProjectPermission};
2+
use aruna_rust_api::api::storage::models::v1::{
3+
ObjectGroupOverview, Permission, ProjectPermission,
4+
};
35
use aruna_rust_api::api::storage::services::v1::{
4-
EditUserPermissionsForProjectRequest, GetObjectByIdRequest, GetReferencesRequest,
5-
ObjectReference, UpdateObjectRequest,
6+
EditUserPermissionsForProjectRequest, GetObjectByIdRequest, GetObjectGroupByIdRequest,
7+
GetReferencesRequest, ObjectReference, UpdateObjectRequest,
68
};
79
use aruna_rust_api::api::storage::{
810
models::v1::{
@@ -513,7 +515,7 @@ pub fn get_object(collection_id: String, object_id: String) -> Object {
513515

514516
/// GetObjectById wrapper for simplified use in tests.
515517
#[allow(dead_code)]
516-
pub fn get_raw_db_object_by_id(object_id: &String) -> DbObject {
518+
pub fn get_raw_db_object_by_id(object_id: &str) -> DbObject {
517519
use diesel::prelude::*;
518520

519521
let db = database::connection::Database::new("postgres://root:test123@localhost:26257/test");
@@ -693,3 +695,20 @@ pub fn update_object(update: &TCreateUpdate) -> Object {
693695

694696
updated_object
695697
}
698+
699+
// Helper function to fetch an object group directly from database
700+
#[allow(dead_code)]
701+
pub fn get_raw_db_object_group(
702+
object_group_uuid: &String,
703+
collection_uuid: &String,
704+
) -> ObjectGroupOverview {
705+
let db = database::connection::Database::new("postgres://root:test123@localhost:26257/test");
706+
707+
db.get_object_group_by_id(&GetObjectGroupByIdRequest {
708+
group_id: object_group_uuid.to_string(),
709+
collection_id: collection_uuid.to_string(),
710+
})
711+
.unwrap()
712+
.object_group
713+
.unwrap()
714+
}

tests/e1_object_tests.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ fn delete_object_test() {
636636
is_specification: false,
637637
preferred_endpoint_id: "".to_string(),
638638
multi_part: false,
639-
hash: None, //ToDo: Implement?
639+
hash: None, // Note: Maybe has to be refactored for future hash validation
640640
};
641641

642642
let new_id = uuid::Uuid::new_v4();
@@ -848,13 +848,13 @@ fn get_object_test() {
848848
let get_obj_internal = db
849849
.get_object_by_id(
850850
&uuid::Uuid::parse_str(&new_obj).unwrap(),
851-
&uuid::Uuid::parse_str(&random_collection.id.to_string()).unwrap(),
851+
&uuid::Uuid::parse_str(&random_collection.id).unwrap(),
852852
)
853853
.unwrap()
854854
.object
855855
.unwrap();
856856

857-
assert_eq!(get_obj_internal.id.to_string(), new_obj);
857+
assert_eq!(get_obj_internal.id, new_obj);
858858
}
859859

860860
#[test]
@@ -1147,7 +1147,7 @@ fn delete_multiple_objects_test() {
11471147

11481148
// Check random_collection objects
11491149
let get_obj = GetObjectsRequest {
1150-
collection_id: source_collection.id.to_string(),
1150+
collection_id: source_collection.id,
11511151
page_request: None,
11521152
label_id_filter: None,
11531153
with_url: false,
@@ -1160,14 +1160,10 @@ fn delete_multiple_objects_test() {
11601160
// - obj_3_rev_0: moved writeable to random_collection2
11611161
assert_eq!(resp.len(), 0);
11621162

1163-
let obj_1_rev_0_check =
1164-
common::functions::get_raw_db_object_by_id(&rnd_obj_1_rev_0.id.to_string());
1165-
let obj_1_rev_1_check =
1166-
common::functions::get_raw_db_object_by_id(&rnd_obj_1_rev_1.id.to_string());
1167-
let obj_2_rev_0_check =
1168-
common::functions::get_raw_db_object_by_id(&rnd_obj_2_rev_0.id.to_string());
1169-
let obj_3_rev_0_check =
1170-
common::functions::get_raw_db_object_by_id(&rnd_obj_3_rev_0.id.to_string());
1163+
let obj_1_rev_0_check = common::functions::get_raw_db_object_by_id(&rnd_obj_1_rev_0.id);
1164+
let obj_1_rev_1_check = common::functions::get_raw_db_object_by_id(&rnd_obj_1_rev_1.id);
1165+
let obj_2_rev_0_check = common::functions::get_raw_db_object_by_id(&rnd_obj_2_rev_0.id);
1166+
let obj_3_rev_0_check = common::functions::get_raw_db_object_by_id(&rnd_obj_3_rev_0.id);
11711167

11721168
assert_eq!(obj_1_rev_0_check.object_status, ObjectStatus::AVAILABLE); // Read-only available in collection2
11731169
assert_eq!(obj_1_rev_1_check.object_status, ObjectStatus::AVAILABLE); // Revision 1 is still read-only in collection2

0 commit comments

Comments
 (0)