Skip to content

Commit edbe87d

Browse files
authored
Add tests for dashboards handlers (#173)
* Add tests for common handlers Signed-off-by: Sergio Castaño Arteaga <[email protected]> * Add user dashboard tests Signed-off-by: Sergio Castaño Arteaga <[email protected]> * Add community dashboard tests Signed-off-by: Sergio Castaño Arteaga <[email protected]> * Add group dashboard tests Signed-off-by: Sergio Castaño Arteaga <[email protected]> * Improve tests expectations Signed-off-by: Sergio Castaño Arteaga <[email protected]> * Consolidate handlers tests helpers Signed-off-by: Sergio Castaño Arteaga <[email protected]> --------- Signed-off-by: Sergio Castaño Arteaga <[email protected]>
1 parent 0e490d9 commit edbe87d

File tree

24 files changed

+5637
-498
lines changed

24 files changed

+5637
-498
lines changed

ocg-server/src/handlers/community/explore.rs

Lines changed: 31 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -326,36 +326,22 @@ pub(crate) async fn search_groups(
326326

327327
#[cfg(test)]
328328
mod tests {
329-
use std::collections::BTreeMap;
330-
331329
use axum::{
332330
body::{Body, to_bytes},
333331
http::{
334332
HeaderMap, HeaderValue, Request, StatusCode,
335333
header::{CACHE_CONTROL, CONTENT_TYPE, HOST},
336334
},
337335
};
338-
use chrono::{TimeZone, Utc};
339336
use tower::ServiceExt;
340337
use uuid::Uuid;
341338

342339
use crate::{
343-
db::{
344-
BBox,
345-
common::{SearchCommunityEventsOutput, SearchCommunityGroupsOutput},
346-
mock::MockDB,
347-
},
340+
db::mock::MockDB,
341+
handlers::tests::*,
348342
router::setup_test_router,
349343
services::notifications::MockNotificationsManager,
350-
templates::community::{
351-
explore::{self, FilterOption},
352-
pagination,
353-
},
354-
types::{
355-
community::{Community, Theme},
356-
event::{EventDetailed, EventKind},
357-
group::{GroupCategory, GroupDetailed, GroupRegion},
358-
},
344+
templates::community::{explore, pagination},
359345
};
360346

361347
#[tokio::test]
@@ -366,9 +352,11 @@ mod tests {
366352
// Setup database mock
367353
let mut db = MockDB::new();
368354
db.expect_get_community_id()
355+
.times(1)
369356
.withf(|host| host == "example.test")
370357
.returning(move |_| Ok(Some(community_id)));
371358
db.expect_get_community()
359+
.times(1)
372360
.withf(move |id| *id == community_id)
373361
.returning(|_| Err(anyhow::anyhow!("db error")));
374362

@@ -400,9 +388,11 @@ mod tests {
400388
// Setup database mock
401389
let mut db = MockDB::new();
402390
db.expect_get_community_id()
391+
.times(1)
403392
.withf(|host| host == "example.test")
404393
.returning(move |_| Ok(Some(community_id)));
405394
db.expect_get_community()
395+
.times(1)
406396
.withf(move |id| *id == community_id)
407397
.returning(move |_| Ok(sample_community(community_id)));
408398

@@ -434,9 +424,11 @@ mod tests {
434424
// Setup database mock
435425
let mut db = MockDB::new();
436426
db.expect_get_community_id()
427+
.times(1)
437428
.withf(|host| host == "example.test")
438429
.returning(move |_| Ok(Some(community_id)));
439430
db.expect_get_community()
431+
.times(1)
440432
.withf(move |id| *id == community_id)
441433
.returning(move |_| Ok(sample_community(community_id)));
442434

@@ -469,15 +461,19 @@ mod tests {
469461
// Setup database mock
470462
let mut db = MockDB::new();
471463
db.expect_get_community_id()
464+
.times(1)
472465
.withf(|host| host == "example.test")
473466
.returning(move |_| Ok(Some(community_id)));
474467
db.expect_get_community()
468+
.times(1)
475469
.withf(move |id| *id == community_id)
476470
.returning(move |_| Ok(sample_community(community_id)));
477471
db.expect_get_community_filters_options()
472+
.times(1)
478473
.withf(move |id| *id == community_id)
479474
.returning(|_| Ok(sample_filters_options()));
480475
db.expect_search_community_events()
476+
.times(1)
481477
.withf(move |id, _| *id == community_id)
482478
.returning(move |_, _| Ok(sample_search_community_events_output(event_id)));
483479

@@ -518,15 +514,19 @@ mod tests {
518514
// Setup database mock
519515
let mut db = MockDB::new();
520516
db.expect_get_community_id()
517+
.times(1)
521518
.withf(|host| host == "example.test")
522519
.returning(move |_| Ok(Some(community_id)));
523520
db.expect_get_community()
521+
.times(1)
524522
.withf(move |id| *id == community_id)
525523
.returning(move |_| Ok(sample_community(community_id)));
526524
db.expect_get_community_filters_options()
525+
.times(1)
527526
.withf(move |id| *id == community_id)
528527
.returning(|_| Ok(sample_filters_options()));
529528
db.expect_search_community_groups()
529+
.times(1)
530530
.withf(move |id, _| *id == community_id)
531531
.returning(move |_, _| Ok(sample_search_community_groups_output(group_id)));
532532

@@ -567,12 +567,15 @@ mod tests {
567567
// Setup database mock
568568
let mut db = MockDB::new();
569569
db.expect_get_community_id()
570+
.times(1)
570571
.withf(|host| host == "example.test")
571572
.returning(move |_| Ok(Some(community_id)));
572573
db.expect_get_community_filters_options()
574+
.times(1)
573575
.withf(move |id| *id == community_id)
574576
.returning(|_| Ok(sample_filters_options()));
575577
db.expect_search_community_events()
578+
.times(1)
576579
.withf(move |id, _| *id == community_id)
577580
.returning(move |_, _| Ok(sample_search_community_events_output(event_id)));
578581

@@ -618,9 +621,11 @@ mod tests {
618621
// Setup database mock
619622
let mut db = MockDB::new();
620623
db.expect_get_community_id()
624+
.times(1)
621625
.withf(|host| host == "example.test")
622626
.returning(move |_| Ok(Some(community_id)));
623627
db.expect_search_community_events()
628+
.times(1)
624629
.withf(move |id, _| *id == community_id)
625630
.returning(move |_, _| Ok(sample_search_community_events_output(event_id)));
626631

@@ -666,12 +671,15 @@ mod tests {
666671
// Setup database mock
667672
let mut db = MockDB::new();
668673
db.expect_get_community_id()
674+
.times(1)
669675
.withf(|host| host == "example.test")
670676
.returning(move |_| Ok(Some(community_id)));
671677
db.expect_get_community_filters_options()
678+
.times(1)
672679
.withf(move |id| *id == community_id)
673680
.returning(|_| Ok(sample_filters_options()));
674681
db.expect_search_community_groups()
682+
.times(1)
675683
.withf(move |id, _| *id == community_id)
676684
.returning(move |_, _| Ok(sample_search_community_groups_output(group_id)));
677685

@@ -717,9 +725,11 @@ mod tests {
717725
// Setup database mock
718726
let mut db = MockDB::new();
719727
db.expect_get_community_id()
728+
.times(1)
720729
.withf(|host| host == "example.test")
721730
.returning(move |_| Ok(Some(community_id)));
722731
db.expect_search_community_groups()
732+
.times(1)
723733
.withf(move |id, _| *id == community_id)
724734
.returning(move |_, _| Ok(sample_search_community_groups_output(group_id)));
725735

@@ -765,9 +775,11 @@ mod tests {
765775
// Setup database mock
766776
let mut db = MockDB::new();
767777
db.expect_get_community_id()
778+
.times(1)
768779
.withf(|host| host == "example.test")
769780
.returning(move |_| Ok(Some(community_id)));
770781
db.expect_search_community_events()
782+
.times(1)
771783
.withf(move |id, _| *id == community_id)
772784
.returning(move |_, _| Ok(sample_search_community_events_output(event_id)));
773785

@@ -808,9 +820,11 @@ mod tests {
808820
// Setup database mock
809821
let mut db = MockDB::new();
810822
db.expect_get_community_id()
823+
.times(1)
811824
.withf(|host| host == "example.test")
812825
.returning(move |_| Ok(Some(community_id)));
813826
db.expect_search_community_groups()
827+
.times(1)
814828
.withf(move |id, _| *id == community_id)
815829
.returning(move |_, _| Ok(sample_search_community_groups_output(group_id)));
816830

@@ -857,139 +871,4 @@ mod tests {
857871
let filters = explore::GroupsFilters::new(&headers, raw_query).unwrap();
858872
pagination::build_url("/explore?entity=groups", &filters).unwrap()
859873
}
860-
861-
/// Helper to create a sample bounding box for tests.
862-
fn sample_bbox() -> BBox {
863-
BBox {
864-
ne_lat: 1.0,
865-
ne_lon: 2.0,
866-
sw_lat: -1.0,
867-
sw_lon: -2.0,
868-
}
869-
}
870-
871-
/// Helper to create a sample community for tests.
872-
fn sample_community(community_id: Uuid) -> Community {
873-
Community {
874-
active: true,
875-
community_id,
876-
community_site_layout_id: "default".to_string(),
877-
created_at: 0,
878-
description: "Test community".to_string(),
879-
display_name: "Test".to_string(),
880-
header_logo_url: "/static/images/placeholder_cncf.png".to_string(),
881-
host: "example.test".to_string(),
882-
name: "test".to_string(),
883-
theme: Theme {
884-
palette: BTreeMap::new(),
885-
primary_color: "#000000".to_string(),
886-
},
887-
title: "Test Community".to_string(),
888-
..Default::default()
889-
}
890-
}
891-
892-
/// Helper to create a sample event for tests.
893-
fn sample_event(event_id: Uuid) -> EventDetailed {
894-
EventDetailed {
895-
event_id,
896-
group_category_name: "Category".to_string(),
897-
group_name: "Group".to_string(),
898-
group_slug: "group".to_string(),
899-
kind: EventKind::InPerson,
900-
name: "Sample Event".to_string(),
901-
published: true,
902-
slug: "sample-event".to_string(),
903-
timezone: chrono_tz::UTC,
904-
ends_at: Some(Utc.timestamp_opt(1_000, 0).unwrap()),
905-
group_city: Some("City".to_string()),
906-
group_country_code: Some("US".to_string()),
907-
group_country_name: Some("United States".to_string()),
908-
group_state: Some("CA".to_string()),
909-
latitude: Some(1.0),
910-
longitude: Some(2.0),
911-
starts_at: Some(Utc.timestamp_opt(0, 0).unwrap()),
912-
venue_address: Some("123 Main St".to_string()),
913-
venue_city: Some("City".to_string()),
914-
venue_name: Some("Venue".to_string()),
915-
..Default::default()
916-
}
917-
}
918-
919-
/// Helper to create sample filter options for tests.
920-
fn sample_filters_options() -> explore::FiltersOptions {
921-
explore::FiltersOptions {
922-
distance: vec![FilterOption {
923-
name: "5 km".to_string(),
924-
value: "5".to_string(),
925-
}],
926-
event_category: vec![FilterOption {
927-
name: "Category".to_string(),
928-
value: "category".to_string(),
929-
}],
930-
group_category: vec![FilterOption {
931-
name: "Category".to_string(),
932-
value: "category".to_string(),
933-
}],
934-
region: vec![FilterOption {
935-
name: "Region".to_string(),
936-
value: "region".to_string(),
937-
}],
938-
}
939-
}
940-
941-
/// Helper to create a sample group for tests.
942-
fn sample_group(group_id: Uuid) -> GroupDetailed {
943-
let group_category_id = Uuid::from_u128(2);
944-
let region_id = Uuid::from_u128(3);
945-
946-
GroupDetailed {
947-
active: true,
948-
category: GroupCategory {
949-
group_category_id,
950-
name: "Category".to_string(),
951-
normalized_name: "category".to_string(),
952-
order: None,
953-
},
954-
created_at: Utc.timestamp_opt(0, 0).unwrap(),
955-
group_id,
956-
name: "Sample Group".to_string(),
957-
slug: "sample-group".to_string(),
958-
city: Some("City".to_string()),
959-
country_code: Some("US".to_string()),
960-
country_name: Some("United States".to_string()),
961-
latitude: Some(1.0),
962-
longitude: Some(2.0),
963-
region: Some(GroupRegion {
964-
region_id,
965-
name: "Region".to_string(),
966-
normalized_name: "region".to_string(),
967-
order: None,
968-
}),
969-
state: Some("CA".to_string()),
970-
..Default::default()
971-
}
972-
}
973-
974-
/// Helper to create sample events search output for tests.
975-
fn sample_search_community_events_output(event_id: Uuid) -> SearchCommunityEventsOutput {
976-
let mut output = SearchCommunityEventsOutput {
977-
events: vec![sample_event(event_id)],
978-
total: 1,
979-
..Default::default()
980-
};
981-
output.bbox = Some(sample_bbox());
982-
output
983-
}
984-
985-
/// Helper to create sample groups search output for tests.
986-
fn sample_search_community_groups_output(group_id: Uuid) -> SearchCommunityGroupsOutput {
987-
let mut output = SearchCommunityGroupsOutput {
988-
groups: vec![sample_group(group_id)],
989-
total: 1,
990-
..Default::default()
991-
};
992-
output.bbox = Some(sample_bbox());
993-
output
994-
}
995874
}

0 commit comments

Comments
 (0)